X-Git-Url: https://secure.softndesign.org/git/?a=blobdiff_plain;f=indent.c;h=26b19288163877e594201fe048c3e0d0273f2fcb;hb=refs%2Fheads%2Fmaster;hp=145661ba7950afed98f7a0c292e6e9f04ee87ffc;hpb=943fa96eea0aac7767c54c217f587c07f4ae8181;p=indent.git diff --git a/indent.c b/indent.c index 145661b..26b1928 100644 --- a/indent.c +++ b/indent.c @@ -3,6 +3,7 @@ /* linker: debug.o */ #include +#include #include #include #include @@ -19,7 +20,7 @@ //#define BUFFERSIZE 4096 #define BUFFERSIZE 256 -#define TABSIZE 4 +#define TABSIZE 2 /* type definition */ @@ -54,8 +55,19 @@ int indent (FILE *fin, FILE *fout, cmode_t cmode) { char bufin[BUFFERSIZE + 1] = {0}; char bufout[BUFFERSIZE * TABSIZE + 1] = {0}; size_t i, nb; + size_t nbindent = 0; + int begin = 1; + int parent = 0; + int comment = 0; + int newline = 0; + int string = 0; + int character = 0; + int special = 0; + int space = 0; while (!feof (fin)) { + memset (bufin, 0, sizeof (bufin)); + memset (bufout, 0, sizeof (bufout)); /* read file */ nb = fread (bufin, 1, BUFFERSIZE, fin); @@ -69,21 +81,100 @@ int indent (FILE *fin, FILE *fout, cmode_t cmode) { char *ptin = bufin; char *ptout = bufout; while (*ptin != '\0') { + VERBOSE (DEBUG, fprintf (stdout, "caracter: %c\n", *ptin)); + + /* manage comment */ + if (comment > 0) { + if (((comment == 1) && (*ptin == '\n')) || + ((comment == 2) && ((*ptin == '*') && (ptin[1] == '/')))) { + comment = 0; + } + special = 0; + *ptout++ = *ptin++; + continue; + } + + /* manage indent */ switch (*ptin) { + case '/': + comment = (ptin[1] == '/') ? 1 : (ptin[1] == '*') ? 2 : 0; + if (begin) { + for (i = 0; i < nbindent * TABSIZE; i++) { + *ptout++ = ' '; + } + begin = 0; + } + *ptout++ = *ptin; + break; + case ' ': case '\t': - for (i = 0; i < TABSIZE; i++) { - *ptout++ = ' '; + if (begin == 0) { + if ((string) || (!space)) { + *ptout++ = *ptin; + } } break; case '{': + *ptout++ = '\n'; + for (i = 0; i < nbindent * TABSIZE; i++) { + *ptout++ = ' '; + } + *ptout++ = *ptin; + *ptout++ = '\n'; + nbindent++; + newline = 1; + begin = 1; + break; case '}': + *ptout++ = '\n'; + nbindent--; + for (i = 0; i < nbindent * TABSIZE; i++) { + *ptout++ = ' '; + } + *ptout++ = *ptin; + if (ptin[1] != ';') { + *ptout++ = '\n'; + } + newline = 1; + begin = 1; + break; case ';': *ptout++ = *ptin; + if (parent) { + break; + } *ptout++ = '\n'; + newline = 1; + begin = 1; + break; + case '\n': + if (newline == 1) { + newline = 0; + } else { + *ptout++ = '\n'; + } + begin = 1; + break; + case '\r': break; default: + if ((*ptin == '"') && (!character) && (!special)) { + string ^= 1; + } + if ((*ptin == '\'') && (!string) && (!special)) { + character ^= 1; + } + if (begin) { + for (i = 0; i < nbindent * TABSIZE; i++) { + *ptout++ = ' '; + } + begin = 0; + } *ptout++ = *ptin; } + space = ((*ptin == ' ') || (*ptin == '\t')); + special = (*ptin == '\\'); + parent += (*ptin == '(') ? +1 : (*ptin == ')') ? -1 : 0; ptin++; } ptout = '\0'; @@ -91,7 +182,8 @@ int indent (FILE *fin, FILE *fout, cmode_t cmode) { /* write file */ VERBOSE (DEBUG, fprintf (stdout, "buffer out: %d\n", strlen (bufout))); ptout = bufout; - while ((nb = fread (ptout, 1, strlen (ptout), fout)) != strlen (ptout)) { + while ((nb = fwrite (ptout, 1, strlen (ptout), fout)) != strlen (ptout)) { + VERBOSE (DEBUG, fprintf (stdout, "buffer out: %d/%d\n", nb, strlen (ptout))); if (errno != 0) { VERBOSE (ERROR, fprintf (stderr, "can't write file (%d)\n", errno)); exit (1); @@ -164,7 +256,7 @@ int main (int argc, char *argv[]) /* check output */ FILE *fout = NULL; if (output) { - fout = fopen (input, "wb"); + fout = fopen (output, "wb"); if (!fout) { VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", output)); fclose (fin);