aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
authorThomas White <taw@bitwiz.me.uk>2018-10-28 00:03:51 +0200
committerThomas White <taw@bitwiz.me.uk>2018-10-28 00:03:51 +0200
commit8e05062ad3b94068825a1f80eb362aa3d3e9098f (patch)
tree77944d65437fa57755a7bc8bc16fd3e2c5b43f17 /src/utils.c
parent791ee4e65f3ec7ef470302e4381baefb431fb56d (diff)
parent21418cfb52b87a32cfdc9bb14be83b85a8e70281 (diff)
Merge branch 'json-stylesheets'
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c92
1 files changed, 16 insertions, 76 deletions
diff --git a/src/utils.c b/src/utils.c
index 9033466..277b3f1 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -54,90 +54,30 @@ int safe_strcmp(const char *a, const char *b)
}
-static char *fgets_long(FILE *fh, size_t *lp)
+int parse_double(const char *a, float v[2])
{
- char *line;
- size_t la;
- size_t l = 0;
-
- la = 1024;
- line = malloc(la);
- if ( line == NULL ) return NULL;
-
- do {
-
- int r;
-
- r = fgetc(fh);
- if ( r == EOF ) {
- if ( l == 0 ) {
- free(line);
- *lp = 0;
- return NULL;
- } else {
- line[l++] = '\0';
- *lp = l;
- return line;
- }
- }
-
- line[l++] = r;
-
- if ( r == '\n' ) {
- line[l++] = '\0';
- *lp = l;
- return line;
- }
-
- if ( l == la ) {
+ int nn;
- char *ln;
-
- la += 1024;
- ln = realloc(line, la);
- if ( ln == NULL ) {
- free(line);
- *lp = 0;
- return NULL;
- }
-
- line = ln;
-
- }
+ nn = sscanf(a, "%fx%f", &v[0], &v[1]);
+ if ( nn != 2 ) {
+ fprintf(stderr, _("Invalid size '%s'\n"), a);
+ return 1;
+ }
- } while ( 1 );
+ return 0;
}
-char *load_everything(const char *filename)
+int parse_tuple(const char *a, float v[4])
{
- FILE *fh;
- size_t el = 1;
- char *everything = strdup("");
-
- fh = fopen(filename, "r");
- if ( fh == NULL ) return NULL;
-
- while ( !feof(fh) ) {
-
- size_t len = 0;
- char *line = fgets_long(fh, &len);
-
- if ( line != NULL ) {
-
- everything = realloc(everything, el+len);
- if ( everything == NULL ) {
- fprintf(stderr, _("Failed to allocate memory\n"));
- return NULL;
- }
- el += len;
-
- strcat(everything, line);
- }
+ int nn;
+ nn = sscanf(a, "%f,%f,%f,%f", &v[0], &v[1], &v[2], &v[3]);
+ if ( nn != 4 ) {
+ fprintf(stderr, _("Invalid tuple '%s'\n"), a);
+ return 1;
}
- fclose(fh);
-
- return everything;
+ return 0;
}
+