|
/*
Eva library C++ (www.elxala.de)
Copyright (C) 2005 Alejandro Xalabarder Aulet
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
EvaFile.cpp
author Alejandro Xalabarder
@todo General review, document it in english and make it conform with Eva specification 1.1
*/
#include "EvaFile.h"
#include <stdio.h>
#include <stdlib.h>
#define MAX_LINE_SIZE (2048)
const char EvaFile::MARCA_Unit [] = "#";
const char EvaFile::MARCA_Eva1 [] = "<";
const char EvaFile::MARCA_Eva2 [] = ">";
const char EvaFile::MARCA_FinEva [] = "#**";
bool EvaFile::getEvaUnitCatalog (const char * FileName, vector<string> & vCatalog)
{
vCatalog.clear ();
if (false == OpenFile (FileName))
return false;
while (SiguienteUniEva())
vCatalog.push_back (cNombre);
CloseFile ();
return true;
}
bool EvaFile::load (EvaUnit & eUnit, const char * FileName, const char * UnitName)
{
Eva esaEva;
EvaLine esaLin;
int que;
eUnit.clear ();
eUnit.Nombre = UnitName;
if (false == OpenFile (FileName)) return false; // >>>>
if (BuscaUniEva (UnitName) && SiguienteEva()) {
esaEva.clear ();
esaEva.Nombre = cNombre;
do {
que = LeeLineaNeta (true);
switch (que) {
case esEVA:
eUnit.add (esaEva);
esaEva.clear ();
esaEva.Nombre = cNombre;
break;
case esDATOS:
esaLin.clear ();
esaLin.set (cLinFiltrada);
esaEva.addLine (esaLin);
break;
}
} while (que >= esEVA);
eUnit.add (esaEva);
}
CloseFile ();
return true;
}
bool EvaFile::saveSingleUnit (EvaUnit & ueva, const char * FileName)
{
FILE * elFix = fopen (FileName, "w");
if (elFix == NULL) return false; // access forbiden ?
fprintf (elFix, "%s\n", ueva.toString ().c_str ());
fclose (elFix);
return true;
}
bool EvaFile::OpenFile (const char * FileName)
{
cNombre = "";
cLinFiltrada = "";
cLinBruta = "";
cLinSobrante = "";
m_file = fopen (FileName, "r");
return (m_file != NULL);
}
bool EvaFile::ReadLine ()
{
if (cLinSobrante.length () > 0)
{
cLinBruta = cLinSobrante;
}
else
{
char ss [MAX_LINE_SIZE + 1];
cLinBruta = "";
//(o) celtaC++_util_EvaFile_FALTA controlar error
if (NULL == fgets (ss, MAX_LINE_SIZE, m_file)) return false;
//(o) celtaC++_util_EvaFile_FALTA quitar el retorno m�s limpiamente
while (strlen(ss) > 0 && ss[strlen(ss)-1] < (char) 15) ss[strlen(ss)-1] = 0;
cLinBruta = ss;
if (feof (m_file) && cLinBruta.length () == 0) return false;
}
cLinFiltrada.setStr (cLinBruta.c_str ());
cLinFiltrada.trimMe ();
cLinSobrante = "";
return true;
}
void EvaFile::CloseFile ()
{
fclose (m_file);
}
bool EvaFile::BuscaUniEva (const char * nom)
{
while (SiguienteUniEva())
if (nom == cNombre) // FALTA ignore case ....!!!!!
return true;
return false;
}
EvaFile::rLineType EvaFile::QueEs ()
{
Cadena sLin = cLinFiltrada;
// nothing ?
if (sLin.length () == 0)
return esCOMENTARIO; // >>>>
// no #Unit# no <Eva> ?
char letra = sLin.charAt (0);
if (letra != MARCA_Unit[0] && letra != MARCA_Eva1[0])
return esDATOS; // >>>>
// soft end of file ?
//if (MARCA_FinEva.equals (sLin.substrBE (0, MARCA_FinEva.length())))
if (sLin.InitialMatch (MARCA_FinEva))
return esFIN_FICHERO_SOFT; // >>>>
// is it a Comment line ?
// "# " or "< " (followed by a blank)
// #Unit# or <Eva> not are correctly ended ?
if ((sLin.length () <= 2) || (sLin.charAt (1) == ' '))
return esCOMENTARIO; // >>>>
int woEnd;
if (letra == MARCA_Unit[0])
woEnd = sLin.indexOf (MARCA_Unit, 1);
else
woEnd = sLin.indexOf (MARCA_Eva2, 1);
if (woEnd == -1)
return esCOMENTARIO; // >>>>
// #Unit# or <Eva> : take the name and delete this from sLin ?
cNombre = sLin.substrBE (1, woEnd-1);
const char * pp = cNombre.c_str ();
cLinSobrante = sLin.substrBE (woEnd+1, sLin.length ());
pp = cLinSobrante.c_str ();
return (letra == MARCA_Unit[0]) ? esUNIDAD: esEVA;
}
EvaFile::rLineType EvaFile::LeeLineaNeta (bool AhorraComentario)
{
rLineType ques;
do {
if (!ReadLine ())
return esFIN_FICHERO_HARD; // >>>>
ques = QueEs ();
} while (AhorraComentario && (ques == esCOMENTARIO));
return ques;
}
EvaFile::rLineType EvaFile::NextUoE ()
{
rLineType quees;
do {
quees = LeeLineaNeta (true);
if ((quees == esUNIDAD) || (quees == esEVA))
return quees;
} while (quees > esFIN_FICHERO);
return quees;
}
bool EvaFile::SiguienteEva()
{
return (NextUoE () == esEVA);
}
/**
* Buscar en un fichero EVA la siguiente unidad
* Retorna: TRUE si la encuentra y FALSE si fin de fichero o FinEva
*/
bool EvaFile::SiguienteUniEva()
{
rLineType ques;
do {
ques = NextUoE ();
if (ques == esUNIDAD) return true;
} while (ques == esEVA);
return false;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I have a Telecomunications Engineering degree but since I learnt the first language (Pascal), almost I haven't stopped developing software. Mainly with C and C++ but I have touched many other languages and scripts. I use also to develop for fun (reinventing the wheel, of course!), currently almost all of such programs and tools are written in java (visit www.elxala.de).