Advertisement
Luis_Pedro

Untitled

Nov 8th, 2018
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.24 KB | None | 0 0
  1. void carregaConfiguracoes(void){
  2.    
  3.     FILE * fp;
  4.    
  5.     fp = fopen(FILE_CONFIG,"r");
  6.     config=(Configuracao*) malloc(sizeof(Configuracao));
  7.    
  8.     char buffer[100];
  9.    
  10.     int linha,i;
  11.     //int max_x, max_y,n_drones,n_armazens,freq,quant,unid_tempo;
  12.     //char prod[5][30];
  13.    
  14.    
  15.     if ( fp != NULL)
  16.     {
  17.         linha = 0;
  18.         int l=0;
  19.         while((fgets(buffer,MAX_BUFFER,fp)) != NULL ){
  20.             if ( linha == 0){
  21.                 char * token = ( char * ) malloc(MAX_BUFFER * sizeof(char));
  22.                 char * aux;
  23.                 strcpy(token,buffer);
  24.                 aux = strtok(token,",");
  25.                 config->maxX=atoi(aux);
  26.                 aux=strtok(NULL, "\n");
  27.                 config->maxY=atoi(aux);
  28.                
  29.                 printf("%d x %d y\n", config->maxX, config->maxY);
  30.                 free(token);
  31.             }
  32.            
  33.             if (linha == 1){
  34.                 printf("saltei linha nao sei o que faz\n");
  35.             }
  36.            
  37.             if(linha == 2){
  38.                 config->numero_drones=atoi(buffer);
  39.                 printf("%d drone no sistema\n",config->numero_drones);
  40.             }
  41.             if(linha == 3){
  42.                 char * token = ( char * ) malloc(MAX_BUFFER * sizeof(char));
  43.                 token = strtok(buffer, ",");
  44.                 config->frequencia_abastecimento_s= atoi(token);
  45.                 token = strtok(NULL, ",");
  46.                 config->frequencia_abastecimento_q= atoi(token);
  47.                 token = strtok(NULL,"\n");
  48.                 config->frequencia_abastecimento_t = atoi(token);
  49.                 printf("%d s %d q %d t\n", config->frequencia_abastecimento_s,config->frequencia_abastecimento_q,config->frequencia_abastecimento_t);
  50.                
  51.             }
  52.             if (linha==5){
  53.                 char * token = ( char * ) malloc(MAX_BUFFER * sizeof(char));
  54.                 token = strtok(buffer, ",");
  55.                 config->numero_armazens_sistema= atoi(token);
  56.                 printf("%d armazens no sistema\n",config->numero_armazens_sistema);
  57.                
  58.             }
  59.             if (linha>5){
  60.                 for (int i=0; i<config->numero_armazens_sistema;i++){
  61.                     struct armazem *novoArmazem=malloc(sizeof(struct armazem));
  62.                    
  63.                     char * token = ( char * ) malloc(MAX_BUFFER * sizeof(char));
  64.                    
  65.                    
  66.                     token = strtok(buffer, ":");
  67.                    
  68.                     strcpy(string[0],token);
  69.        
  70.                     token = strtok(NULL, ":");
  71.                     strcpy(string[1],token);
  72.                     token = strtok(NULL,"\n");
  73.                     strcpy(string[2],token);
  74.                    
  75.                     token = strtok(string[0], " ");//nome armazem
  76.                     strcpy(novoArmazem->nome, token);
  77.                     token = strtok(string[1], ",");
  78.                     novoArmazem->posX=atoi(token);
  79.                     token = strtok(NULL, " ");
  80.                     novoArmazem->posY=atoi(token);
  81.                     token =strtok(string[2], ", ");
  82.                     //printf("string--%s\n",token);
  83.                     int j=0;
  84.                     while(token !=NULL){
  85.                        
  86.                         struct produto *novoProduto=malloc(sizeof(struct produto));
  87.                         strcpy(novoProduto->nome, token);
  88.                         token = strtok(NULL, ", ");
  89.                         novoProduto->quantidade=atoi(token);
  90.                         token=strtok(NULL, ", ");
  91.                         //printf("%s \n", token);
  92.                         config->armazensSistema[i].produtos[j]=*novoProduto;
  93.                         printf("%s", config->armazensSistema[i].produtos[j].nome);
  94.                         j++;
  95.                     }
  96.                     config->armazensSistema[i]=*novoArmazem;
  97.                     printf("%s, %d, %d\n", config->armazensSistema[i].nome,config->armazensSistema[i].posX,config->armazensSistema[i].posY);
  98.                    
  99.                     fgets(buffer,MAX_BUFFER,fp); //passa linha
  100.                 }
  101.             }
  102.             linha++;
  103.         }
  104.        
  105.     }
  106.    
  107.     fclose(fp);
  108.    
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement