Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
source code to manage the Windows Unix and Mac file formats.
authorcherierm <cherierm@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Wed, 4 Jun 2008 17:47:23 +0000 (17:47 +0000)
committercherierm <cherierm@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Wed, 4 Jun 2008 17:47:23 +0000 (17:47 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@5536 48e7efb5-ca39-0410-a469-dd3cf9ba447f

tools/tesh2/include/readline.h [new file with mode: 0644]
tools/tesh2/src/readline.c [new file with mode: 0644]

diff --git a/tools/tesh2/include/readline.h b/tools/tesh2/include/readline.h
new file mode 100644 (file)
index 0000000..2298c3a
--- /dev/null
@@ -0,0 +1,20 @@
+#ifndef __READLINE_H   \r
+#define __READLINE_H\r
+\r
+#include <com.h>\r
+\r
+#ifdef __cplusplus\r
+extern "C" {\r
+#endif\r
+\r
+long\r
+readline(FILE* stream, char **buf, size_t *n);\r
+\r
+#ifdef __cplusplus\r
+}\r
+#endif\r
+\r
+\r
+\r
+#endif /* !__READLINE_H */\r
+\r
diff --git a/tools/tesh2/src/readline.c b/tools/tesh2/src/readline.c
new file mode 100644 (file)
index 0000000..6a2c674
--- /dev/null
@@ -0,0 +1,80 @@
+#include <readline.h>
+
+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;
+}
+