leeXML.cpp

Ir a la documentación de este archivo.
00001 // vim: set expandtab tabstop=8 shiftwidth=8 foldmethod=marker:
00012 #include <stdio.h>
00013 #include <iostream>
00014 #include "expat.h"
00015 #include "NodoTrieS.hpp"
00016 
00017 using namespace std;
00018 #if defined(__amigaos__) && defined(__USE_INLINE__)
00019 #include <proto/expat.h>
00020 #endif
00021 
00022 #ifdef XML_LARGE_SIZE
00023 #if defined(XML_USE_MSC_EXTENSIONS) && _MSC_VER < 1400
00024 #define XML_FMT_INT_MOD "I64"
00025 #else
00026 #define XML_FMT_INT_MOD "ll"
00027 #endif
00028 #else
00029 #define XML_FMT_INT_MOD "l"
00030 #endif
00031 
00032 const int MAXPROF = 100;
00033 
00034 const char *elem[MAXPROF];
00035 const char **vatts[MAXPROF];
00036 
00037 int profundidad = 0;
00038 int numdoc = -1;
00039 NodoTrieS *nodotries;
00040 XML_Parser parser = NULL;
00041 
00042 
00043 static void XMLCALL
00044 startElement(void *userData, const char *name, const char **atts)
00045 {
00046         /*int i;
00047          for (i = 0; i < profundidad; i++)
00048             putchar('\t');
00049           puts(name); 
00050           for (i = 0; atts[i] != NULL; i+=2) {
00051                   printf("%s -> %s\n", atts[i], atts[i+1]);
00052           } */
00053         if (profundidad < MAXPROF ) {
00054                 elem[profundidad] = name;
00055                 vatts[profundidad] = atts;
00056         }
00057         profundidad ++;
00058 }
00059 
00060 static void XMLCALL
00061 endElement(void *userData, const char *name)
00062 {
00063         profundidad--;
00064 }
00065 
00066 static void XMLCALL
00067 charHandler(void *userData, const char *s, int len)
00068 {
00069         if (len >= 0 && profundidad > 0 && profundidad < MAXPROF) {
00070                 string pal = utf8_a_latin1(s, len);
00071                 //cout << elem[profundidad - 1] << ":" << pal << endl;
00072                 nodotries->insertaConEtiqueta(pal, elem[profundidad - 1],
00073                                               numdoc, XML_GetCurrentByteIndex(parser) + 1);
00074         }
00075 }
00076 
00077 
00079 void leeXML(const char *na, long ndoc, NodoTrieS &t)
00080 {
00081         ASSERT(na!=NULL && na[0] != '\0' && strlen(na)<FILENAME_MAX);
00082         ASSERT(ndoc >= 0);
00083 
00084         numdoc = ndoc;
00085         char buf[BUFSIZ];
00086         parser = XML_ParserCreate("ISO-8859-1");
00087         if (XML_SetEncoding(parser, "ISO-8859-1") != XML_STATUS_OK) {
00088                 throw "No puede ponerse codificación ISO-8859-1" ;
00089         }
00090         int done;
00091         int depth = 0;
00092         XML_SetUserData(parser, &depth);
00093         XML_SetElementHandler(parser, startElement, endElement);
00094         XML_SetCharacterDataHandler(parser, charHandler);
00095         nodotries = &t;
00096         FILE *fh = fopen(na, "r");
00097         if (fh == NULL) {
00098                 return;
00099         }
00100         do {
00101                 unsigned int len = (int)fread(buf, 1, sizeof(buf), fh);
00102                 done = len < sizeof(buf);
00103                 if (XML_Parse(parser, buf, len, done) == XML_STATUS_ERROR) {
00104                         fprintf(stderr,
00105                                 "%s at line %" XML_FMT_INT_MOD "u\n",
00106                                 XML_ErrorString(XML_GetErrorCode(parser)),
00107                                 XML_GetCurrentLineNumber(parser));
00108                         return;
00109                 }
00110         } while (!done);
00111         XML_ParserFree(parser);
00112         fclose(fh);
00113 }

Generado el Wed Jan 6 06:58:22 2010 para Mt77 por  doxygen 1.5.4