Advertisement
AntonioVillanueva

Gestion tabulaciones y comentarios

Aug 3rd, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.29 KB | None | 0 0
  1. #include <stdlib.h>
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <string.h>
  6. #define FICHERO "tcp_vegas.c"
  7.  
  8. char* copia(char* dest,char*input){
  9.     if (!*input){return NULL;}
  10.    
  11.     char *ref =dest;
  12.    
  13.     while (*input && *input!='\n'){
  14.         *dest++=*input++;
  15.  
  16.     }
  17.     *dest++=*input;//Copia el ultimo 0 o \n
  18.     *dest=0;
  19.    
  20.  
  21.     dest=ref;
  22.     return ++input;
  23. }
  24.  
  25.  char *read_file(const char *filename)
  26. {
  27.  
  28.    //Abre ficheros
  29.     unsigned char c=0;
  30.     unsigned int size=0;
  31.     char *fichero =NULL;
  32.     char *ref=NULL;
  33.     FILE *src;
  34.     src=fopen(filename,"r");
  35.  
  36.   if (!src ){
  37.         fprintf (stderr," A ) Fallo apertura fichero fuente %s !!!",filename);
  38.         return NULL;
  39.     }else printf ("OK apertura %s\n",filename);
  40.  
  41.     //Analisis longitud
  42.      while (c!=(unsigned char)EOF){
  43.         c=fgetc(src);
  44.        // printf ("%c",c);
  45.         size++;
  46.     };
  47.     //size-=1;
  48.     rewind (src);
  49.     fichero=calloc (size+1,(sizeof(char)));
  50.     ref=fichero;
  51.  
  52.     if (!fichero){printf ("Error calloc");return NULL;}//ERROR LECTURA
  53.     else {printf ("Read File calloc ok %p \n",(void*)fichero);}
  54.     //Efectua la copia
  55.     c=1;
  56.     while (c!=(unsigned char)EOF){
  57.  
  58.         c=fgetc(src);
  59.        // printf ("%c",(char)c);
  60.  
  61.      if (c >0 && c<128) {  *fichero=c;}
  62.        fichero++;
  63.  
  64.     };
  65.     *fichero=0;
  66.  
  67.     fichero=ref;
  68.     fclose (src);
  69.  
  70.    printf ("Fin apertura \n");
  71.     return ref;
  72. }
  73.  
  74.  
  75. /* Indent the C-code at memory block <indent>. String <pad> represents
  76.  * one block of indentation. Only opening curly braces '{' increase the
  77.  * indentation level, and closing curly braces '}' decrease the indentation level.
  78.  * Return the pointer to the code after modification.
  79.  * Calling code is responsible of freeing only the memory block returned by
  80.  * the function.
  81.  */
  82. char *indent(char *input, const char *pad)
  83. {
  84.  
  85.     char cadena[100];
  86.     char *output=NULL;
  87.     char *pto=NULL;
  88.     int nivel=0;//NIvel tabulacion segun { }
  89.     int comentario=0;//Dentro de comentarios /* */
  90.     int comentarioB=0;//Dentro de comentarios //
  91.     int vacia=1;
  92.  
  93.    output=calloc (strlen (input)*2,sizeof (char)); //SOBREDIMENSIONADO X 2
  94.    pto=output;
  95.    
  96.    if (!output){printf ("Problema calloc en indent \n"); return NULL;}
  97.    else {printf ("Ok calloc indent \n");}
  98.         printf ("input vale %p output vale %p \n",input,output );
  99.     //Inicio bucle principal
  100.    
  101.    while (*(input=copia(&cadena[0],input))){//Lee una linea la copia en cadena
  102.         //printf ("%s",cadena);
  103.         //printf ("Test puntero input %p \n",input);
  104.  
  105.         //No se espacian comentarios
  106.         if ( (strstr (cadena,"/*"))){comentario=1;}//Inicio comentario /* */
  107.         if ( (strstr (cadena,"//"))){comentarioB=1;}//Inicio comentario tipo //
  108.         //if ( (strstr (cadena,"*/"))){comentario=0;}
  109.  
  110.         //if ( (strstr (cadena,"{"))){nivel++;}
  111.         if ( (strstr (cadena,"}"))){nivel--;}
  112.  
  113.         //Si la linea esta vacia no se toca
  114.         for (int i=0;(i<100 || !cadena[i]);i++){
  115.             if (cadena[i]!=' '){ vacia=1;}//No esta vacio
  116.             else{vacia=0;}
  117.         }
  118.  
  119.  
  120.         //deteccion y eliminacion primer espacio en comentarios
  121.         if (comentario && cadena[0]==' ' && vacia){
  122.             memmove (&cadena[0],&cadena[1],strlen(cadena)+1);
  123.         }
  124.  
  125.         //pto=output;
  126.  
  127.         if (!comentario && !comentarioB && vacia){
  128.             for (int i=1;i<=nivel;i++){
  129.                 pto=memcpy(pto,pad,strlen(pad));
  130.                 pto+=strlen(pad);
  131.             }
  132.         }
  133.  
  134.         if ( (strstr (cadena,"*/"))){comentario=0;}//Fin comentario /* */
  135.         comentarioB=0;//Fin de comentarios en linea hasta el fin de linea
  136.  
  137.         if ( (strstr (cadena,"{"))){nivel++;}//Inicio { tabulacion
  138.  
  139.  
  140.        
  141.         memcpy(pto,&cadena[0],strlen(cadena));//Copia hasta el final ....
  142.         pto+=strlen(cadena);
  143.  
  144.     }
  145.    
  146.         memcpy(pto,&cadena[0],strlen(cadena));//Copia hasta el final ....
  147.         pto+=strlen(cadena);
  148.    
  149.     //Fin bucle principal
  150.  
  151.     //printf ("Output  value %p  pto %p\n",output,pto);
  152.  
  153.  
  154.    return output;
  155. }
  156.  
  157. int main(){
  158.  
  159.     const char *pad={"    "};//Patron sustitucion
  160.     char *input=read_file(FICHERO);
  161.     char *fichero=indent(input, pad);
  162.  
  163.  
  164.    printf ("RESULTADO \n %s",fichero);
  165.  
  166.  
  167.  
  168.     if (input){free(input);}
  169.     if (fichero){free(fichero);}
  170.  
  171.     return 0;
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement