commit 23f8d73b56802f07152fa9cbe558d31bd09fd313
parent 40932ba184f3c2ab60ed913f45383590295195c7
Author: Eduardo Fontana Lazzari <edufonlaz@gmail.com>
Date: Tue, 28 Jan 2025 10:40:38 +0100
Change sphin_config so the library compiles
Also, made a local copy of each line while reading the input file, so
strtok_r doesnt change it and one can print the original line, for
instance in an error message.
Replaced the trim_keyword function so it can be used everywhere in the
library.
Diffstat:
2 files changed, 71 insertions(+), 25 deletions(-)
diff --git a/src/sphin_config.c b/src/sphin_config.c
@@ -21,13 +21,16 @@
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
+#define _POSIX_C_SOURCE 200112L /* for strtok_r support */
#include "sphin.h"
#include "sphin_c.h"
+#include "sphin_config.h"
#include <rsys/cstr.h> /* res_to_cstr */
#include <rsys/ref_count.h> /* ref_T */
-#include <rsys/text_reader.h>
+#include <rsys/str.h> /* struct str */
+#include <rsys/text_reader.h> /* struct txtrdr */
#include <errno.h>
#include <stdio.h> /* fopen, fclose */
@@ -43,23 +46,6 @@ struct sphin_config {
/*******************************************************************************
* Helper functions
******************************************************************************/
-static char*
-trim_keyword
- (char* str)
-{
- char* token_ptr = NULL;
- char* token = NULL;
- char* remaining_token = NULL;
-
- token = strtok_r(str, " ", &token_ptr);
- if (NULL == token) return NULL;
-
- remaining_token = strtok_r(NULL, " ", &token_ptr);
- if (NULL != remaining_token) return NULL;
-
- return token;
-}
-
static res_T
config_create
(struct sphin* sphin,
@@ -114,6 +100,7 @@ load_stream
{
struct sphin_config* config = NULL;
struct txtrdr* txtrdr = NULL;
+ struct str line;
char* keyword = NULL;
char* token = NULL;
char* token_ptr = NULL;
@@ -124,6 +111,8 @@ load_stream
ASSERT(NULL != streamname);
ASSERT(NULL != out_config);
+ str_init(sphin->allocator, &line);
+
res = config_create(sphin, &config);
if (res != RES_OK) {
goto error;
@@ -140,26 +129,28 @@ load_stream
}
while (NULL != txtrdr_get_line(txtrdr)) {
- const char* line = txtrdr_get_cline(txtrdr);
- /* TODO copy line in local string */
+ res = str_set(&line, txtrdr_get_cline(txtrdr));
+ if (res != RES_OK) {
+ goto error;
+ }
/* volume : "Mon volume"\0 */
/* volume \0 "Mon volume"\0 */
/* volume\0\0 "Mon volume"\0 */
- token = strtok_r(txtrdr_get_line(txtrdr), ":", &token_ptr);
+ token = strtok_r(str_get(&line), ":", &token_ptr);
keyword = trim_keyword(token);
if (NULL == keyword){
res = RES_BAD_ARG;
}
else if (0 == strcmp(keyword, "volume")){
- res = parse_volume(txtrdr, config) /* Not yet implemented */
+ // res = parse_volume(txtrdr, config) /* Not yet implemented */
}
else if (0 == strcmp(keyword, "source")){
- res = parse_source(txtrdr, config) /* Not yet implemented */
+ // res = parse_source(txtrdr, config) /* Not yet implemented */
}
else if (0 == strcmp(keyword, "surface")){
- res = parse_surface(txtrdr, config) /* Not yet implemented */
+ // res = parse_surface(txtrdr, config) /* Not yet implemented */
}
else {
res = RES_BAD_ARG;
@@ -171,10 +162,10 @@ load_stream
txtrdr_get_name(txtrdr), txtrdr_get_line_num(txtrdr));
goto error;
}
- /* TODO */
}
exit:
+ str_release(&line);
*out_config = config;
if (NULL != txtrdr) {
txtrdr_ref_put(txtrdr);
@@ -193,6 +184,27 @@ error:
}
/*******************************************************************************
+ * Local functions
+ ******************************************************************************/
+char*
+trim_keyword
+ (char* str)
+{
+ char* token_ptr = NULL;
+ char* token = NULL;
+ char* remaining_token = NULL;
+ ASSERT(NULL != str);
+
+ token = strtok_r(str, " \t", &token_ptr);
+ if (NULL == token) return NULL;
+
+ remaining_token = strtok_r(NULL, " \t", &token_ptr);
+ if (NULL != remaining_token) return NULL;
+
+ return token;
+}
+
+/*******************************************************************************
* Exported functions
******************************************************************************/
res_T
diff --git a/src/sphin_config.h b/src/sphin_config.h
@@ -0,0 +1,34 @@
+/* Copyright (C) 2024 Centre National de la Recherche Scientifique
+ * Copyright (C) 2024 Clermont Auvergne INP
+ * Copyright (C) 2024 INSA Lyon
+ * Copyright (C) 2024 Institut Mines Télécom Albi-Carmaux
+ * Copyright (C) 2024 Institut National Polytechnique de Toulouse
+ * Copyright (C) 2024 |Méso|Star> (contact@meso-star.com)
+ * Copyright (C) 2024 PhotonLyX (info@photonlyx.com)
+ * Copyright (C) 2024 Université de Lorraine
+ * Copyright (C) 2024 Université Paul Sabatier
+ * Copyright (C) 2024 Université Toulouse - Jean Jaurès
+ *
+ * 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 <http://www.gnu.org/licenses/>. */
+
+#ifndef SPHIN_CONFIG_H
+#define SPHIN_CONFIG_H
+
+#include <rsys/rsys.h>
+
+extern LOCAL_SYM char*
+trim_keyword
+ (char* str);
+
+#endif /* SPHIN_CONFIG_H */