changeset 6:9eea0c9ae113

Helper functions
author mmj
date Thu, 22 Apr 2004 06:35:57 +1000
parents 7e2a5f2c9315
children e447e6033387
files include/gethdrline.h include/mygetline.h src/gethdrline.c src/mygetline.c
diffstat 4 files changed, 112 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/gethdrline.h	Thu Apr 22 06:35:57 2004 +1000
@@ -0,0 +1,6 @@
+#ifndef __GETHDRLINE_H__
+#define __GETHDRLINE_H__
+
+char *gethdrline(FILE *infile);
+
+#endif /* __GETHDRLINE_H__ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/mygetline.h	Thu Apr 22 06:35:57 2004 +1000
@@ -0,0 +1,9 @@
+#ifndef __MYGETLINE_H__
+#define __MYGETLINE_H__
+#include <stdio.h>
+
+#define BUFSIZE 256
+
+char *mygetline(FILE *infile);
+
+#endif /* #ifndef __MYGETLINE_H__ */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/gethdrline.c	Thu Apr 22 06:35:57 2004 +1000
@@ -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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/mygetline.c	Thu Apr 22 06:35:57 2004 +1000
@@ -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