]> granicus.if.org Git - flex/commitdiff
chomp'd lines when reading external skel file.
authorJohn Millaway <john43@users.sourceforge.net>
Fri, 10 May 2002 19:33:34 +0000 (19:33 +0000)
committerJohn Millaway <john43@users.sourceforge.net>
Fri, 10 May 2002 19:33:34 +0000 (19:33 +0000)
flexdef.h
misc.c

index 8b82bb39a1da157b1a8952a3a85a906225344989..83843a1013a1c16f8f1064a187cf341fe375d174 100644 (file)
--- a/flexdef.h
+++ b/flexdef.h
@@ -1058,4 +1058,7 @@ extern struct Buf defs_buf;
 extern jmp_buf flex_main_jmp_buf;
 #define FLEX_EXIT(status) longjmp(flex_main_jmp_buf,(status)+1)
 
+/* Removes all \n and \r chars from tail of str. returns str. */
+extern char* chomp(char* str);
+
 #endif /* not defined FLEXDEF_H */
diff --git a/misc.c b/misc.c
index 55fd07da72a28e76b99412e2c37ca223c27b8703..2e389bc79a4610247a06d4ee8b09b17281519f7a 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -796,7 +796,12 @@ void skelout()
        while ( skelfile ?
                (fgets( buf, MAXLINE, skelfile ) != NULL) :
                ((buf = (char *) skel[skel_ind++]) != 0) )
-               { /* copy from skel array */
+               { 
+
+               if (skelfile )
+                       chomp(buf);
+
+               /* copy from skel array */
                if ( buf[0] == '%' )
                        { /* control line */
                        /* print the control line as a comment. */
@@ -844,15 +849,7 @@ void skelout()
                        }
 
                else if ( do_copy )
-                       {
-                       if ( skelfile )
-                               /* Skeleton file reads include final
-                                * newline, skel[] array does not.
-                                */
-                               out( buf );
-                       else
                                outn( buf );
-                       }
                }
        }
 
@@ -915,3 +912,22 @@ size_t size_in_bytes;
        while ( rp < rp_end )
                *rp++ = 0;
        }
+
+/* Remove all '\n' and '\r' characters, if any, from the end of str.
+ * str can be any null-terminated string, or NULL.
+ * returns str. */
+char* chomp(char* str){
+    char* p=str;
+    if (!str || !*str)  /* s is null or empty string */
+        return str;
+
+    /* find end of string minus one*/
+    while (*p)
+        ++p;
+    --p;
+
+    /* eat newlines */
+    while (p >= str && (*p == '\r' || *p == '\n'))
+        *p-- = 0;
+    return str;
+}