int main (void) { int atual; /* libcgi declarations */ /* libxml2 declarations */ xmlChar *strUTF = NULL; xmlDocPtr doc; xmlNodePtr root_element, cur_node, new_form, new_node, edited_patient, old_patient; char buf[5]; /* * this initialize the library and check potential ABI mismatches * between the version it was compiled for and the actual shared * library used. */ LIBXML_TEST_VERSION /****************************************************************************** * READ CONTENT STRING FROM SERVER. * * PROCESS DATA. * * CREATE A LINKED LIST. * ******************************************************************************/ cgi_init(); /****************************************************************************** * OPENING AND PARSING AN XML FILE TO A TREE * ******************************************************************************/ doc = xmlReadFile(XML_FILE_PATH, NULL, XML_PARSE_NOBLANKS); if (doc == NULL) { printError("Failed to parse doc"); usualFreeMemory(NULL); exit(0); } root_element = xmlDocGetRootElement(doc); cur_node = root_element->children; /****************************************************************************** * SEARCH THE SAME <numeroGeral> * ******************************************************************************/ atual = 1; while(cur_node != NULL ) { new_form = cur_node; new_node = xmlNewNode (NULL, "id"); sprintf(buf, "%d", atual); xmlNodeAddContent (new_node, buf); xmlAddChild (new_form, new_node); atual++; cur_node = cur_node->next; } /****************************************************************************** * DUMPING DOCUMENT TO FILE * ******************************************************************************/ xmlSaveFormatFileEnc(XML_TEMP_FILE, doc, "UTF-8", 1); remove(XML_FILE_PATH); if (rename(XML_TEMP_FILE, XML_FILE_PATH)) { printError("Erro ao renomear o arquivo atualizado"); usualFreeMemory(doc); exit(0); } /****************************************************************************** * FREE MEMORY AND EXIT * ******************************************************************************/ printSuccess(buf); usualFreeMemory(doc); return 0; }
int main (void) { char *formName, *pid, *username, *newPid; boolean found_patient; FILE *document; /* libcgi declarations */ formvars *first_input, *input; /* libxml2 declarations */ xmlChar *strUTF = NULL; xmlDocPtr doc; xmlNodePtr root_element, cur_node, new_form, new_node, edited_patient, old_patient; /* * this initialize the library and check potential ABI mismatches * between the version it was compiled for and the actual shared * library used. */ LIBXML_TEST_VERSION /****************************************************************************** * READ CONTENT STRING FROM SERVER. * * PROCESS DATA. * * CREATE A LINKED LIST. * ******************************************************************************/ cgi_init(); first_input = cgi_process_form(); newPid = cgi_param("numeroGeral"); pid = cgi_param("antigoNumeroGeral"); formName = cgi_param("form"); //username = cgi_param("uid"); if(!(username= getenv("REMOTE_USER"))) //verifica se string lida é null { printf("Content-type: text/html\n\n"); printf("<html>\n"); printf("<head>\n"); printf("<title>Resultado</title>\n"); printf("</head>\n"); printf("<body>\n"); printf("Erro ao verificar o usuário."); printf("</body>\n"); printf("</html>\n"); exit(0); } if ((!pid) || (!formName)) { printError("ID do paciente e nome do formulário não foram enviados"); usualFreeMemory(NULL); exit(0); } /****************************************************************************** * OPENING AND PARSING AN XML FILE TO A TREE * ******************************************************************************/ document = fopen(XML_FILE_PATH, "r+"); if (document == NULL) { printError("O arquivo de pacientes não pode ser aberto"); exit(0); } if(flock(fileno(document), LOCK_EX)) { printError("Erro ao trancar o arquivo"); fclose(document); exit(0); } //printWait("Trancado!"); //sleep(20); doc = xmlReadFd(fileno(document), XML_FILE_PATH, NULL, XML_PARSE_NOBLANKS); if (doc == NULL) { printError("Failed to parse doc"); usualFreeMemory(NULL); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } root_element = xmlDocGetRootElement(doc); /****************************************************************************** * SEARCH THE SAME <numeroGeral> * ******************************************************************************/ cur_node = root_element->children; /* Get the node <paciente> if file isn't empty */ if (!cur_node) //test!!!! printf!! pode-se tb na remocao exigir a remocao do arquivo se o paciente excluido for o unico do arquivo found_patient = false; else { /* looping through patients looking for the right patient */ for (found_patient = false; ((!found_patient) && (cur_node)); cur_node = cur_node->next) { cur_node = cur_node->children; /* <triagem> */ cur_node = cur_node->children; /* <numeroGeral> ? */ while ((!xmlStrEqual(cur_node->name, BAD_CAST "numeroGeral")) && (cur_node)) cur_node = cur_node->next; if (xmlStrEqual(cur_node->name, BAD_CAST "numeroGeral")) { if (xmlStrEqual(cur_node->children->content, BAD_CAST pid)) { found_patient = true; old_patient = cur_node->parent; /*old_paciente recebe o noh <triagem> do paciente que possui o numeroGeral procurado */ } else { cur_node = cur_node->parent; /* <triagem> */ cur_node = cur_node->parent; /* <paciente> */ } } else { printError("Erro de formação do XML. Tem paciente sem número geral."); usualFreeMemory(doc); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } } } /****************************************************************************** * CHECK IF PATIENT WAS FOUND * * IF TRUE, EXIT * ******************************************************************************/ if (!found_patient) { printError("Este paciente não existe."); usualFreeMemory(doc); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } /****************************************************************************** * Verificando se o novo numero geral jah existe * *******************************************************************************/ cur_node = root_element->children; /* Get the node <paciente> if file isn't empty */ if (!cur_node) //test!!!! printf!! pode-se tb na remocao exigir a remocao do arquivo se o paciente excluido for o unico do arquivo found_patient = false; else { /* looping through patients looking for the right patient */ for (found_patient = false; ((!found_patient) && (cur_node)); cur_node = cur_node->next) { cur_node = cur_node->children; /* <triagem> */ cur_node = cur_node->children; /* <numeroGeral> ? */ while ((!xmlStrEqual(cur_node->name, BAD_CAST "numeroGeral")) && (cur_node)) cur_node = cur_node->next; if (xmlStrEqual(cur_node->name, BAD_CAST "numeroGeral")) { if (xmlStrEqual(cur_node->children->content, BAD_CAST newPid)) { found_patient = true; old_patient = cur_node->parent; /*old_paciente recebe o noh <triagem> do paciente que possui o numeroGeral procurado */ } else { cur_node = cur_node->parent; /* <triagem> */ cur_node = cur_node->parent; /* <paciente> */ } } else { printError("Erro de formação do XML. Tem paciente sem número geral."); usualFreeMemory(doc); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } } } /****************************************************************************** * Comfirmando se novo numero geral jah existe * *******************************************************************************/ if((found_patient) && (strcmp(newPid,pid) != 0)) { printError("O novo número geral escolhido já existe."); usualFreeMemory(doc); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } /****************************************************************************** * CRIANDO NOVO FORMULARIO DE TRIAGEM * ******************************************************************************/ edited_patient = xmlNewNode (NULL,BAD_CAST "triagem"); /****************************************************************************** * ADD NEW FORM * ******************************************************************************/ strUTF = fixCgiStr(BAD_CAST formName); if (!strUTF) { cgi_init_headers(); printError("Erro na conversão de formName para UTF-8"); usualFreeMemory(doc); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } new_form = xmlNewChild (edited_patient, NULL, BAD_CAST strUTF, NULL); free(strUTF);//frees formName for (input = first_input; input; input = input->next) { if (!strcmp(input->name,"form")) input = input->next; if (!strcmp(input->name,"uid")) input = input->next; if (!strcmp(input->name,"antigoNumeroGeral")) input = input->next; /* Validate tag name input against UTF-8 */ strUTF = fixCgiStr((unsigned char *)input->name); if (!strUTF) { printError("Erro na conversão de input->name para UTF-8"); usualFreeMemory(doc); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } new_node = xmlNewNode (NULL, strUTF); free(strUTF);//frees input->name /* Validate tag value input against UTF-8 */ strUTF = fixCgiStr((unsigned char *)input->value); if (!strUTF) { printError("Erro na conversão de input->value para UTF-8"); usualFreeMemory(doc); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } xmlNodeAddContent (new_node, strUTF); free(strUTF);//frees input->value xmlAddChild (new_form, new_node); } /****************************************************************************** * Substituindo o noh antigo pelo novo * ******************************************************************************/ xmlReplaceNode (old_patient, edited_patient->children); xmlFreeNode (old_patient); /****************************************************************************** * DUMPING DOCUMENT TO FILE * ******************************************************************************/ if ((xmlSaveFormatFileEnc(XML_FILE_PATH, doc, "ISO-8859-1", 1)) < 0) { printError("Erro ao salvar arquivo"); usualFreeMemory(doc); xmlFreeNode(edited_patient); flock(fileno(document), LOCK_EX); fclose(document); exit(0); } /*remove(XML_FILE_PATH); if (rename(XML_TEMP_FILE, XML_FILE_PATH)) { printError("Erro ao renomear o arquivo atualizado"); usualFreeMemory(doc); exit(0); }*/ /****************************************************************************** * FREE MEMORY AND EXIT * ******************************************************************************/ printSuccess(username); usualFreeMemory(doc); xmlFreeNode(edited_patient); flock(fileno(document), LOCK_EX); fclose(document); autoBackup(); return 0; }
int main (void) { char *formName, *pid, *username, *paciente; boolean found_patient; int atual, paciente_id; /* libcgi declarations */ formvars *first_input, *input; /* libxml2 declarations */ xmlChar *strUTF = NULL; xmlDocPtr doc; xmlNodePtr root_element, cur_node, new_form, new_node, edited_patient, old_patient; /* * this initialize the library and check potential ABI mismatches * between the version it was compiled for and the actual shared * library used. */ LIBXML_TEST_VERSION /****************************************************************************** * READ CONTENT STRING FROM SERVER. * * PROCESS DATA. * * CREATE A LINKED LIST. * ******************************************************************************/ cgi_init(); first_input = cgi_process_form(); pid = cgi_param("pid"); username = cgi_param("uid"); paciente = cgi_param("paciente"); paciente_id = atoi(paciente); /****************************************************************************** * OPENING AND PARSING AN XML FILE TO A TREE * ******************************************************************************/ doc = xmlReadFile(XML_FILE_PATH, NULL, XML_PARSE_NOBLANKS); if (doc == NULL) { printError("Failed to parse doc"); usualFreeMemory(NULL); exit(0); } root_element = xmlDocGetRootElement(doc); cur_node = root_element->children; /****************************************************************************** * SEARCH THE SAME <numeroGeral> * ******************************************************************************/ //atual = 1; atual = cur_node->getElementsByTagName("id"); while((cur_node != NULL) && (atual->value != paciente_id )) { cur_node = cur_node->next; //atual ++; atual = cur_node->getElementsByTagName("id"); } old_patient = cur_node; /****************************************************************************** * CHECK IF PATIENT WAS FOUND * * IF TRUE, EXIT * ******************************************************************************/ /****************************************************************************** * CRIANDO NOVO FORMULARIO DE TRIAGEM * ******************************************************************************/ edited_patient = xmlNewNode (NULL,BAD_CAST "paciente"); /****************************************************************************** * ADD NEW FORM * ******************************************************************************/ new_form = edited_patient; for (input = first_input; input; input = input->next) { if (!strcmp(input->name,"form")) input = input->next; if (!strcmp(input->name,"uid")) input = input->next; if (!strcmp(input->name,"paciente")) input = input->next; /* Validate tag name input against UTF-8 */ strUTF = input->name; //printf("%s : \n", strUTF); new_node = xmlNewNode (NULL, strUTF); free(strUTF);//frees input->name /* Validate tag value input against UTF-8 */ strUTF = input->value; //printf("%s <br>\n", strUTF); xmlNodeAddContent (new_node, strUTF); free(strUTF);//frees input->value xmlAddChild (new_form, new_node); } /*printf ("</span>"); printf ("\t</body>\n"); printf ("</html>"); return 0;*/ //printf ("</html>\n"); //printf ("\t</body>\n"); //usualFreeMemory(NULL); //exit(0); /****************************************************************************** * Substituindo o noh antigo pelo novo * ******************************************************************************/ xmlReplaceNode (old_patient, edited_patient); xmlFreeNode (old_patient); /****************************************************************************** * DUMPING DOCUMENT TO FILE * ******************************************************************************/ if ((xmlSaveFormatFileEnc(XML_TEMP_FILE, doc, "UTF-8", 1)) < 0) { remove(XML_TEMP_FILE); printError("Erro ao salvar arquivo"); usualFreeMemory(doc); exit(0); } remove(XML_FILE_PATH); if (rename(XML_TEMP_FILE, XML_FILE_PATH)) { printError("Erro ao renomear o arquivo atualizado"); usualFreeMemory(doc); exit(0); } /****************************************************************************** * FREE MEMORY AND EXIT * ******************************************************************************/ printSuccess(username); usualFreeMemory(doc); xmlFreeNode(edited_patient); return 0; }
int main (void) { /* general declarations */ char *pid, *formName; char xmlFilePath [FILE_NAME_MAX]; char tempFile[] = "xml/pacientesTemp.xml"; int rc, utf8; boolean found_patient, found_form; /* date and time declarations */ /*time_t rawtime; struct tm *timeinfo; int day, month, year, hour, min;*/ /* libcgi declarations */ formvars *first_input, *input; /* libxml2 declarations */ xmlChar *strUTF; xmlDocPtr doc; xmlNodePtr root_element, cur_node, new_form, new_node, right_patient; /* * this initialize the library and check potential ABI mismatches * between the version it was compiled for and the actual shared * library used. */ LIBXML_TEST_VERSION /****************************************************************************** * GET CURRENT DATE AND TIME * ******************************************************************************/ /* time(&rawtime); timeinfo = localtime(&rawtime); day = timeinfo->tm_mday; month = timeinfo->tm_mon +1; year = timeinfo->tm_year +1900; hour = timeinfo->tm_hour; min = timeinfo->tm_min; *//* date and time are being entered manually */ cgi_init(); /****************************************************************************** * READ CONTENT STRING FROM SERVER. * * PROCESS DATA. * * CREATE A LINKED LIST. * ******************************************************************************/ first_input = cgi_process_form(); formName = cgi_param("form"); pid = cgi_param("pid"); if ((!pid) || (!formName)) { printError("ID do paciente e nome do formulário não foram enviados"); usualFreeMemory(NULL); exit(0); } /****************************************************************************** * OPENING AND PARSING AN XML FILE TO A TREE * ******************************************************************************/ //strcpy (healthCenter, "Guadalupe"); //snprintf (xmlFilePath, FILE_NAME_MAX, "xml/pacientes%s.xml", healthCenter); strcpy (xmlFilePath, "xml/pacientesGuadalupe.xml"); doc = xmlReadFile(xmlFilePath, NULL, 256); /* 256 = remove blank nodes */ if (doc == NULL) { printError("Failed to parse doc"); usualFreeMemory(NULL); exit(0); } /****************************************************************************** * SEARCH PATIENT * ******************************************************************************/ root_element = xmlDocGetRootElement(doc); cur_node = root_element->children; /* Get the node <paciente> if file isn't empty */ if (!cur_node) //test!!!! printf!! pode-se tb na remocao exigir a remocao do arquivo se o paciente excluido for o unico do arquivo found_patient = false; else { /* looping through patients looking for the right patient */ for (found_patient = false; ((!found_patient) && (cur_node)); cur_node = cur_node->next) { cur_node = cur_node->children; /* <triagem> */ cur_node = cur_node->children; /* <numeroGeral> ? */ while ((!xmlStrEqual(cur_node->name, BAD_CAST "numeroGeral")) && (cur_node)) cur_node = cur_node->next; if (xmlStrEqual(cur_node->name, BAD_CAST "numeroGeral")) { if (xmlStrEqual(cur_node->children->content, BAD_CAST pid)) { //MANDAR IMPRIMIR o CONTEUDO VAZIO DE UM NUMERO GERAL PARA TESTE! found_patient = true; right_patient = cur_node->parent->parent; } else { cur_node = cur_node->parent; /* <triagem> */ cur_node = cur_node->parent; /* <paciente> */ } } else { printError("Erro de formação do XML. Tem paciente sem número geral."); usualFreeMemory(doc); exit(0); } } } /****************************************************************************** * CHECK IF PATIENT WAS FOUND * * IF TRUE, CONVERT FORM NAME INPUT TO UTF-8 IF NECESSARY * ******************************************************************************/ if (found_patient) { utf8 = xmlCheckUTF8 (BAD_CAST formName); if (!utf8) { rc = convertISO88591toUTF8 ((unsigned char *)formName, &strUTF); if (rc < 0) { printError("Erro convertendo valor para UTF-8"); usualFreeMemory(doc); free(strUTF); exit(0); } formName = (char *)strUTF; } /****************************************************************************** * CHECK IF FORM EXISTS * ******************************************************************************/ cur_node = right_patient->children; found_form = false; while ((cur_node) && (!found_form)) { if ((xmlStrEqual(BAD_CAST formName, cur_node->name)) && (strcmp(formName, "followUp"))) found_form = true; cur_node = cur_node->next; } if (found_form) { printError("O paciente já possui este formulário"); usualFreeMemory(doc); free(strUTF); exit(0); } /****************************************************************************** * ADD NEW FORM * ******************************************************************************/ new_form = xmlNewNode (NULL, BAD_CAST formName); free(strUTF);//frees formName for (input = first_input; input; input = input->next) { while ((!strcmp(input->name,"form")) || (!strcmp(input->name,"pid"))) input = input->next; /* Validate tag name input against UTF-8 */ utf8 = xmlCheckUTF8 (BAD_CAST input->name); if (!utf8) { rc = convertISO88591toUTF8 ((unsigned char *)input->name, &strUTF); /** the code above is equiv to the code below inlen = strlen(input->name); lenUTF = 2 * inlen; strUTF = BAD_CAST malloc ((size_t) (lenUTF + 1)); rc = isolat1ToUTF8 (strUTF, &lenUTF, input->name, &inlen); **/ if (rc < 0) { printError("Erro convertendo valor para UTF-8"); usualFreeMemory(doc); free(strUTF); exit(0); } new_node = xmlNewNode (NULL, strUTF); free(strUTF); } else new_node = xmlNewNode (NULL, BAD_CAST input->name); /* Validate tag value input against UTF-8 */ utf8 = xmlCheckUTF8 (BAD_CAST input->value); if (!utf8) { rc = convertISO88591toUTF8 ((unsigned char *)input->value, &strUTF); if (rc < 0) { printError("Erro convertendo valor para UTF-8"); usualFreeMemory(doc); free(strUTF); exit(0); } xmlNodeAddContent (new_node, strUTF); free(strUTF); } else xmlNodeAddContent (new_node, BAD_CAST input->value); xmlAddChild (new_form, new_node); } xmlAddChild (right_patient, new_form); /****************************************************************************** * DUMPING DOCUMENT TO FILE * ******************************************************************************/ if ((xmlSaveFormatFileEnc(tempFile, doc, "ISO-8859-1", 1)) < 0) { remove(tempFile); printError("Erro ao salvar arquivo"); usualFreeMemory(doc); exit(0); } remove(xmlFilePath); if (rename(tempFile, xmlFilePath)) { printError("Erro ao renomear o arquivo atualizado"); usualFreeMemory(doc); exit(0); } } else { printError("O paciente não tem registro no sistema"); usualFreeMemory(doc); exit(0); } /****************************************************************************** * FREE MEMORY AND EXIT * ******************************************************************************/ usualFreeMemory(doc); printError("Fomulário adicionado com sucesso"); return (0); }