compat with std-99 (centos6, gcc 4.4.7)
authorNiki Roo <niki@nikiroo.be>
Sat, 26 Feb 2022 22:18:36 +0000 (23:18 +0100)
committerNiki Roo <niki@nikiroo.be>
Sat, 26 Feb 2022 22:18:36 +0000 (23:18 +0100)
src/nsub.d
src/nsub/nsub.c
src/nsub/nsub_read_lrc.c
src/nsub/nsub_write_lrc.c
src/nsub/nsub_write_webvtt.c
src/utils.d
src/utils/array.c
src/utils/desktop.c
src/utils/utils.c [new file with mode: 0644]
src/utils/utils.h

index 09f551efac5429816eaa9d9365aa45f97f24bfdd..da878cfb5a89846d29516cd109696c044885e9d0 100644 (file)
@@ -1,4 +1,4 @@
-CFLAGS   += -Wall -I./
+CFLAGS   += -Wall -I./ -std=c99
 CXXFLAGS += -Wall -I./
 PREFIX   =  /usr/local
 
index 780eff360778b71a3fbb2702ea951794d2f04529..2d91c62d73ac01cfd864062b44955a9cd350658d 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "nsub.h"
 #include "utils/array.h"
+#include "utils/utils.h"
 
 /* Public */
 
@@ -69,7 +70,7 @@ void song_add_unknown(song_t *song, char *text) {
        lyric.start = 0;
        lyric.stop = 0;
        lyric.name = NULL;
-       lyric.text = text ? strdup(text) : NULL;
+       lyric.text = text ? utils_strdup(text) : NULL;
        array_add(song->lyrics, &lyric);
 }
 
@@ -91,7 +92,7 @@ void song_add_comment(song_t *song, char *comment) {
        lyric.start = 0;
        lyric.stop = 0;
        lyric.name = NULL;
-       lyric.text = comment ? strdup(comment) : NULL;
+       lyric.text = comment ? utils_strdup(comment) : NULL;
        array_add(song->lyrics, &lyric);
 }
 
@@ -103,15 +104,15 @@ void song_add_lyric(song_t *song, int start, int stop, char *name, char *text) {
        lyric.num = song->current_num;
        lyric.start = start;
        lyric.stop = stop;
-       lyric.name = name ? strdup(name) : NULL;
-       lyric.text = text ? strdup(text) : NULL;
+       lyric.name = name ? utils_strdup(name) : NULL;
+       lyric.text = text ? utils_strdup(text) : NULL;
        array_add(song->lyrics, &lyric);
 }
 
 void song_add_meta(song_t *song, char *key, char *value) {
        meta_t meta;
-       meta.key = key ? strdup(key) : NULL;
-       meta.value = value ? strdup(value) : NULL;
+       meta.key = key ? utils_strdup(key) : NULL;
+       meta.value = value ? utils_strdup(value) : NULL;
        array_add(song->metas, &meta);
 }
 
@@ -158,4 +159,3 @@ int nsub_write(FILE *out, song_t *song, NSUB_FORMAT fmt, int apply_offset) {
                return 0;
        }
 }
-
index 7d7f38998e66e1f4556d930856cd78aa06a4ad79..b9b9d0830161a41efad72aff6c310a8eb211cca3 100644 (file)
@@ -20,6 +20,7 @@
 #include <string.h>
 
 #include "nsub.h"
+#include "utils/utils.h"
 
 /* Declarations */
 
@@ -86,7 +87,7 @@ int nsub_read_lrc(song_t *song, char *line) {
                line[colon] = '\0';
                line[end] = '\0';
                if (!strcmp("language", line + 1)) {
-                       song->lang = strdup(line + text_offset);
+                       song->lang = utils_strdup(line + text_offset);
                } else {
                        song_add_meta(song, line + 1, line + text_offset);
                }
@@ -210,7 +211,7 @@ int lrc_millisec(char *line) {
        }
 
        int imult = 0;
-       for (ssize_t i = end; i >= 0; i--) {
+       for (size_t i = end; 1; i--) {
                char car = line[i];
                int digit = (car >= '0' && car <= '9');
 
@@ -219,6 +220,9 @@ int lrc_millisec(char *line) {
 
                if (car == '-')
                        total = -total;
+
+               if (i == 0)
+                       break;
        }
 
        return total;
index 704b2e2fbd2e6ab37b387dbc2c6117d03b2ed6c9..cb38ddd6db1bff3252be03b810be2884120fa207 100644 (file)
@@ -131,7 +131,7 @@ char *nsub_lrc_time_str(int time, int show_sign) {
        if (h) {
                sprintf(time_str, "%s%d:%02d:%02d.%02d", sign, h, m, s, c);
        } else {
-               sprintf(time_str, "%s%d:%02d.%02d", sign, m, s, c);
+               sprintf(time_str, "%s%02d:%02d.%02d", sign, m, s, c);
        }
 
        return time_str;
index c94ba7c58ecaac07d9cc27a72d4063ecdbf8dbda..b856776a636cdf926e2c1bf89538732a846fe2ed 100644 (file)
@@ -117,7 +117,7 @@ char *nsub_webvtt_time_str(int time, int show_sign) {
        if (h) {
                sprintf(time_str, "%s%d:%02d:%02d.%03d", sign, h, m, s, c);
        } else {
-               sprintf(time_str, "%s%d:%02d.%03d", sign, m, s, c);
+               sprintf(time_str, "%s%02d:%02d.%03d", sign, m, s, c);
        }
 
        return time_str;
index 624473b9c3281ad7effd098e0a7f82fe64099bf2..7cf29f1ec96017d1d5d4de0f744f32e6a488dc12 100644 (file)
@@ -1,4 +1,4 @@
-CFLAGS   += -Wall -I./
+CFLAGS   += -Wall -I./ -std=c99
 CXXFLAGS += -Wall -I./
 PREFIX   =  /usr/local
 
index 33be88219fdca5b40fd363c57796e7536402eaf0..77c75f8dddd85081fb13aaecd21a33877d634d90 100644 (file)
@@ -24,7 +24,6 @@
 
 #include "array.h"
 
-typedef struct array_p array;
 struct array_p {
        size_t elem_size;
        size_t count;
index 7e6022eaa8c48866be1c85aa6f4bb19cef7c7a45..eee8c9fc5157183dc76b1b0d3b0755568e476cc4 100644 (file)
@@ -25,8 +25,7 @@
 #include <string.h>
 #include <strings.h>
 
-#include "array.h"
-#include "desktop.h"
+#include "utils.h"
 
 #define EXT "desktop"
 
@@ -55,7 +54,7 @@ desktop *new_desktop(const char filename[], int best_size) {
        me->id = 0;
 
        // Copy name
-       me->name = strdup(filename);
+       me->name = utils_strdup(filename);
 
        // Get extension an remove ".desktop" from name
        char *ext = rindex(me->name, '.');
@@ -66,7 +65,7 @@ desktop *new_desktop(const char filename[], int best_size) {
                        me->name[idot] = '\0';
        }
        if (ext)
-               ext = strdup(ext);
+               ext = utils_strdup(ext);
 
        // If PNG of the same name, use as default icon
        me->icon_file = desktop_find_icon(me->name, best_size);
@@ -78,7 +77,7 @@ desktop *new_desktop(const char filename[], int best_size) {
                slash = rindex(me->name, '/');
        }
        if (slash) {
-               char *copy = strdup(slash + 1);
+               char *copy = utils_strdup(slash + 1);
                free(me->name);
                me->name = copy;
        }
@@ -143,14 +142,14 @@ desktop *new_desktop(const char filename[], int best_size) {
                n = strlen(startsWith);
                if (!strncmp(line, startsWith, n)) {
                        free(me->name);
-                       me->name = strdup(line + n);
+                       me->name = utils_strdup(line + n);
                }
 
                startsWith = "Exec=";
                n = strlen(startsWith);
                if (!strncmp(line, startsWith, n)) {
                        free(me->exec);
-                       me->exec = strdup(line + n);
+                       me->exec = utils_strdup(line + n);
                        // TODO: %f %F %u %U %i %c %k: inject values instead
                        char *cars = "ifFuUck";
                        for (char *ptr = index(me->exec, '%'); ptr; ptr = index(ptr, '%')) {
@@ -166,7 +165,7 @@ desktop *new_desktop(const char filename[], int best_size) {
                n = strlen(startsWith);
                if (!strncmp(line, startsWith, n)) {
                        free(me->icon);
-                       me->icon = strdup(line + n);
+                       me->icon = utils_strdup(line + n);
                }
        }
 
@@ -355,17 +354,17 @@ char *desktop_find_icon(const char basename[], int icon_size) {
                        if (!strncmp(line, startsWith, n)) {
                                free(theme);
                                if (line[n] == '"') {
-                                       theme = strdup(line + n + 1);
+                                       theme = utils_strdup(line + n + 1);
                                        theme[strlen(theme) - 1] = '\0';
                                } else {
-                                       theme = strdup(line + n);
+                                       theme = utils_strdup(line + n);
                                }
                        }
                }
 
                if (!theme || !theme[0]) {
-                       theme = strdup("");
-                       ltheme = strdup("");
+                       theme = utils_strdup("");
+                       ltheme = utils_strdup("");
                } else {
                        tmp = theme;
                        theme = desktop_concat("/usr/share/icons/", tmp, "/", NULL);
@@ -381,7 +380,7 @@ char *desktop_find_icon(const char basename[], int icon_size) {
                return NULL;
 
        // exact match
-       tmp = strdup(basename);
+       tmp = utils_strdup(basename);
        if (desktop_test_file(tmp))
                return tmp;
        free(tmp);
diff --git a/src/utils/utils.c b/src/utils/utils.c
new file mode 100644 (file)
index 0000000..4bdeb6d
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * CUtils: some small C utilities
+ *
+ * Copyright (C) 2022 Niki Roo
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <string.h>
+
+#include "utils.h"
+
+char *utils_strdup(const char *source) {
+       size_t sz = strlen(source);
+       char *new = malloc((sz + 1) * sizeof(char));
+       strcpy(new, source);
+       return new;
+}
index 1fd1e46994d4654b0dac1d1384cca2f0d044fee3..09be6441bb8d015913c7f7f07e50f51bd3e9f09b 100644 (file)
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
 
+/**
+ * @file utils.h
+ * @author Niki
+ * @date 2020
+ *
+ * @brief Include all the other .H as well as a C99-compatible utils_strdup function.
+ */
 #ifndef UTILS_H
 #define UTILS_H
 
-#include "../../nsub/utils/array.h"
-#include "../../nsub/utils/desktop.h"
-#include "../../nsub/utils/print.h"
-#include "../../nsub/utils/timing.h"
+#include "array.h"
+#include "desktop.h"
+#include "print.h"
+#include "timing.h"
+
+/**
+ * A C99-compatible strdup function.
+ *
+ * @param source the source string to copy
+ *
+ * @return a new string (malloc'ed) which your are now respionsible of
+ */
+char *utils_strdup(const char *source);
 
 #endif // UTILS_H