mirror of
https://github.com/kgabis/parson.git
synced 2025-05-10 03:32:08 +00:00
Better handling of CRLF line breaks.
This commit is contained in:
parent
c147744b72
commit
387c5665f6
14
parson.c
14
parson.c
@ -262,7 +262,8 @@ static int is_decimal(const char *string, size_t length) {
|
|||||||
|
|
||||||
static char * read_file(const char * filename) {
|
static char * read_file(const char * filename) {
|
||||||
FILE *fp = fopen(filename, "r");
|
FILE *fp = fopen(filename, "r");
|
||||||
size_t file_size;
|
size_t size_to_read = 0;
|
||||||
|
size_t size_read = 0;
|
||||||
long pos;
|
long pos;
|
||||||
char *file_contents;
|
char *file_contents;
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
@ -274,22 +275,21 @@ static char * read_file(const char * filename) {
|
|||||||
fclose(fp);
|
fclose(fp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
file_size = pos;
|
size_to_read = pos;
|
||||||
rewind(fp);
|
rewind(fp);
|
||||||
file_contents = (char*)parson_malloc(sizeof(char) * (file_size + 1));
|
file_contents = (char*)parson_malloc(sizeof(char) * (size_to_read + 1));
|
||||||
if (!file_contents) {
|
if (!file_contents) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (fread(file_contents, file_size, 1, fp) < 1) {
|
size_read = fread(file_contents, 1, size_to_read, fp);
|
||||||
if (ferror(fp)) {
|
if (size_read == 0 || ferror(fp)) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
parson_free(file_contents);
|
parson_free(file_contents);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
file_contents[file_size] = '\0';
|
file_contents[size_read] = '\0';
|
||||||
return file_contents;
|
return file_contents;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
17
tests.c
17
tests.c
@ -614,33 +614,34 @@ void serialization_example(void) {
|
|||||||
|
|
||||||
static char * read_file(const char * filename) {
|
static char * read_file(const char * filename) {
|
||||||
FILE *fp = fopen(filename, "r");
|
FILE *fp = fopen(filename, "r");
|
||||||
size_t file_size;
|
size_t size_to_read = 0;
|
||||||
|
size_t size_read = 0;
|
||||||
long pos;
|
long pos;
|
||||||
char *file_contents;
|
char *file_contents;
|
||||||
if (!fp)
|
if (!fp) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
}
|
||||||
fseek(fp, 0L, SEEK_END);
|
fseek(fp, 0L, SEEK_END);
|
||||||
pos = ftell(fp);
|
pos = ftell(fp);
|
||||||
if (pos < 0) {
|
if (pos < 0) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
file_size = pos;
|
size_to_read = pos;
|
||||||
rewind(fp);
|
rewind(fp);
|
||||||
file_contents = (char*)malloc(sizeof(char) * (file_size + 1));
|
file_contents = (char*)malloc(sizeof(char) * (size_to_read + 1));
|
||||||
if (!file_contents) {
|
if (!file_contents) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (fread(file_contents, file_size, 1, fp) < 1) {
|
size_read = fread(file_contents, 1, size_to_read, fp);
|
||||||
if (ferror(fp)) {
|
if (size_read == 0 || ferror(fp)) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
free(file_contents);
|
free(file_contents);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
file_contents[file_size] = '\0';
|
file_contents[size_read] = '\0';
|
||||||
return file_contents;
|
return file_contents;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user