add getline impl
This commit is contained in:
parent
86d72ffba3
commit
1da71680f7
5 changed files with 64 additions and 3 deletions
|
|
@ -8,12 +8,16 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/* file.c */
|
||||||
GSDECL GSFile GSFileOpen(GSEngine engine, const char* path);
|
GSDECL GSFile GSFileOpen(GSEngine engine, const char* path);
|
||||||
GSDECL int GSFileRead(GSFile file, void* out, int size);
|
GSDECL int GSFileRead(GSFile file, void* out, int size);
|
||||||
GSDECL void GSFileSeek(GSFile file, int pos);
|
GSDECL void GSFileSeek(GSFile file, int pos);
|
||||||
GSDECL unsigned int GSFileSize(GSFile file);
|
GSDECL unsigned int GSFileSize(GSFile file);
|
||||||
GSDECL void GSFileClose(GSFile file);
|
GSDECL void GSFileClose(GSFile file);
|
||||||
|
|
||||||
|
/* getline.c */
|
||||||
|
GSDECL int GSFileGetLine(char** lineptr, int* n, FILE* fp);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
54
engine/src/getline.c
Normal file
54
engine/src/getline.c
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
#include <GearSrc/File.h>
|
||||||
|
|
||||||
|
#define GETLINE_MINSIZE 16
|
||||||
|
|
||||||
|
int GSFileGetLine(char** lineptr, int* n, FILE* fp) {
|
||||||
|
int ch;
|
||||||
|
int i = 0;
|
||||||
|
char free_on_err = 0;
|
||||||
|
char* p;
|
||||||
|
|
||||||
|
if(lineptr == NULL || n == NULL || fp == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
if(*lineptr == NULL) {
|
||||||
|
*n = GETLINE_MINSIZE;
|
||||||
|
*lineptr = (char*)malloc(sizeof(char) * (*n));
|
||||||
|
if(*lineptr == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
free_on_err = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for(i = 0;; i++) {
|
||||||
|
ch = fgetc(fp);
|
||||||
|
while(i >= (*n) - 2) {
|
||||||
|
*n *= 2;
|
||||||
|
p = realloc(*lineptr, sizeof(char) * (*n));
|
||||||
|
if(p == NULL) {
|
||||||
|
if(free_on_err)
|
||||||
|
free(*lineptr);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*lineptr = p;
|
||||||
|
}
|
||||||
|
if(ch == EOF) {
|
||||||
|
if(i == 0) {
|
||||||
|
if(free_on_err)
|
||||||
|
free(*lineptr);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
(*lineptr)[i] = '\0';
|
||||||
|
*n = i;
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(ch == '\n') {
|
||||||
|
(*lineptr)[i] = '\n';
|
||||||
|
(*lineptr)[i + 1] = '\0';
|
||||||
|
*n = i + 1;
|
||||||
|
return i + 1;
|
||||||
|
}
|
||||||
|
(*lineptr)[i] = (char)ch;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,6 +5,7 @@ include(GNUInstallDirs)
|
||||||
macro(compile_tool name)
|
macro(compile_tool name)
|
||||||
file(GLOB ${name}_SRC ${name}/*.c)
|
file(GLOB ${name}_SRC ${name}/*.c)
|
||||||
add_executable(${name} ${${name}_SRC})
|
add_executable(${name} ${${name}_SRC})
|
||||||
|
target_link_libraries(${name} PRIVATE GearSrc)
|
||||||
install_target(${name})
|
install_target(${name})
|
||||||
endmacro()
|
endmacro()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,16 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <GearSrc/File.h>
|
||||||
|
|
||||||
#include <stb_image_write.h>
|
#include <stb_image_write.h>
|
||||||
|
|
||||||
#define CHECK(s) ((linelen > strlen(s)) && memcmp(line, s, strlen(s)) == 0)
|
#define CHECK(s) ((linelen > strlen(s)) && memcmp(line, s, strlen(s)) == 0)
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
char* line = NULL;
|
char* line = NULL;
|
||||||
size_t linesize;
|
int linesize;
|
||||||
ssize_t linelen;
|
int linelen;
|
||||||
int iw, ih;
|
int iw, ih;
|
||||||
unsigned char* font = NULL;
|
unsigned char* font = NULL;
|
||||||
int c;
|
int c;
|
||||||
|
|
@ -17,7 +19,7 @@ int main() {
|
||||||
int y = 0;
|
int y = 0;
|
||||||
int w, h;
|
int w, h;
|
||||||
|
|
||||||
while((linelen = getline(&line, &linesize, stdin)) != -1) {
|
while((linelen = GSFileGetLine(&line, &linesize, stdin)) != -1) {
|
||||||
linelen--;
|
linelen--;
|
||||||
|
|
||||||
line[linelen] = 0;
|
line[linelen] = 0;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue