-CFLAGS += -Wall -I./
+CFLAGS += -Wall -I./ -std=c99
CXXFLAGS += -Wall -I./
PREFIX = /usr/local
#include "nsub.h"
#include "utils/array.h"
+#include "utils/utils.h"
/* Public */
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);
}
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);
}
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);
}
return 0;
}
}
-
#include <string.h>
#include "nsub.h"
+#include "utils/utils.h"
/* Declarations */
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);
}
}
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');
if (car == '-')
total = -total;
+
+ if (i == 0)
+ break;
}
return total;
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;
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;
-CFLAGS += -Wall -I./
+CFLAGS += -Wall -I./ -std=c99
CXXFLAGS += -Wall -I./
PREFIX = /usr/local
#include "array.h"
-typedef struct array_p array;
struct array_p {
size_t elem_size;
size_t count;
#include <string.h>
#include <strings.h>
-#include "array.h"
-#include "desktop.h"
+#include "utils.h"
#define EXT "desktop"
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, '.');
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);
slash = rindex(me->name, '/');
}
if (slash) {
- char *copy = strdup(slash + 1);
+ char *copy = utils_strdup(slash + 1);
free(me->name);
me->name = copy;
}
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, '%')) {
n = strlen(startsWith);
if (!strncmp(line, startsWith, n)) {
free(me->icon);
- me->icon = strdup(line + n);
+ me->icon = utils_strdup(line + n);
}
}
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);
return NULL;
// exact match
- tmp = strdup(basename);
+ tmp = utils_strdup(basename);
if (desktop_test_file(tmp))
return tmp;
free(tmp);
--- /dev/null
+/*
+ * 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;
+}
* 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