Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <ctype.h>
- #include <string.h>
- #define FICHERO "tcp_vegas.c"
- char* copia(char* dest,char*input){
- if (!*input){return NULL;}
- char *ref =dest;
- while (*input && *input!='\n'){
- *dest++=*input++;
- }
- *dest++=*input;//Copia el ultimo 0 o \n
- *dest=0;
- dest=ref;
- return ++input;
- }
- char *read_file(const char *filename)
- {
- //Abre ficheros
- unsigned char c=0;
- unsigned int size=0;
- char *fichero =NULL;
- char *ref=NULL;
- FILE *src;
- src=fopen(filename,"r");
- if (!src ){
- fprintf (stderr," A ) Fallo apertura fichero fuente %s !!!",filename);
- return NULL;
- }else printf ("OK apertura %s\n",filename);
- //Analisis longitud
- while (c!=(unsigned char)EOF){
- c=fgetc(src);
- // printf ("%c",c);
- size++;
- };
- //size-=1;
- rewind (src);
- fichero=calloc (size+1,(sizeof(char)));
- ref=fichero;
- if (!fichero){printf ("Error calloc");return NULL;}//ERROR LECTURA
- else {printf ("Read File calloc ok %p \n",(void*)fichero);}
- //Efectua la copia
- c=1;
- while (c!=(unsigned char)EOF){
- c=fgetc(src);
- // printf ("%c",(char)c);
- if (c >0 && c<128) { *fichero=c;}
- fichero++;
- };
- *fichero=0;
- fichero=ref;
- fclose (src);
- printf ("Fin apertura \n");
- return ref;
- }
- /* Indent the C-code at memory block <indent>. String <pad> represents
- * one block of indentation. Only opening curly braces '{' increase the
- * indentation level, and closing curly braces '}' decrease the indentation level.
- * Return the pointer to the code after modification.
- * Calling code is responsible of freeing only the memory block returned by
- * the function.
- */
- char *indent(char *input, const char *pad)
- {
- char cadena[100];
- char *output=NULL;
- char *pto=NULL;
- int nivel=0;//NIvel tabulacion segun { }
- int comentario=0;//Dentro de comentarios /* */
- int comentarioB=0;//Dentro de comentarios //
- int vacia=1;
- output=calloc (strlen (input)*2,sizeof (char)); //SOBREDIMENSIONADO X 2
- pto=output;
- if (!output){printf ("Problema calloc en indent \n"); return NULL;}
- else {printf ("Ok calloc indent \n");}
- printf ("input vale %p output vale %p \n",input,output );
- //Inicio bucle principal
- while (*(input=copia(&cadena[0],input))){//Lee una linea la copia en cadena
- //printf ("%s",cadena);
- //printf ("Test puntero input %p \n",input);
- //No se espacian comentarios
- if ( (strstr (cadena,"/*"))){comentario=1;}//Inicio comentario /* */
- if ( (strstr (cadena,"//"))){comentarioB=1;}//Inicio comentario tipo //
- //if ( (strstr (cadena,"*/"))){comentario=0;}
- //if ( (strstr (cadena,"{"))){nivel++;}
- if ( (strstr (cadena,"}"))){nivel--;}
- //Si la linea esta vacia no se toca
- for (int i=0;(i<100 || !cadena[i]);i++){
- if (cadena[i]!=' '){ vacia=1;}//No esta vacio
- else{vacia=0;}
- }
- //deteccion y eliminacion primer espacio en comentarios
- if (comentario && cadena[0]==' ' && vacia){
- memmove (&cadena[0],&cadena[1],strlen(cadena)+1);
- }
- //pto=output;
- if (!comentario && !comentarioB && vacia){
- for (int i=1;i<=nivel;i++){
- pto=memcpy(pto,pad,strlen(pad));
- pto+=strlen(pad);
- }
- }
- if ( (strstr (cadena,"*/"))){comentario=0;}//Fin comentario /* */
- comentarioB=0;//Fin de comentarios en linea hasta el fin de linea
- if ( (strstr (cadena,"{"))){nivel++;}//Inicio { tabulacion
- memcpy(pto,&cadena[0],strlen(cadena));//Copia hasta el final ....
- pto+=strlen(cadena);
- }
- memcpy(pto,&cadena[0],strlen(cadena));//Copia hasta el final ....
- pto+=strlen(cadena);
- //Fin bucle principal
- //printf ("Output value %p pto %p\n",output,pto);
- return output;
- }
- int main(){
- const char *pad={" "};//Patron sustitucion
- char *input=read_file(FICHERO);
- char *fichero=indent(input, pad);
- printf ("RESULTADO \n %s",fichero);
- if (input){free(input);}
- if (fichero){free(fichero);}
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement