Browse Source

more robust getline for windows added

Chris Cromer 2 years ago
parent
commit
9488ad1bd6
5 changed files with 177 additions and 73 deletions
  1. 3
    1
      src/Makefile.am
  2. 20
    2
      src/Makefile.in
  3. 147
    0
      src/getline.c
  4. 6
    0
      src/getline.h
  5. 1
    70
      src/readfile.c

+ 3
- 1
src/Makefile.am View File

@@ -10,7 +10,9 @@ generarxml_SOURCES = main.c \
10 10
     makexml.c \
11 11
     makexml.h \
12 12
     encoding.c \
13
-    encoding.h
13
+    encoding.h \
14
+    getline.c \
15
+    getline.h
14 16
 generarxml_CPPFLAGS = $(XML_CPPFLAGS)
15 17
 generarxml_LDFLAGS= $(XML_LIBS)
16 18
 generarxml_LDADD = $(libxml2_LIBS) $(LDFLAGS)

+ 20
- 2
src/Makefile.in View File

@@ -100,7 +100,8 @@ am__installdirs = "$(DESTDIR)$(bindir)"
100 100
 PROGRAMS = $(bin_PROGRAMS)
101 101
 am_generarxml_OBJECTS = generarxml-main.$(OBJEXT) \
102 102
 	generarxml-readconfig.$(OBJEXT) generarxml-readfile.$(OBJEXT) \
103
-	generarxml-makexml.$(OBJEXT) generarxml-encoding.$(OBJEXT)
103
+	generarxml-makexml.$(OBJEXT) generarxml-encoding.$(OBJEXT) \
104
+	generarxml-getline.$(OBJEXT)
104 105
 generarxml_OBJECTS = $(am_generarxml_OBJECTS)
105 106
 am__DEPENDENCIES_1 =
106 107
 generarxml_DEPENDENCIES = $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1)
@@ -269,7 +270,9 @@ generarxml_SOURCES = main.c \
269 270
     makexml.c \
270 271
     makexml.h \
271 272
     encoding.c \
272
-    encoding.h
273
+    encoding.h \
274
+    getline.c \
275
+    getline.h
273 276
 
274 277
 generarxml_CPPFLAGS = $(XML_CPPFLAGS)
275 278
 generarxml_LDFLAGS = $(XML_LIBS)
@@ -361,6 +364,7 @@ distclean-compile:
361 364
 	-rm -f *.tab.c
362 365
 
363 366
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/generarxml-encoding.Po@am__quote@
367
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/generarxml-getline.Po@am__quote@
364 368
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/generarxml-main.Po@am__quote@
365 369
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/generarxml-makexml.Po@am__quote@
366 370
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/generarxml-readconfig.Po@am__quote@
@@ -450,6 +454,20 @@ generarxml-encoding.obj: encoding.c
450 454
 @AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
451 455
 @am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(generarxml_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o generarxml-encoding.obj `if test -f 'encoding.c'; then $(CYGPATH_W) 'encoding.c'; else $(CYGPATH_W) '$(srcdir)/encoding.c'; fi`
452 456
 
457
+generarxml-getline.o: getline.c
458
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(generarxml_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT generarxml-getline.o -MD -MP -MF $(DEPDIR)/generarxml-getline.Tpo -c -o generarxml-getline.o `test -f 'getline.c' || echo '$(srcdir)/'`getline.c
459
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/generarxml-getline.Tpo $(DEPDIR)/generarxml-getline.Po
460
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='getline.c' object='generarxml-getline.o' libtool=no @AMDEPBACKSLASH@
461
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
462
+@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(generarxml_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o generarxml-getline.o `test -f 'getline.c' || echo '$(srcdir)/'`getline.c
463
+
464
+generarxml-getline.obj: getline.c
465
+@am__fastdepCC_TRUE@	$(AM_V_CC)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(generarxml_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT generarxml-getline.obj -MD -MP -MF $(DEPDIR)/generarxml-getline.Tpo -c -o generarxml-getline.obj `if test -f 'getline.c'; then $(CYGPATH_W) 'getline.c'; else $(CYGPATH_W) '$(srcdir)/getline.c'; fi`
466
+@am__fastdepCC_TRUE@	$(AM_V_at)$(am__mv) $(DEPDIR)/generarxml-getline.Tpo $(DEPDIR)/generarxml-getline.Po
467
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	$(AM_V_CC)source='getline.c' object='generarxml-getline.obj' libtool=no @AMDEPBACKSLASH@
468
+@AMDEP_TRUE@@am__fastdepCC_FALSE@	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
469
+@am__fastdepCC_FALSE@	$(AM_V_CC@am__nodep@)$(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(generarxml_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o generarxml-getline.obj `if test -f 'getline.c'; then $(CYGPATH_W) 'getline.c'; else $(CYGPATH_W) '$(srcdir)/getline.c'; fi`
470
+
453 471
 ID: $(am__tagged_files)
454 472
 	$(am__define_uniq_tagged_files); mkid -fID $$unique
455 473
 tags: tags-am

+ 147
- 0
src/getline.c View File

@@ -0,0 +1,147 @@
1
+#ifdef __MINGW32__
2
+#include <stdio.h>
3
+#include <stdlib.h>
4
+#include "getline.h"
5
+
6
+size_t getline(char **lineptr, size_t *n, FILE *stream) {
7
+    int ch;
8
+    size_t chars = 0;
9
+    char *line = *lineptr;
10
+
11
+    /* why even call this if you don't have a stream to read? */
12
+    if (stream == NULL) {
13
+    	return -1;
14
+    }
15
+
16
+    ch = fgetc(stream);
17
+    if (ch == EOF) {
18
+    	return -1;
19
+    }
20
+    
21
+    if (line == NULL) {
22
+        if (*n == 0) {
23
+            line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
24
+            *n = GET_LINE_MAX_MEM;
25
+        }
26
+        else {
27
+            line = (char *) calloc(*n, sizeof(char));
28
+        }
29
+
30
+        /* out of memory */
31
+        if (line == NULL) {
32
+            return -1;
33
+        }
34
+    }
35
+    else {
36
+        free(line);
37
+        *n = GET_LINE_MAX_MEM;
38
+        line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
39
+    }
40
+    
41
+    while(ch != EOF) {
42
+        if (chars == *n) {
43
+            /* not enough memory for more chars, add more */
44
+            *n = *n + GET_LINE_MAX_MEM;
45
+            line = (char *) realloc(line, *n * sizeof(char));
46
+        }
47
+    	if (ch == '\n') {
48
+            if (chars == *n - 1) {
49
+                /* add space for the null terminator */
50
+                *n = *n + 1;
51
+                line = (char *) realloc(line, *n * sizeof(char));
52
+            }
53
+            line[chars] = '\n';
54
+            line[chars + 1] = '\0';
55
+    		break;
56
+    	}
57
+        line[chars] = ch;
58
+        ch = fgetc(stream);
59
+        chars++;
60
+    }
61
+    if (ch == EOF) {
62
+        if (chars == *n) {
63
+            /* add space for the null terminator */
64
+            *n = *n + 1;
65
+            line = (char *) realloc(line, *n * sizeof(char));
66
+        }
67
+        line[chars] = '\0';
68
+    }
69
+
70
+    *lineptr = line;
71
+
72
+    return chars + 1;
73
+}
74
+
75
+size_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream) {
76
+    int ch;
77
+    size_t chars = 0;
78
+    char *line = *lineptr;
79
+
80
+    /* why even call this if you don't have a stream to read? */
81
+    if (stream == NULL) {
82
+    	return -1;
83
+    }
84
+
85
+    ch = fgetc(stream);
86
+    if (ch == EOF) {
87
+    	return -1;
88
+    }
89
+    
90
+    if (line == NULL) {
91
+        if (*n == 0) {
92
+            line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
93
+            *n = GET_LINE_MAX_MEM;
94
+        }
95
+        else {
96
+            line = (char *) calloc(*n, sizeof(char));
97
+        }
98
+
99
+        /* out of memory */
100
+        if (line == NULL) {
101
+            return -1;
102
+        }
103
+    }
104
+    else {
105
+        free(line);
106
+        *n = GET_LINE_MAX_MEM;
107
+        line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
108
+    }
109
+
110
+    while(ch != EOF) {
111
+        if (chars == *n) {
112
+            /* not enough memory for more chars, add more */
113
+            *n = *n + GET_LINE_MAX_MEM;
114
+            line = (char *) realloc(line, *n * sizeof(char));
115
+        }
116
+    	if (ch == '\n') {
117
+            if (chars == *n - 1) {
118
+                /* add space for the null terminator */
119
+                *n = *n + 1;
120
+                line = (char *) realloc(line, *n * sizeof(char));
121
+            }
122
+            line[chars] = delim;
123
+            line[chars + 1] = '\0';
124
+    		break;
125
+    	}
126
+        line[chars] = ch;
127
+        ch = fgetc(stream);
128
+        chars++;
129
+    }
130
+    if (ch == EOF) {
131
+        if (chars == *n) {
132
+            /* add space for the null terminator */
133
+            *n = *n + 1;
134
+            line = (char *) realloc(line, *n * sizeof(char));
135
+        }
136
+        line[chars] = '\0';
137
+    }
138
+
139
+    if (chars < *n) {
140
+        line = (char *) realloc(line, chars + 2 * sizeof(char));
141
+    }
142
+
143
+    *lineptr = line;
144
+
145
+    return chars + 1;
146
+}
147
+#endif

+ 6
- 0
src/getline.h View File

@@ -0,0 +1,6 @@
1
+#ifdef __MINGW32__
2
+#define GET_LINE_MAX_MEM 128
3
+
4
+size_t getline(char **lineptr, size_t *n, FILE *stream);
5
+size_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
6
+#endif

+ 1
- 70
src/readfile.c View File

@@ -7,77 +7,8 @@
7 7
 #include "main.h"
8 8
 #include "readfile.h"
9 9
 #include "encoding.h"
10
-
11 10
 #ifdef __MINGW32__
12
-#define GET_LINE_MAX_MEM 128
13
-size_t getline(char **lineptr, size_t *n, FILE *stream) {
14
-    int ch;
15
-    size_t chars = 0;
16
-    char *line = *lineptr;
17
-
18
-    /* why even call this if you don't have a stream to read? */
19
-    if (stream == NULL) {
20
-        return -1;
21
-    }
22
-
23
-    ch = fgetc(stream);
24
-    if (ch == EOF) {
25
-        return -1;
26
-    }
27
-    
28
-    if (line == NULL) {
29
-        if (*n == 0) {
30
-            line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
31
-            *n = GET_LINE_MAX_MEM;
32
-        }
33
-        else {
34
-            line = (char *) calloc(*n, sizeof(char));
35
-        }
36
-
37
-        /* out of memory */
38
-        if (line == NULL) {
39
-            return -1;
40
-        }
41
-    }
42
-    else {
43
-        free(line);
44
-        *n = GET_LINE_MAX_MEM;
45
-        line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
46
-    }
47
-    
48
-    while(ch != EOF) {
49
-        if (chars == *n) {
50
-            /* not enough memory for more chars, add more */
51
-            *n = *n + GET_LINE_MAX_MEM;
52
-            line = (char *) realloc(line, *n * sizeof(char));
53
-        }
54
-        if (ch == '\n') {
55
-            if (chars == *n - 1) {
56
-                /* add space for the null terminator */
57
-                *n = *n + 1;
58
-                line = (char *) realloc(line, *n * sizeof(char));
59
-            }
60
-            line[chars] = '\n';
61
-            line[chars + 1] = '\0';
62
-            break;
63
-        }
64
-        line[chars] = ch;
65
-        ch = fgetc(stream);
66
-        chars++;
67
-    }
68
-    if (ch == EOF) {
69
-        if (chars == *n) {
70
-            /* add space for the null terminator */
71
-            *n = *n + 1;
72
-            line = (char *) realloc(line, *n * sizeof(char));
73
-        }
74
-        line[chars] = '\0';
75
-    }
76
-
77
-    *lineptr = line;
78
-
79
-    return chars + 1;
80
-}
11
+#include "getline.h"
81 12
 #endif
82 13
 
83 14
 /*

Loading…
Cancel
Save