Soft'N'Design Software
/
projects
/
indent.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
partial indent
[indent.git]
/
indent.c
diff --git
a/indent.c
b/indent.c
index 145661ba7950afed98f7a0c292e6e9f04ee87ffc..a37dad77691017571e161d42ddd78d38ad9a029c 100644
(file)
--- a/
indent.c
+++ b/
indent.c
@@
-19,7
+19,7
@@
//#define BUFFERSIZE 4096
#define BUFFERSIZE 256
//#define BUFFERSIZE 4096
#define BUFFERSIZE 256
-#define TABSIZE
4
+#define TABSIZE
2
/* type definition */
/* 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;
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)) {
while (!feof (fin)) {
+ memset (bufin, 0, sizeof (bufin));
+ memset (bufout, 0, sizeof (bufout));
/* read file */
nb = fread (bufin, 1, BUFFERSIZE, fin);
/* 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') {
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) {
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':
case '\t':
-
for (i = 0; i < TABSIZE; i++
) {
- *ptout++ =
' '
;
+
if (begin == 0
) {
+ *ptout++ =
*ptin
;
}
break;
case '{':
}
break;
case '{':
+ *ptout++ = '\n';
+ for (i = 0; i < nbindent * TABSIZE; i++) {
+ *ptout++ = ' ';
+ }
+ *ptout++ = *ptin;
+ *ptout++ = '\n';
+ nbindent++;
+ newline = 1;
+ begin = 1;
+ break;
case '}':
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;
case ';':
*ptout++ = *ptin;
+ if (parent) {
+ break;
+ }
*ptout++ = '\n';
*ptout++ = '\n';
+ newline = 1;
+ begin = 1;
+ break;
+ case '\n':
+ if (newline == 1) {
+ newline = 0;
+ } else {
+ *ptout++ = '\n';
+ }
+ begin = 1;
+ break;
+ case '\r':
break;
default:
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;
}
*ptout++ = *ptin;
}
+ special = (*ptin == '\\');
+ parent += (*ptin == '(') ? +1 : (*ptin == ')') ? -1 : 0;
ptin++;
}
ptout = '\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;
/* 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);
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) {
/* check output */
FILE *fout = NULL;
if (output) {
- fout = fopen (
in
put, "wb");
+ fout = fopen (
out
put, "wb");
if (!fout) {
VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", output));
fclose (fin);
if (!fout) {
VERBOSE (ERROR, fprintf (stderr, "error: can't open file '%s'\n", output));
fclose (fin);