Browse Source

more robust getline for windows added

master
Chris Cromer 3 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 \
makexml.c \
makexml.h \
encoding.c \
encoding.h
encoding.h \
getline.c \
getline.h
generarxml_CPPFLAGS = $(XML_CPPFLAGS)
generarxml_LDFLAGS= $(XML_LIBS)
generarxml_LDADD = $(libxml2_LIBS) $(LDFLAGS)

+ 20
- 2
src/Makefile.in View File

@@ -100,7 +100,8 @@ am__installdirs = "$(DESTDIR)$(bindir)"
PROGRAMS = $(bin_PROGRAMS)
am_generarxml_OBJECTS = generarxml-main.$(OBJEXT) \
generarxml-readconfig.$(OBJEXT) generarxml-readfile.$(OBJEXT) \
generarxml-makexml.$(OBJEXT) generarxml-encoding.$(OBJEXT)
generarxml-makexml.$(OBJEXT) generarxml-encoding.$(OBJEXT) \
generarxml-getline.$(OBJEXT)
generarxml_OBJECTS = $(am_generarxml_OBJECTS)
am__DEPENDENCIES_1 =
generarxml_DEPENDENCIES = $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1)
@@ -269,7 +270,9 @@ generarxml_SOURCES = main.c \
makexml.c \
makexml.h \
encoding.c \
encoding.h
encoding.h \
getline.c \
getline.h

generarxml_CPPFLAGS = $(XML_CPPFLAGS)
generarxml_LDFLAGS = $(XML_LIBS)
@@ -361,6 +364,7 @@ distclean-compile:
-rm -f *.tab.c

@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/generarxml-encoding.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/generarxml-getline.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/generarxml-main.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/generarxml-makexml.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/generarxml-readconfig.Po@am__quote@
@@ -450,6 +454,20 @@ generarxml-encoding.obj: encoding.c
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@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`

generarxml-getline.o: getline.c
@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
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/generarxml-getline.Tpo $(DEPDIR)/generarxml-getline.Po
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='getline.c' object='generarxml-getline.o' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@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

generarxml-getline.obj: getline.c
@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`
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/generarxml-getline.Tpo $(DEPDIR)/generarxml-getline.Po
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='getline.c' object='generarxml-getline.obj' libtool=no @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@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`

ID: $(am__tagged_files)
$(am__define_uniq_tagged_files); mkid -fID $$unique
tags: tags-am

+ 147
- 0
src/getline.c View File

@@ -0,0 +1,147 @@
#ifdef __MINGW32__
#include <stdio.h>
#include <stdlib.h>
#include "getline.h"

size_t getline(char **lineptr, size_t *n, FILE *stream) {
int ch;
size_t chars = 0;
char *line = *lineptr;

/* why even call this if you don't have a stream to read? */
if (stream == NULL) {
return -1;
}

ch = fgetc(stream);
if (ch == EOF) {
return -1;
}
if (line == NULL) {
if (*n == 0) {
line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
*n = GET_LINE_MAX_MEM;
}
else {
line = (char *) calloc(*n, sizeof(char));
}

/* out of memory */
if (line == NULL) {
return -1;
}
}
else {
free(line);
*n = GET_LINE_MAX_MEM;
line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
}
while(ch != EOF) {
if (chars == *n) {
/* not enough memory for more chars, add more */
*n = *n + GET_LINE_MAX_MEM;
line = (char *) realloc(line, *n * sizeof(char));
}
if (ch == '\n') {
if (chars == *n - 1) {
/* add space for the null terminator */
*n = *n + 1;
line = (char *) realloc(line, *n * sizeof(char));
}
line[chars] = '\n';
line[chars + 1] = '\0';
break;
}
line[chars] = ch;
ch = fgetc(stream);
chars++;
}
if (ch == EOF) {
if (chars == *n) {
/* add space for the null terminator */
*n = *n + 1;
line = (char *) realloc(line, *n * sizeof(char));
}
line[chars] = '\0';
}

*lineptr = line;

return chars + 1;
}

size_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream) {
int ch;
size_t chars = 0;
char *line = *lineptr;

/* why even call this if you don't have a stream to read? */
if (stream == NULL) {
return -1;
}

ch = fgetc(stream);
if (ch == EOF) {
return -1;
}
if (line == NULL) {
if (*n == 0) {
line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
*n = GET_LINE_MAX_MEM;
}
else {
line = (char *) calloc(*n, sizeof(char));
}

/* out of memory */
if (line == NULL) {
return -1;
}
}
else {
free(line);
*n = GET_LINE_MAX_MEM;
line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
}

while(ch != EOF) {
if (chars == *n) {
/* not enough memory for more chars, add more */
*n = *n + GET_LINE_MAX_MEM;
line = (char *) realloc(line, *n * sizeof(char));
}
if (ch == '\n') {
if (chars == *n - 1) {
/* add space for the null terminator */
*n = *n + 1;
line = (char *) realloc(line, *n * sizeof(char));
}
line[chars] = delim;
line[chars + 1] = '\0';
break;
}
line[chars] = ch;
ch = fgetc(stream);
chars++;
}
if (ch == EOF) {
if (chars == *n) {
/* add space for the null terminator */
*n = *n + 1;
line = (char *) realloc(line, *n * sizeof(char));
}
line[chars] = '\0';
}

if (chars < *n) {
line = (char *) realloc(line, chars + 2 * sizeof(char));
}

*lineptr = line;

return chars + 1;
}
#endif

+ 6
- 0
src/getline.h View File

@@ -0,0 +1,6 @@
#ifdef __MINGW32__
#define GET_LINE_MAX_MEM 128

size_t getline(char **lineptr, size_t *n, FILE *stream);
size_t getdelim(char **lineptr, size_t *n, int delim, FILE *stream);
#endif

+ 1
- 70
src/readfile.c View File

@@ -7,77 +7,8 @@
#include "main.h"
#include "readfile.h"
#include "encoding.h"

#ifdef __MINGW32__
#define GET_LINE_MAX_MEM 128
size_t getline(char **lineptr, size_t *n, FILE *stream) {
int ch;
size_t chars = 0;
char *line = *lineptr;

/* why even call this if you don't have a stream to read? */
if (stream == NULL) {
return -1;
}

ch = fgetc(stream);
if (ch == EOF) {
return -1;
}
if (line == NULL) {
if (*n == 0) {
line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
*n = GET_LINE_MAX_MEM;
}
else {
line = (char *) calloc(*n, sizeof(char));
}

/* out of memory */
if (line == NULL) {
return -1;
}
}
else {
free(line);
*n = GET_LINE_MAX_MEM;
line = (char *) calloc(GET_LINE_MAX_MEM, sizeof(char));
}
while(ch != EOF) {
if (chars == *n) {
/* not enough memory for more chars, add more */
*n = *n + GET_LINE_MAX_MEM;
line = (char *) realloc(line, *n * sizeof(char));
}
if (ch == '\n') {
if (chars == *n - 1) {
/* add space for the null terminator */
*n = *n + 1;
line = (char *) realloc(line, *n * sizeof(char));
}
line[chars] = '\n';
line[chars + 1] = '\0';
break;
}
line[chars] = ch;
ch = fgetc(stream);
chars++;
}
if (ch == EOF) {
if (chars == *n) {
/* add space for the null terminator */
*n = *n + 1;
line = (char *) realloc(line, *n * sizeof(char));
}
line[chars] = '\0';
}

*lineptr = line;

return chars + 1;
}
#include "getline.h"
#endif

/*

Loading…
Cancel
Save