I cannot make the following program call the MYSQL C API when called
from the function process_token and I dont know why. I checked the WEB
and I did found nothing related to this bug. I am using slack 10, on a
x86 box with gcc. Mysql ver. 4.0.21.
Thanks in advance. Leonardo.
/************************************************** ************************/
#include <stdio.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
#include <mysql.h>
/************************************************** ***
* Errores
************************************************** ***/
#define ERR_BAD_FILE -1
#define ERR_NO_MEM -2
#define ERR_DB -3
int bDebug = 0;
char *num;
char *type;
char *year;
char *estado;
char *asunto;
char *org;
char *fecha;
char *materia;
char *pub_cod;
char *cant_art;
xmlDoc *doc = NULL;
xmlNode *rnode = NULL;
xmlNode *tag = NULL;
MYSQL *searchdb;
MYSQL *actdb;
MYSQL_RES *searchres;
MYSQL_ROW searchrow;
/************************************************** ****
* substring: simplemente saca substrings de una cadena
* inicial, pero no reasigna memoria
************************************************** ****/
char *
substring (char *orig, char *dest, int offset, int lenght)
{
sprintf (dest, "%.*s", lenght, orig + offset);
return dest;
}
int
process_token (char *token)
{
int rc = 0;
int tlen = strlen(token);
xmlChar *query = malloc (400);
sprintf (query, " SELECT id FROM keys_id LIMIT 1");
if (bDebug)
printf ("query:\n%s\n\n", query);
rc = mysql_query(searchdb, query);
if (rc != 0)
{
fprintf (stderr, "No puedo buscar en la base de datos. Saliendo
\n"
"error %s\n", mysql_error (searchdb));
exit (ERR_DB);
}
searchres = mysql_use_result (searchdb);
searchrow = mysql_fetch_row (searchres);
if (searchrow[0])
{
xmlChar actquery[400];
memset (&actquery, 0, 400);
sprintf (actquery, "INSERT INTO keys_rel(doctype, emisor,
docnum, "
"emitted, docversion, key_id) VALUES('%s', '%s', "
"'%s', '%s','%d','%s');", type, org, num, fecha, 0,
searchrow[0]);
if (bDebug)
printf ("query de actualizacion: %s\n", actquery);
rc = mysql_real_query (actdb, actquery, strlen (actquery));
if (rc != 0)
{
fprintf (stderr, "No puedo actualizar la base de datos.
Saliendo.\n"
"query: %s\nerror:%s\n", actquery, mysql_error (actdb));
exit (ERR_DB);
}
if (bDebug)
printf ("se actualizo el descriptor con el número %s\n",
searchrow[0]);
}
mysql_free_result (searchres);
free (query);
return 0;
}
int
process_dsc (xmlNode * secnode)
{
xmlNode *tag = secnode->xmlChildrenNode;
while (tag != NULL)
{
if ((!xmlStrcmp (tag->name, (const xmlChar *) "DESCRIPT")))
{
xmlChar *token;
xmlChar *descr =
xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (bDebug)
printf ("%s\n", descr);
token = strtok (descr, "-");
while (token)
{
process_token (token);
token = strtok (descr, "-");
}
xmlFree (descr);
}
tag = tag->next;
}
xmlFree (tag);
return 0;
}
void my_setconn (void)
{
int rc = 0;
searchdb = mysql_init (NULL);
actdb = mysql_init (NULL);
searchdb = mysql_real_connect (searchdb, "localhost", "web",
NULL, "be", 0, NULL, 0);
if (!searchdb)
{
printf ("Error conectando a mysql. Saliendo.\n");
exit (ERR_DB);
}
else if (bDebug)
printf ("Estoy conectado a la primera base de datos.\n"
"el error dice %s\n", mysql_error (searchdb));
rc = mysql_select_db (searchdb, "be");
if (rc != 0)
{
printf ("Error conectando a la base de datos 'be'.
Saliendo.\n");
exit (ERR_DB);
}
actdb = mysql_real_connect (actdb, "localhost", "web",
NULL, "be", 0, NULL, 0);
if (!actdb)
{
printf ("Error conectando a mysql. Saliendo.\n");
exit (ERR_DB);
}
else if (bDebug)
printf ("Estoy conectado a la segunda base de datos.\n"
"el error dice %s\n", mysql_error (actdb));
rc = mysql_select_db (actdb, "be");
if (rc != 0)
{
printf ("Error conectando a la base de datos 'be'.
Saliendo.\n");
exit (ERR_DB);
}
}
/************************************************** ****
* Procesamos las gen
************************************************** ****/
int
process_gen (xmlNode * gennode)
{
xmlNode *tag = gennode->xmlChildrenNode;
while (tag != NULL)
{
if ((!xmlStrcmp (tag->name, (const xmlChar *) "OBS_GEN")))
{
xmlNode *obs = tag->xmlChildrenNode;
if ((!xmlStrcmp (obs->name, (const xmlChar *) "OBS")))
{
cant_art = malloc (7);
xmlChar *cont = xmlNodeListGetString (doc,
obs->xmlChildrenNode, 1);
xmlChar *temp = strstr (cont, "ACN");
if (temp)
{
sprintf (cant_art, "%.*s", 4, temp + 4);
if (bDebug)
printf ("La cantidad de artículos de la norma es %s\n",
cant_art);
}
xmlFree (temp);
xmlFree (cont);
}
xmlFree (obs);
}
tag = tag->next;
}
xmlFree (tag);
return 0;
}
int
process_vi (xmlNode * secnode)
{
return 0;
}
static inline int
process_co (xmlNode * secnode)
{
return 0;
}
int
process_fi (xmlNode * secnode)
{
return 0;
}
int
process_an (xmlNode * secnode)
{
return 0;
}
int
process_ar (xmlNode * secnode)
{
return 0;
}
int
process_re (xmlNode * secnode)
{
return 0;
}
int
main (int argc, char **argv)
{
/* asignamos varibles de trabajo */
char *ptype = NULL;
char *ptype_rg;
/* primero checkeamos que nos hayan pasado el archivo a procesar */
if (!argv[1])
{
printf ("Uso %s <<nombre de archivo hve>> <<debug>>\n\n",
argv[0]);
exit (0);
}
if (argv[2])
{
if (strcmp (argv[2], "debug") == 0)
bDebug = 1;
}
/*my_setconn ();*/
/* ahora parseamos el documento que tenemos como parámetro */
doc = xmlParseFile (argv[1]);
if (!doc)
{
printf ("No es posible parsear el documento %s, checkee y vuelva
"
"a intentar. Saliendo.\n\n", argv[1]);
exit (ERR_BAD_FILE);
}
if (bDebug)
printf ("se logró parsear el documento\n");
/* obtenemos el root node */
rnode = xmlDocGetRootElement (doc);
my_setconn();
if (rnode == NULL)
printf ("El documento esta vacío. Saliendo.\n\n");
else if (bDebug)
printf ("El nombre del root es %s\n", rnode->name);
/* ahora vamos por el primer field */
tag = rnode->xmlChildrenNode;
while (tag != NULL)
{
if (ptype && ptype_rg)
{
type = strdup ("REG");
xmlFree (ptype);
xmlFree (ptype_rg);
ptype = NULL;
ptype_rg = NULL;
}
if ((!xmlStrcmp (tag->name, (const xmlChar *) "FIELD")))
{
xmlChar *fieldname = xmlGetProp (tag, "NAME");
if ((!xmlStrcmp (fieldname, "STANDARD_NORMATIVO")))
{
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, xmlNodeListGetString (doc,
tag->xmlChildrenNode,
1));
}
if ((!xmlStrcmp (fieldname, "NUMERO")))
{
num = xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, num);
}
if ((!xmlStrcmp (fieldname, "ESTADO")))
{
estado = xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, estado);
}
if ((!xmlStrcmp (fieldname, "ASUNTO")))
{
asunto = xmlNodeListGetRawString (doc, tag->xmlChildrenNode,
0);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, asunto);
}
if ((!xmlStrcmp (fieldname, "TIPO_NORMA")))
{
ptype = xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, ptype);
}
if ((!xmlStrcmp (fieldname, "TIPO_RESOLUCION")))
{
ptype_rg = xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, ptype_rg);
}
if ((!xmlStrcmp (fieldname, "ORGANISMO_EMISOR")))
{
org = xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, org);
}
if ((!xmlStrcmp (fieldname, "FECHA")))
{
xmlChar *tmp =
xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (tmp)
{
/* alojamos espacio para un formato de fecha iso */
fecha = malloc (11);
if (!fecha)
{
perror ("imposible asignar memoria. Saliendo.\n");
exit (ERR_NO_MEM);
}
sprintf (fecha, "%.*s-%.*s-%.*s", 4, tmp, 2, tmp + 4, 2,
tmp + 6);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, fecha);
xmlFree (tmp);
}
}
if ((!xmlStrcmp (fieldname, "MAT")))
{
materia = xmlNodeListGetString (doc, tag->xmlChildrenNode, 1);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, materia);
}
if ((!xmlStrcmp (fieldname, "PUBLICACION_CODIFICADA")))
{
pub_cod = xmlNodeListGetRawString (doc, tag->xmlChildrenNode,
1);
if (bDebug)
printf ("se encontró el campo %s, con el valor %s\n",
fieldname, pub_cod);
}
if ((!xmlStrcmp (fieldname, "TEXTO")))
{
xmlNode *seccion = tag->xmlChildrenNode;
while (seccion != NULL)
{
if ((!xmlStrcmp (seccion->name, "S")))
{
xmlChar *tipo_seccion = xmlGetProp (seccion, "T");
if ((!xmlStrcmp (tipo_seccion, "GEN")))
process_gen (seccion);
else if ((!xmlStrcmp (tipo_seccion, "DSC")))
process_dsc (seccion);
else if ((!xmlStrcmp (tipo_seccion, "VI")))
process_vi (seccion);
else if ((!xmlStrcmp (tipo_seccion, "CO")))
process_co (seccion);
else if ((!xmlStrcmp (tipo_seccion, "RE")))
process_re (seccion);
else if ((!xmlStrcmp (tipo_seccion, "FI")))
process_fi (seccion);
else if ((xmlStrstr (tipo_seccion, "AR")))
process_ar (seccion);
else if ((xmlStrstr (tipo_seccion, "AN")))
process_an (seccion);
}
seccion = seccion->next;
}
if (bDebug)
printf ("se encontró el campo texto\n\n");
}
xmlFree (fieldname);
xmlFree (fecha);
}
tag = tag->next;
}
xmlFreeDoc (doc);
xmlCleanupParser ();
return (0);
}