]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
Helper functions
authormmj <none@none>
Wed, 21 Apr 2004 20:35:57 +0000 (06:35 +1000)
committermmj <none@none>
Wed, 21 Apr 2004 20:35:57 +0000 (06:35 +1000)
include/gethdrline.h [new file with mode: 0644]
include/mygetline.h [new file with mode: 0644]
src/gethdrline.c [new file with mode: 0644]
src/mygetline.c [new file with mode: 0644]

diff --git a/include/gethdrline.h b/include/gethdrline.h
new file mode 100644 (file)
index 0000000..f5d3db5
--- /dev/null
@@ -0,0 +1,6 @@
+#ifndef __GETHDRLINE_H__
+#define __GETHDRLINE_H__
+
+char *gethdrline(FILE *infile);
+
+#endif /* __GETHDRLINE_H__ */
diff --git a/include/mygetline.h b/include/mygetline.h
new file mode 100644 (file)
index 0000000..60db803
--- /dev/null
@@ -0,0 +1,9 @@
+#ifndef __MYGETLINE_H__
+#define __MYGETLINE_H__
+#include <stdio.h>
+
+#define BUFSIZE 256
+
+char *mygetline(FILE *infile);
+
+#endif /* #ifndef __MYGETLINE_H__ */
diff --git a/src/gethdrline.c b/src/gethdrline.c
new file mode 100644 (file)
index 0000000..5b360b8
--- /dev/null
@@ -0,0 +1,49 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "mygetline.h"
+#include "gethdrline.h"
+#include "stringfuncs.h"
+
+char *gethdrline(FILE *infile)
+{
+       char *line = NULL, *retstr = NULL, *nextline = NULL, *tmp = NULL;
+       int ch;
+       
+       for(;;) {
+               line = mygetline(infile);
+               if(line == NULL)
+                       return NULL;
+               ch = getc(infile);
+               if(ch == '\t') {
+                       ungetc(ch, infile);
+                       nextline = mygetline(infile);
+                       tmp = retstr;
+                       retstr = concatstr(3, retstr, line, nextline);
+                       free(tmp); free(line); free(nextline);
+                       tmp = line = nextline = NULL;
+               } else {
+                       ungetc(ch, infile);
+                       tmp = retstr;
+                       retstr = concatstr(3, retstr, line, nextline);
+                       free(tmp);
+
+                       return retstr;
+               }
+       }
+}
+#if 0
+int main(int argc, char **argv)
+{
+       char *str;
+
+       while((str = gethdrline(stdin))) {
+               printf("%s", str);
+               free(str);
+       }
+
+       free(str);
+
+       return 0;
+}
+#endif
diff --git a/src/mygetline.c b/src/mygetline.c
new file mode 100644 (file)
index 0000000..e0a0c56
--- /dev/null
@@ -0,0 +1,48 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "mygetline.h"
+
+char *mygetline(FILE *infile)
+{
+       char *buf = malloc(BUFSIZE);
+       char *str = malloc(BUFSIZE);
+       size_t lenbuf, lenstr, i = 1;
+       
+       buf[0] = str[0] = 0;
+       for(;;) {
+               if(fgets(buf, BUFSIZE, infile) != NULL) {
+                       if(i == 1) {
+                               free(buf);
+                               free(str);
+                               return NULL;
+                       } else {
+                               free(buf);
+                               return str;
+                       }
+               }
+               lenbuf = strlen(buf);
+               lenstr = strlen(str);
+               realloc(str, lenbuf + lenstr + 1);
+               strcat(str, buf);
+               if(!((lenbuf == BUFSIZE - 1) && (buf[BUFSIZE - 1] != 'n'))) {
+                       free(buf);
+                       return str;
+               }
+       }
+}
+#if 0
+int main(int argc, char **argv)
+{
+       char *str;
+       
+       while((str = mygetline(stdin))) {
+               printf("%s", str);
+               free(str);
+       }
+
+       free(str);
+       return 0;
+}
+#endif