Posts database functions.
Constantes
Text PdbSepFunciones
Set PdbRead(Text inpDir)Set PdbFirstN(Set inpSet, Real maxNum, Code funSel)Real PdbAppend(Text pstTxt)//////////////////////////////////////////////////////////////////////////////
Text PdbSep = Repeat("_",78);
//////////////////////////////////////////////////////////////////////////////
PutDescription("Post separator inside the agendas.", PdbSep);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set PdbRead(Text inpDir)
//////////////////////////////////////////////////////////////////////////////
{
Real err(Text msg){ Text WriteLn("\nERROR: "+msg+"\n"); FALSE }; // Function
Text filSep = Char(7);
Text WriteLn("Reading "+inpDir+"...");
Text inpAll = DirReadFiles(inpDir, "age", filSep);
Text inpTxt = Replace(inpAll, PdbSep+"\n",filSep);
Set inpSet = Tokenizer(inpTxt,filSep);
Set inpTab = EvalSet(inpSet, Set(Text inf)
{
// Read
Set pstCla = Txt2Set(TxtBetween2Tag(inf,"<Pst.Cla>", "\n<", TRUE),";");
Text pstNam = TxtBetween2Tag(inf,"<Pst.Nam>", "\n<", TRUE);
Text pstTit = TxtBetween2Tag(inf,"<Pst.Tit>", "\n<", TRUE);
Date pstDte = Eval(TxtBetween2Tag(inf,"<Pst.Dte>", "\n<", TRUE));
Date pstUpd = Eval(TxtBetween2Tag(inf,"<Pst.Upd>", "\n<", TRUE));
Text pstTxt = TxtBetween2Tag(inf,"<Pst.Txt>", "\n<", FALSE);
Text pstLnk = TxtBetween2Tag(inf,"<Pst.Lnk>", "\n<", FALSE);
// Check classes
Set chkCla = EvalSet(pstCla, Real(Text claNam) // Category class
{ If(claNam <: CatAll, TRUE, err("Class: "+pstNam+" "+claNam)) });
Real chkPer = // Check periods and dates
{
Text chkLst = pstCla[Card(pstCla)]; // The period must be the last
Set chkSet = Tokenizer(chkLst, "-");
Text chkIni = chkSet[1];
Text chkEnd = chkSet[2];
Text chkYea = FormatReal(Year(pstDte),"%4.0lf");
If(And(chkYea >= chkIni, chkYea <= chkEnd), TRUE,
err(pstNam+"Period and date: "+chkIni+" ?<= "+chkYea+" ?<= "+chkEnd))
};
Real chkNam = // Check names and dates
{
Text yeaNam = Sub(TxtBetween2Tag(pstNam, ",", "]", TRUE),1,4);
Text yeaDte = FormatReal(Year(pstDte),"%4.0lf");
If(yeaNam == yeaDte, TRUE,
err("Name and date: "+pstNam+" ? "+yeaDte))
};
// Store and returns a post object
PdbSt(pstCla, pstNam, pstTit, pstDte, pstUpd, pstTxt, pstLnk)
});
Set inpSor = Sort(inpTab, Real(Set a, Set b) // Lasts first
{ Compare(b->pstDte,a->pstDte) });
// Check duplicated names
Real chkDup =
{
Set inpCla = Classify(inpTab, Real(Set a, Set b) // By name
{ Compare(a->pstNam,b->pstNam) });
Set inpDup = EvalSet(inpCla, Real(Set cla)
{
Real crd = Card(cla);
Text nam = cla[1]->pstNam;
If(EQ(crd,1), TRUE, err("Name: "+nam+" "+
FormatReal(crd,"%.0lf")+" times"))
});
Card(inpDup)
};
Text WriteLn("Readed "+FormatReal(Card(inpSor),"%.0lf")+" registers");
inpSor
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Reads and returns a post database.",
PdbRead);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set PdbFirstN(Set inpSet, // Post database
Real maxNum, // Maximum numbers of posts to return
Code funSel) // Post selection conditions
//////////////////////////////////////////////////////////////////////////////
{
Set selFst = Select(inpSet, funSel); // Select all that funSel()
SetFirstN(selFst, maxNum) // Fst maxNum or all if there are few
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns the maxNum recents posts selected using the function funSel.",
PdbFirstN);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Real PdbAppend(Text pstTxt) // Post text
//////////////////////////////////////////////////////////////////////////////
{
Text pstOut = pstTxt + PdbSep + "\n";
Text Case(
!TextFind(pstTxt, "<Pst.Nam> [Salmerón A."),
AppendFile(CtrAge+"/06.externos.txt", pstOut), // Otros autores
TextFind(pstTxt, "<iframe"),
AppendFile(CtrAge+"/05.iframe.txt", pstOut), // Videos
TextFind(pstTxt, "; Educación básica;"),
AppendFile(CtrAge+"/04.niñas.txt", pstOut), // Niñas educacion
TextFind(pstTxt, "<Pst.Cla> Pintura y poesía;"),
AppendFile(CtrAge+"/03.arte.txt", pstOut), // Arte
TextFind(pstTxt, "<Pst.Cla> Negocios;"),
AppendFile(CtrAge+"/02.negocios.txt", pstOut), // Negocios
TextFind(pstTxt, "<Pst.Cla> Tecnología;"),
AppendFile(CtrAge+"/01.tecnologia.txt", pstOut), // Tecnologia
TRUE,
AppendFile(CtrAge+"/00.inclasificable.txt", pstOut) // Error
);
TRUE
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Guarda el contenido de un post en un fichero.
Esta funcion solo se emplea cuando es necesario convertir una agenda grande
en varias pequeñas.
A los ficheros se les pone extension txt y no age, pero poder revisarlos
antes de la siguiente carga.",
PdbAppend);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// FILE : pdb.tol
// AUTHOR : http://www.asolver.com
// PURPOSE : Posts database functions.
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// CONSTANTS
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text PdbSep = Repeat("_",78);
//////////////////////////////////////////////////////////////////////////////
PutDescription("Post separator inside the agendas.", PdbSep);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set PdbRead(Text inpDir)
//////////////////////////////////////////////////////////////////////////////
{
Real err(Text msg){ Text WriteLn("\nERROR: "+msg+"\n"); FALSE }; // Function
Text filSep = Char(7);
Text WriteLn("Reading "+inpDir+"...");
Text inpAll = DirReadFiles(inpDir, "age", filSep);
Text inpTxt = Replace(inpAll, PdbSep+"\n",filSep);
Set inpSet = Tokenizer(inpTxt,filSep);
Set inpTab = EvalSet(inpSet, Set(Text inf)
{
// Read
Set pstCla = Txt2Set(TxtBetween2Tag(inf,"<Pst.Cla>", "\n<", TRUE),";");
Text pstNam = TxtBetween2Tag(inf,"<Pst.Nam>", "\n<", TRUE);
Text pstTit = TxtBetween2Tag(inf,"<Pst.Tit>", "\n<", TRUE);
Date pstDte = Eval(TxtBetween2Tag(inf,"<Pst.Dte>", "\n<", TRUE));
Date pstUpd = Eval(TxtBetween2Tag(inf,"<Pst.Upd>", "\n<", TRUE));
Text pstTxt = TxtBetween2Tag(inf,"<Pst.Txt>", "\n<", FALSE);
Text pstLnk = TxtBetween2Tag(inf,"<Pst.Lnk>", "\n<", FALSE);
// Check classes
Set chkCla = EvalSet(pstCla, Real(Text claNam) // Category class
{ If(claNam <: CatAll, TRUE, err("Class: "+pstNam+" "+claNam)) });
Real chkPer = // Check periods and dates
{
Text chkLst = pstCla[Card(pstCla)]; // The period must be the last
Set chkSet = Tokenizer(chkLst, "-");
Text chkIni = chkSet[1];
Text chkEnd = chkSet[2];
Text chkYea = FormatReal(Year(pstDte),"%4.0lf");
If(And(chkYea >= chkIni, chkYea <= chkEnd), TRUE,
err(pstNam+"Period and date: "+chkIni+" ?<= "+chkYea+" ?<= "+chkEnd))
};
Real chkNam = // Check names and dates
{
Text yeaNam = Sub(TxtBetween2Tag(pstNam, ",", "]", TRUE),1,4);
Text yeaDte = FormatReal(Year(pstDte),"%4.0lf");
If(yeaNam == yeaDte, TRUE,
err("Name and date: "+pstNam+" ? "+yeaDte))
};
// Store and returns a post object
PdbSt(pstCla, pstNam, pstTit, pstDte, pstUpd, pstTxt, pstLnk)
});
Set inpSor = Sort(inpTab, Real(Set a, Set b) // Lasts first
{ Compare(b->pstDte,a->pstDte) });
// Check duplicated names
Real chkDup =
{
Set inpCla = Classify(inpTab, Real(Set a, Set b) // By name
{ Compare(a->pstNam,b->pstNam) });
Set inpDup = EvalSet(inpCla, Real(Set cla)
{
Real crd = Card(cla);
Text nam = cla[1]->pstNam;
If(EQ(crd,1), TRUE, err("Name: "+nam+" "+
FormatReal(crd,"%.0lf")+" times"))
});
Card(inpDup)
};
Text WriteLn("Readed "+FormatReal(Card(inpSor),"%.0lf")+" registers");
inpSor
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Reads and returns a post database.",
PdbRead);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Set PdbFirstN(Set inpSet, // Post database
Real maxNum, // Maximum numbers of posts to return
Code funSel) // Post selection conditions
//////////////////////////////////////////////////////////////////////////////
{
Set selFst = Select(inpSet, funSel); // Select all that funSel()
SetFirstN(selFst, maxNum) // Fst maxNum or all if there are few
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns the maxNum recents posts selected using the function funSel.",
PdbFirstN);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Real PdbAppend(Text pstTxt) // Post text
//////////////////////////////////////////////////////////////////////////////
{
Text pstOut = pstTxt + PdbSep + "\n";
Text Case(
!TextFind(pstTxt, "<Pst.Nam> [Salmerón A."),
AppendFile(CtrAge+"/06.externos.txt", pstOut), // Otros autores
TextFind(pstTxt, "<iframe"),
AppendFile(CtrAge+"/05.iframe.txt", pstOut), // Videos
TextFind(pstTxt, "; Educación básica;"),
AppendFile(CtrAge+"/04.niñas.txt", pstOut), // Niñas educacion
TextFind(pstTxt, "<Pst.Cla> Pintura y poesía;"),
AppendFile(CtrAge+"/03.arte.txt", pstOut), // Arte
TextFind(pstTxt, "<Pst.Cla> Negocios;"),
AppendFile(CtrAge+"/02.negocios.txt", pstOut), // Negocios
TextFind(pstTxt, "<Pst.Cla> Tecnología;"),
AppendFile(CtrAge+"/01.tecnologia.txt", pstOut), // Tecnologia
TRUE,
AppendFile(CtrAge+"/00.inclasificable.txt", pstOut) // Error
);
TRUE
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Guarda el contenido de un post en un fichero.
Esta funcion solo se emplea cuando es necesario convertir una agenda grande
en varias pequeñas.
A los ficheros se les pone extension txt y no age, pero poder revisarlos
antes de la siguiente carga.",
PdbAppend);
//////////////////////////////////////////////////////////////////////////////
Antonio.Salmeron construye las páginas y documentos del sitio web antoniosalmeron.con
2015 asolver.com | Aviso legal | XHTML | Δ Θ Ξ | Creative Commons | Mapa y funciones del sitio