From: John Millaway Date: Fri, 10 May 2002 19:33:34 +0000 (+0000) Subject: chomp'd lines when reading external skel file. X-Git-Tag: flex-2-5-10~62 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=3c5f2fde0dedf5b4fd54abac9a7320d347d749b6;p=flex chomp'd lines when reading external skel file. --- diff --git a/flexdef.h b/flexdef.h index 8b82bb3..83843a1 100644 --- 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 55fd07d..2e389bc 100644 --- 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; +}