void read_xml_file( char *fname ) { char tag[MaxStr], contents[MaxStr], tagname[MaxStr], attrname[MaxStr], value[MaxStr]; float x1, y1, z1, x2, y2, z2, t0, t1; int linum=0; FILE *infile=0, *outfile=0; infile = fopen(fname,"r"); if (infile==0) {printf("Error: Cannot open input file '%s'.\n",fname); exit(1);} xml_parse( infile, tag, contents, MaxStr, &linum ); while (tag[0]!='\0') { xml_grab_tag_name( tag, tagname, MaxStr ); /* Get tag name. */ /* Add your application code here to accept tag-name, such as: */ printf("Tag name = '%s'\n", tagname ); xml_grab_attrib( tag, attrname, value, MaxStr ); /* Get any attributes within tag. */ while (value[0] != '\0') { /* Add application code here to accept attribute attrname and value, such as: */ printf(" Attribute: %s = '%s'\n", attrname, value ); xml_grab_attrib( tag, attrname, value, MaxStr ); /* Get next attribute, if any. */ } /* Add application code here to accept contents between tags, such as: */ printf(" Contents = '%s'\n", contents ); xml_parse( infile, tag, contents, MaxStr, &linum ); /* Get next tag, if any. */ } fclose(infile); }
Xml_object *Xml_Read_File( char *fname ) { FILE *infile; int lnn=0; char *tag, *attrib, *value, *contents; Xml_object *newtag, *roottag=0; Xml_name_value_pair *newattrib, *lastattrib; lastattrib = NULL ; struct xml_private_stack *current_parent, *old_tag; /* Current_parent points to parent of current children. */ infile = fopen(fname,"r"); if (infile==0) {printf("XML Error: Cannot open input file '%s'.\n",fname); return 0;} current_parent = new_xml_stack_item(0,0); /* Set the root tag level. */ tag = (char *)malloc(XML_MAX_STRLEN); attrib = (char *)malloc(XML_MAX_STRLEN); value = (char *)malloc(XML_MAX_STRLEN); contents = (char *)malloc(XML_MAX_STRLEN); xml_parse( infile, tag, contents, XML_MAX_STRLEN, &lnn ); while (!feof(infile)) { /*next_tag*/ xml_grab_tag_name( tag, attrib, XML_MAX_STRLEN ); if (attrib[0]=='/') /* If tag begins with "/", then go-up (pop-stack after comparing tag). */ { /*pop-stack*/ if ((current_parent->current_object==0) || (strcasecmp(current_parent->current_object->tag,&(attrib[1]))!=0)) {printf("Xml Error: Mismatching closing tag '%s'. Aborting.\n",attrib); free(tag); free(attrib); free(value); free(contents); return 0;} old_tag = current_parent; current_parent = current_parent->parent; xml_private_free_stack_item( old_tag ); if (current_parent==0) {printf("Xml Error: extra closing tag '%s'. Aborting.\n",attrib); free(tag); free(attrib); free(value); free(contents); return 0;} } /*pop-stack*/ else { /*Open-tag*/ newtag = new_xml_object( attrib, contents ); if (roottag == 0) roottag = newtag; else { if (current_parent->last_child == 0) current_parent->current_object->children = newtag; else current_parent->last_child->nxt = newtag; newtag->parent = current_parent->current_object; } current_parent->last_child = newtag; xml_grab_attrib( tag, attrib, value, XML_MAX_STRLEN ); /* Accept the attributes within tag. */ while ((attrib[0]!='\0') && (attrib[0]!='/')) { newattrib = new_xml_attribute( attrib, value ); if (newtag->attributes==0) newtag->attributes = newattrib; else lastattrib->nxt = newattrib; lastattrib = newattrib; xml_grab_attrib( tag, attrib, value, XML_MAX_STRLEN ); } /* If tag does not end in "/", then go-down (push-stack). IE. Next tag should be a child of present tag. */ if (attrib[0] != '/') { current_parent = new_xml_stack_item( current_parent, newtag ); } /* Otherwise, attaches to last child of present parent. */ } /*Open-tag*/ xml_parse( infile, tag, contents, XML_MAX_STRLEN, &lnn ); } /*next_tag*/ fclose(infile); while (xml_stack_freelist != 0) /* Cleanup temporary variables. */ { current_parent = xml_stack_freelist; xml_stack_freelist = xml_stack_freelist->parent; free(current_parent); } free(tag); free(attrib); free(value); free(contents); return roottag; }
void carregarAutomatosJFLAP(ListaAutomatos **listaAutomatos) { ListaAutomatos *lAux; lAux = *listaAutomatos; while(lAux!=NULL) { printf("\n\n Arquivo do automato : %s ",lAux->automato->XML_File_Name); int lendoEstados = 0; int estadoNum = 0; int estadoInicial= 0; int estadoFinal= 0; int lendoTransicoes = 0; int from = 0; int fromNum = 0; int to = 0; int toNum = 0; int read = 0; char readTerminal[40] = "\0"; ListaEstados *listaEstados; inicializaLista(&listaEstados); char *fname = lAux->automato->XML_File_Name; //strcpy(fname,); char tag[MaxStr], contents[MaxStr], tagname[MaxStr], attrname[MaxStr], value[MaxStr]; float x1, y1, z1, x2, y2, z2, t0, t1; int linum=0; FILE *infile=0, *outfile=0; infile = fopen(fname,"r"); if (infile==0) {printf("Error: Cannot open input file '%s'.\n",fname); exit(1);} xml_parse( infile, tag, contents, MaxStr, &linum ); while (tag[0]!='\0') { xml_grab_tag_name( tag, tagname, MaxStr ); /* Get tag name. */ /* Add your application code here to accept tag-name, such as: */ //printf("Tag name = '%s'\n", tagname ); if(strcmp(tagname,"state")==0) { lendoEstados = 1; //printf("Tag name comparaao = '%s'\n", tagname ); } if((lendoEstados==1)&&(strcmp(tagname,"initial")==0)) { estadoInicial = 1; //printf("Tag name comparaao = '%s'\n", tagname ); } if((lendoEstados==1)&&(strcmp(tagname,"final")==0)) { estadoFinal = 1; //printf("Tag name comparaao = '%s'\n", tagname ); } if((lendoEstados==1)&&(strcmp(tagname,"/state")==0)) { Estado *novoEstado; inicializarEstado(&novoEstado,estadoNum,estadoFinal,estadoInicial); adicionarListaEstados(novoEstado,&listaEstados); //printf("\n Criou estado %d, estado inicial %d, estado final %d",novoEstado->estado,novoEstado->estadoInicial,novoEstado->estadoFinal); lendoEstados = 0; estadoNum = 0; estadoInicial = 0; estadoFinal = 0; //printf("Tag name comparaao = '%s'\n", tagname ); } if(strcmp(tagname,"transition")==0) { lendoTransicoes = 1; //printf("Tag name comparaao = '%s'\n", tagname ); } if((lendoTransicoes==1)&&(strcmp(tagname,"from")==0)) { from = 1; } if((lendoTransicoes==1)&&(strcmp(tagname,"/from")==0)) { from = 0; } if((lendoTransicoes==1)&&(strcmp(tagname,"to")==0)) { to = 1; } if((lendoTransicoes==1)&&(strcmp(tagname,"/to")==0)) { to = 0; } if((lendoTransicoes==1)&&(strcmp(tagname,"read")==0)) { read = 1; } if((lendoTransicoes==1)&&(strcmp(tagname,"/read")==0)) { read = 0; } if((lendoTransicoes==1)&&(strcmp(tagname,"/transition")==0)) { lendoTransicoes = 0; Estado *estadoFrom = buscarEstadoPorNumero(listaEstados,fromNum); Estado *estadoTo = buscarEstadoPorNumero(listaEstados,toNum); if(readTerminal[0]=='"') { //Cria uma transicao Transicao* transicao = (Transicao *) malloc (sizeof(Transicao)); transicao->proximoEstado = estadoTo; int i = 0; char terminalSemAspas[40] = "\0"; for(i= 1;i<strlen(readTerminal)-1;i++) { terminalSemAspas[i-1] = readTerminal[i]; } strcpy(transicao->terminal,terminalSemAspas); ListaTransicao *listaTransicao = estadoFrom->listaTransicao; adicionarListaTransicao(transicao,&listaTransicao); estadoFrom->listaTransicao = listaTransicao; //printf("\n Criou transicao from %d to %d com o terminal %s ",estadoFrom->estado,estadoTo->estado,transicao->terminal); } else { ChamadaSubMaquina* chamadaSubMaquina = (ChamadaSubMaquina *) malloc (sizeof(ChamadaSubMaquina)); Automato *automatoAux = buscarAutomatoPorID(*listaAutomatos,readTerminal); if(automatoAux!=NULL) { chamadaSubMaquina->proxAutomato = automatoAux; } chamadaSubMaquina ->estadoRetorno = estadoTo; estadoFrom->chamadaSubMaquina = chamadaSubMaquina; //printf("\n Criou chamada submaquina from %d para o automato %s com retorno em %d",estadoFrom->estado,automatoAux->ID,estadoTo->estado); //Cria uma chamada de submaquina } char readTerminal[40] = "\0"; //printf("Tag name comparaao = '%s'\n", tagname ); } xml_grab_attrib( tag, attrname, value, MaxStr ); /* Get any attributes within tag. */ while (value[0] != '\0') { /* Add application code here to accept attribute attrname and value, such as: */ //printf(" Attribute: %s = '%s'\n", attrname, value ); if((lendoEstados==1) && (strcmp(attrname,"id")==0)) { estadoNum = atoi(value); } xml_grab_attrib( tag, attrname, value, MaxStr ); /* Get next attribute, if any. */ } /* Add application code here to accept contents between tags, such as: */ //printf("\n Contents = '%s'\n", contents ); if(from == 1) { fromNum = atoi(contents); } if(to == 1) { toNum = atoi(contents); } if(read==1) { strcpy(readTerminal,contents); } xml_parse( infile, tag, contents, MaxStr, &linum ); } fclose(infile); Automato *automatoAux = buscarAutomatoPorID(*listaAutomatos,lAux->automato->ID); automatoAux->listaEstados = listaEstados; lAux = lAux->prox; } }