//#define BUFFERSIZE 4096
#define BUFFERSIZE 256
-#define TABSIZE 4
+#define TABSIZE 2
/* type definition */
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);
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';
/* 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);
/* 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);