From: cherierm Date: Wed, 4 Jun 2008 17:47:23 +0000 (+0000) Subject: source code to manage the Windows Unix and Mac file formats. X-Git-Tag: v3.3~423 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/b93bbbeb48fa3f92e3f8cf5d2c636ed7e0528a89?ds=sidebyside source code to manage the Windows Unix and Mac file formats. git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@5536 48e7efb5-ca39-0410-a469-dd3cf9ba447f --- diff --git a/tools/tesh2/include/readline.h b/tools/tesh2/include/readline.h new file mode 100644 index 0000000000..2298c3a690 --- /dev/null +++ b/tools/tesh2/include/readline.h @@ -0,0 +1,20 @@ +#ifndef __READLINE_H +#define __READLINE_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +long +readline(FILE* stream, char **buf, size_t *n); + +#ifdef __cplusplus +} +#endif + + + +#endif /* !__READLINE_H */ + diff --git a/tools/tesh2/src/readline.c b/tools/tesh2/src/readline.c new file mode 100644 index 0000000000..6a2c674340 --- /dev/null +++ b/tools/tesh2/src/readline.c @@ -0,0 +1,80 @@ +#include + +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(tesh); + +long +readline(FILE* stream, char **buf, size_t *n) +{ + size_t i; + int ch; + int cr = 0; + fpos_t pos; + + if (!*buf) { + *buf = calloc(512, sizeof(char)); + *n = 512; + } + + if (feof(stream)) + return (ssize_t)-1; + + for (i=0; (ch = fgetc(stream)) != EOF; i++) + { + if (i >= (*n) + 1) + *buf = xbt_realloc(*buf, *n += 512); + + + (*buf)[i] = ch; + + if(cr && (*buf)[i] != '\n') + { + /* old Mac os uses CR */ + i--; + (*buf)[i] = '\n'; + + /* move to the previous pos (pos of the CR) */ + fsetpos(stream, &pos); + + /* process as linux now */ + cr --; + } + + if((*buf)[i] == '\n') + { + if(cr) + { + /* Windows uses CRLF */ + (*buf)[i - 1] = '\n'; + (*buf)[i] = '\0'; + break; + } + else + { + /* Unix use LF */ + i++; + (*buf)[i] = '\0'; + break; + } + } + else if(ch == '\r') + { + cr ++; + + /* register the CR position for mac */ + fgetpos(stream, &pos); + } + } + + + if (i == *n) + *buf = xbt_realloc(*buf, *n += 1); + + /* Mac os file ended with a blank line */ + if(ch == EOF && (*buf)[i - 1] == '\r') + (*buf)[i - 1] = '\n'; + + (*buf)[i] = '\0'; + + return (ssize_t)i; +} +