partial indent
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 12 Dec 2023 08:46:03 +0000 (09:46 +0100)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 12 Dec 2023 08:46:03 +0000 (09:46 +0100)
debug.h
indent.c

diff --git a/debug.h b/debug.h
index 32dc0cae3b7ce62d18b13728a8418401ebeeaa19..fb1c01227312e78b01b480bfff1cb78a33bf744d 100644 (file)
--- a/debug.h
+++ b/debug.h
@@ -10,7 +10,7 @@
 
 /* macros */
 
-#define VERBOSE(level, statement...) do { if (level <= verbose) { statement; } } while(0)
+#define VERBOSE(level, statement...) do { if (level <= verbose) { statement; fflush (stdout); fflush (stderr); } } while(0)
 
 /* gobal variables */
 
index 145661ba7950afed98f7a0c292e6e9f04ee87ffc..a37dad77691017571e161d42ddd78d38ad9a029c 100644 (file)
--- a/indent.c
+++ b/indent.c
@@ -19,7 +19,7 @@
 
 //#define BUFFERSIZE 4096
 #define BUFFERSIZE 256
-#define TABSIZE 4
+#define TABSIZE 2
 
 /* type definition */
 
@@ -54,8 +54,18 @@ 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;
 
     while (!feof (fin)) {
+        memset (bufin, 0, sizeof (bufin));
+        memset (bufout, 0, sizeof (bufout));
 
         /* read file */
         nb = fread (bufin, 1, BUFFERSIZE, fin);
@@ -69,21 +79,97 @@ 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) {
+                        *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;
             }
+            special = (*ptin == '\\');
+            parent += (*ptin == '(') ? +1 : (*ptin == ')') ? -1 : 0;
             ptin++;
         }
         ptout = '\0';
@@ -91,7 +177,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 +251,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);