[MPlayer-cvslog] r34648 - in trunk/gui: cfg.c skin/font.c skin/skin.c util/string.c util/string.h

ib subversion at mplayerhq.hu
Fri Feb 3 14:38:42 CET 2012


Author: ib
Date: Fri Feb  3 14:38:42 2012
New Revision: 34648

Log:
Add fgetstr() to read from files without end-of-line characters.

Use it where fgets'ed lines are cleared of EOLs.

Remove gfgets() and replace these calls by fgetstr().

Modified:
   trunk/gui/cfg.c
   trunk/gui/skin/font.c
   trunk/gui/skin/skin.c
   trunk/gui/util/string.c
   trunk/gui/util/string.h

Modified: trunk/gui/cfg.c
==============================================================================
--- trunk/gui/cfg.c	Thu Feb  2 19:26:17 2012	(r34647)
+++ trunk/gui/cfg.c	Fri Feb  3 14:38:42 2012	(r34648)
@@ -223,27 +223,6 @@ static const m_option_t gui_opts[] = {
     { NULL,                          NULL,                     0,                     0,           0,     0,       NULL }
 };
 
-static char *gfgets(char *str, int size, FILE *f)
-{
-    char *s, c;
-
-    s = fgets(str, size, f);
-
-    if (s) {
-        c = s[strlen(s) - 1];
-
-        if (c == '\n' || c == '\r')
-            s[strlen(s) - 1] = 0;
-
-        c = s[strlen(s) - 1];
-
-        if (c == '\n' || c == '\r')
-            s[strlen(s) - 1] = 0;
-    }
-
-    return s;
-}
-
 int cfg_gui_include(m_option_t *conf, const char *filename)
 {
     (void)conf;
@@ -290,12 +269,12 @@ int cfg_read(void)
             char tmp[512];
             plItem *item;
 
-            if (gfgets(tmp, 512, f) == NULL)
+            if (fgetstr(tmp, 512, f) == NULL)
                 continue;
 
             item       = calloc(1, sizeof(plItem));
             item->path = strdup(tmp);
-            gfgets(tmp, 512, f);
+            fgetstr(tmp, 512, f);
             item->name = strdup(tmp);
             listSet(gtkAddPlItem, item);
         }
@@ -315,7 +294,7 @@ int cfg_read(void)
             char tmp[512];
             urlItem *item;
 
-            if (gfgets(tmp, 512, f) == NULL)
+            if (fgetstr(tmp, 512, f) == NULL)
                 continue;
 
             item      = calloc(1, sizeof(urlItem));
@@ -339,7 +318,7 @@ int cfg_read(void)
         while (!feof(f)) {
             char tmp[512];
 
-            if (gfgets(tmp, 512, f) == NULL)
+            if (fgetstr(tmp, 512, f) == NULL)
                 continue;
 
             fsHistory[i++] = gstrdup(tmp);

Modified: trunk/gui/skin/font.c
==============================================================================
--- trunk/gui/skin/font.c	Thu Feb  2 19:26:17 2012	(r34647)
+++ trunk/gui/skin/font.c	Fri Feb  3 14:38:42 2012	(r34648)
@@ -125,8 +125,7 @@ int fntRead(char *path, char *fname)
         return -3;
     }
 
-    while (fgets(buf, sizeof(buf), f)) {
-        buf[strcspn(buf, "\n\r")] = 0; // remove any kind of newline, if any
+    while (fgetstr(buf, sizeof(buf), f)) {
         strswap(buf, '\t', ' ');
         trim(buf);
         decomment(buf);

Modified: trunk/gui/skin/skin.c
==============================================================================
--- trunk/gui/skin/skin.c	Thu Feb  2 19:26:17 2012	(r34647)
+++ trunk/gui/skin/skin.c	Fri Feb  3 14:38:42 2012	(r34648)
@@ -1055,10 +1055,9 @@ int skinRead(char *sname)
     currWinName[0] = 0;
     linenumber     = 0;
 
-    while (fgets(line, sizeof(line), skinFile)) {
+    while (fgetstr(line, sizeof(line), skinFile)) {
         linenumber++;
 
-        line[strcspn(line, "\n\r")] = 0; // remove any kind of newline, if any
         strswap(line, '\t', ' ');
         trim(line);
         decomment(line);

Modified: trunk/gui/util/string.c
==============================================================================
--- trunk/gui/util/string.c	Thu Feb  2 19:26:17 2012	(r34647)
+++ trunk/gui/util/string.c	Fri Feb  3 14:38:42 2012	(r34648)
@@ -16,7 +16,6 @@
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
-#include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
@@ -321,3 +320,26 @@ char *TranslateFilename(int how, char *f
 
     return fname;
 }
+
+/**
+ * @brief Read characters from @a file.
+ *
+ * @note Reading stops with '\\r', '\\n' or EOF.
+ *
+ * @param str pointer to a buffer to receive the read characters
+ * @param size number of characters read at the most (including a terminating null-character)
+ * @param file file to read from
+ *
+ * @return str (success) or NULL (error)
+ */
+char *fgetstr(char *str, int size, FILE *file)
+{
+    char *s;
+
+    s = fgets(str, size, file);
+
+    if (s)
+        s[strcspn(s, "\n\r")] = 0;
+
+    return s;
+}

Modified: trunk/gui/util/string.h
==============================================================================
--- trunk/gui/util/string.h	Thu Feb  2 19:26:17 2012	(r34647)
+++ trunk/gui/util/string.h	Fri Feb  3 14:38:42 2012	(r34648)
@@ -19,7 +19,10 @@
 #ifndef MPLAYER_GUI_STRING_H
 #define MPLAYER_GUI_STRING_H
 
+#include <stdio.h>
+
 char *decomment(char *in);
+char *fgetstr(char *str, int size, FILE *file);
 int gstrcasecmp(const char *a, const char *b);
 char *gstrchr(const char *str, int c);
 int gstrcmp(const char *a, const char *b);


More information about the MPlayer-cvslog mailing list