File functions.
//////////////////////////////////////////////////////////////////////////////
// FILE : fil.tol
// AUTHOR : http://www.asolver.com
// PURPOSE : File functions.
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// FUNCTIONS
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Real FilDateLT(Text filInp, // Input file
Text filOut) // Output file
//////////////////////////////////////////////////////////////////////////////
{
If(Not(FileExist(filInp)), TRUE,
If(Not(FileExist(filOut)), FALSE,
FileTime(filInp) < FileTime(filOut)))
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns true if the date of the file filInp is less than the date of the
file filOut or filInp doesn't exist.
Returns false if the date of the file filInp is great or equal than the date
of the file filOut or filOut does not exist.
This functions lets build makefiles systems.",
FilDateLT);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Text FilKbytes(Text filPth, // Input file
Text defTxt) // Default text
//////////////////////////////////////////////////////////////////////////////
{
If(Not(FileExist(filPth)), defTxt,
FormatReal(FileBytes(filPth)/1024, "%.0lf")+" KBytes")
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns a text expression with the integer size of a file in KBytes.
If the file dows not exist returns the default text defTxt.",
FilKbytes);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Real FilMake(Text filInp, // Input file
Text filOut, // Output file
Code makFun) // Real make function with 2 text arguments
//////////////////////////////////////////////////////////////////////////////
{
If(FilDateLT(filInp, filOut), TRUE,
makFun(filInp, filOut))
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"If file filInp exist and is newer than file filOut then applies the 2
file arguments makFun to build filOut from filInp and returns the real result
of makFun.
Returns TRUE if filOut is newer than filInp.
Example, makes a jpg file from a tif file:
Real tinDon = FilMake(clsTif, tinJpg, Real(Text inp, Text out)
{ AlcImg2JpgDpi(inp, out, 199, 299, 0) });",
FilMake);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Real FilMakeSet(Set filSet, // Input file
Code filOut, // Text function to obtain output from input file
Code makFun) // Real make function with 2 text arguments
//////////////////////////////////////////////////////////////////////////////
{
Set makCic = EvalSet(filSet, Real(Text filInp)
{ FilMake(filInp, filOut(filInp), makFun) });
SetSum(makCic)
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Applie FilMake() to a set of files.
The path of the output of each file in filSet is obtained appling filOut()
to the path of each element in filSet.",
FilMakeSet);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Real FilMakeUpdate(Text filInp, // Input file
Text filOut, // Output file
Code makFun) // Real make function with 2 text arguments
//////////////////////////////////////////////////////////////////////////////
{
If(Not(FileExist(filOut)), FALSE,
FilMake(filInp, filOut, makFun))
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"If file filInp exist and is newer than file filOut then applies the 2
file arguments makFun to build filOut from filInp and returns the real result
of makFun.
Returns TRUE if filOut is newer than filInp.
Only works if filOut already exists.
Example, update a zip file if the zip already exists and the jpg is newer than
the zip file:
Real zipDon = FilMakeUpdate(da4Jpg, da4Zip, ZipAdd);",
FilMakeUpdate);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Real FilOccurrences(Text filPth, // Input file path
Text txtPat) // Pattern
//////////////////////////////////////////////////////////////////////////////
{
If(Not(FileExist(filPth)), 0,
{
Text filTxt = ReadFile(filPth);
TextOccurrences(filTxt, txtPat)
})
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Returns the number of occurrences of the searched text txtPat within a
file filPth.
If the file filPth does not exist then returns 0.",
FilOccurrences);
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
Real FilSplit(Text inpFil, // File name
Real overWr, // If true overwrite the output files
Code filOut) // Function that returns files paths
//////////////////////////////////////////////////////////////////////////////
{
Set ctrSet = Copy(Empty); // Output file control
// Open as Bdb
Set inpBdb = BDBOpen(inpFil,Copy(FALSE),"\n","","");
Real maxReg = inpBdb->RegNumber;
// Procesar desde iniOk hasta endOk
Real numReg = 1;
Real While(LE(numReg,maxReg),
{
Text inpLin = BDBReg(inpBdb, numReg)[1]; // Read a line
Text outFil = filOut(inpLin);
Real chkOve = If(Not(overWr), FALSE, // Not overwrite
{
Real ctrPos = SetTxtFindFirst(ctrSet, outFil, FALSE);
Real notNew = If(GT(ctrPos,0), TRUE, // Already writed,
{
Set(ctrSet:=ctrSet<<[[Copy(outFil)]]); // Insert the file path
Text WriteFile(outFil,""); // Overwrite
FALSE
})
});
Text AppendFile(outFil,inpLin+"\n"); // La agrega al final
Real(numReg:=numReg+1);
TRUE
});
Real BDBClose(inpBdb);
numReg
};
//////////////////////////////////////////////////////////////////////////////
PutDescription(
"Split a file (inpFil) y several files and returns the number of lines
processed.
This function applies the argument function filOut() to each line of the file,
filOut() function must return a output file path where the line will be
append.
If the argument overWr is false this function only append lines (not
overwrite) to output files.
If the argument overWr is true this function overwrite the output files before
the first append.
An example of use in ApaSplitFile() function.",
FilSplit);
//////////////////////////////////////////////////////////////////////////////
Ink.Watercolor construye las páginas del sitio web inkwatercolor.com
2015 asolver.com | Aviso legal | XHTML | Δ Θ Ξ | Creative Commons | Mapa y funciones del sitio