From 18a48c3c1c1aa183a1b1ab033be34329685199f6 Mon Sep 17 00:00:00 2001 From: "humper@google.com" Date: Mon, 14 Jan 2013 19:42:08 +0000 Subject: [PATCH] replace getline with fgets for portability BUG= Review URL: https://codereview.appspot.com/7085058 git-svn-id: http://skia.googlecode.com/svn/trunk@7163 2bbb7eff-a529-9590-31e7-b0007b416f81 --- include/core/SkOSFile.h | 9 +++------ src/ports/SkOSFile_stdio.cpp | 14 +++----------- src/utils/SkRTConf.cpp | 18 ++++++------------ 3 files changed, 12 insertions(+), 29 deletions(-) diff --git a/include/core/SkOSFile.h b/include/core/SkOSFile.h index 685248cd00..257b66ae67 100644 --- a/include/core/SkOSFile.h +++ b/include/core/SkOSFile.h @@ -43,6 +43,9 @@ bool sk_frewind(SkFILE*); size_t sk_fread(void* buffer, size_t byteCount, SkFILE*); size_t sk_fwrite(const void* buffer, size_t byteCount, SkFILE*); + +char* sk_fgets(char* str, int size, SkFILE* f); + void sk_fflush(SkFILE*); int sk_fseek(SkFILE*, size_t, int); @@ -54,12 +57,6 @@ bool sk_exists(const char *path); // Returns true if a directory exists at this path. bool sk_isdir(const char *path); -// Get a single line of input from a file. Returns -1 on failure. -// passing NULL for lineptr will allocate memory for the line with -// sk_malloc; make sure to use sk_free to get rid of it when you're -// done. -ptrdiff_t sk_getline(char **lineptr, size_t *n, SkFILE *stream); - // Have we reached the end of the file? int sk_feof(SkFILE *); diff --git a/src/ports/SkOSFile_stdio.cpp b/src/ports/SkOSFile_stdio.cpp index e7f65cd499..7663f8799d 100644 --- a/src/ports/SkOSFile_stdio.cpp +++ b/src/ports/SkOSFile_stdio.cpp @@ -41,19 +41,11 @@ SkFILE* sk_fopen(const char path[], SkFILE_Flags flags) return f; } -ptrdiff_t sk_getline(char **lineptr, size_t *n, SkFILE *f) { - bool make_private_copy = (NULL == *lineptr); - - ptrdiff_t ret = ::getline(lineptr, n, (FILE *) f); - if (make_private_copy) { - char *local_copy = (char *) sk_malloc_throw(strlen(*lineptr) + 1); - ::memcpy(local_copy, *lineptr, strlen(*lineptr)); - ::free(*lineptr); - *lineptr = local_copy; - } - return ret; +char* sk_fgets(char* str, int size, SkFILE* f) { + return ::fgets(str, size, (FILE *)f); } + int sk_feof(SkFILE *f) { return ::feof((FILE *)f); } diff --git a/src/utils/SkRTConf.cpp b/src/utils/SkRTConf.cpp index 2ccfd061cb..38bc64ce76 100644 --- a/src/utils/SkRTConf.cpp +++ b/src/utils/SkRTConf.cpp @@ -16,29 +16,23 @@ SkRTConfRegistry::SkRTConfRegistry(): fConfs(100) { return; } - char *line = NULL; - size_t n = 0; + char line[1024]; while (!sk_feof(fp)) { - if (line) { - sk_free(line); - } - line = NULL; - if (sk_getline(&line, &n, fp) == -1) break; + if (!sk_fgets(line, sizeof(line), fp)) { + break; + } char *commentptr = strchr(line, '#'); if (commentptr == line) { continue; } if (NULL != commentptr) { - char *tmp = (char *) sk_malloc_throw(commentptr-line+1); - strncpy(tmp, line, commentptr-line); - sk_free(line); - line = tmp; + *commentptr = '\0'; } - char sep[] = " \t"; + char sep[] = " \t\r\n"; char *keyptr = strtok(line, sep); if (!keyptr) {