aboutsummarylogtreecommitdiffstats
diff options
context:
space:
mode:
authorAntony Kellermann2018-08-04 02:12:51 -0400
committerAntony Kellermann2018-08-04 02:12:51 -0400
commit22539aa6a4286102216225d96b09411447ec40ba (patch)
tree550ca493f0fd54f92aa5270f3a592a0d90843316
parent72d9d671d9b6b0bb0be477a3ccd59ec3b2d4ae3b (diff)
downloadaur-22539aa6a4286102216225d96b09411447ec40ba.tar.gz
Added csv utils
-rw-r--r--utils.c45
-rw-r--r--utils.h32
2 files changed, 77 insertions, 0 deletions
diff --git a/utils.c b/utils.c
index 1acd0e52509a..7618fcab13a1 100644
--- a/utils.c
+++ b/utils.c
@@ -112,4 +112,49 @@ int is_str_number(const char* string) {
return 0;
return 1;
+}
+
+double csv_read_next_double(String* pString, size_t* idx) {
+ char string[32];
+ size_t i, j = 0;
+ for (i = *idx; pString->data[i] != ',' && pString->data[i] != '\n' && i < pString->len; i++,
+ j++)
+ string[j] = pString->data[i];
+
+ string[j] = '\0';
+ *idx = i + 1;
+ return strtod(string, NULL);
+}
+
+int csv_goto_next_line(String* pString, size_t* idx) {
+ size_t i;
+ for (i = *idx; i < pString->len; i++) {
+ if (pString->data[i] == '\n') {
+ *idx = i + 1;
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+int csv_goto_next_value(String* pString, size_t* idx) {
+ size_t i;
+ for (i = *idx; i < pString->len; i++) {
+ if (pString->data[i] == ',') {
+ *idx = i + 1;
+ return 1;
+ }
+ }
+
+ return 0;
+}
+
+size_t string_get_num_lines(String* pString) {
+ size_t lines = 0, i = 0;
+ while (i++ < pString->len)
+ if (pString->data[i] == '\n')
+ lines++;
+
+ return lines;
} \ No newline at end of file
diff --git a/utils.h b/utils.h
index b30432a844a0..60ad747ba5e4 100644
--- a/utils.h
+++ b/utils.h
@@ -118,4 +118,36 @@ int is_string_json_array(const String* pString);
*/
int is_str_number(const char* string);
+/**
+ * Starting at pString->data[*idx], returns the next double and advances idx to after the next
+ * comma or newline
+ * @param pString String*
+ * @param idx index
+ * @return value as double
+ */
+double csv_read_next_double(String* pString, size_t* idx);
+
+/**
+ * Advances idx past the next newline in pString
+ * @param pString String*
+ * @param idx index
+ * @return 1 if success, 0 if encountered end of string
+ */
+int csv_goto_next_line(String* pString, size_t* idx);
+
+/**
+ * Advances idx to the start of the next value
+ * @param pString String*
+ * @param idx index
+ * @return 1 if success, 0 if encountered end of string
+ */
+int csv_goto_next_value(String* pString, size_t* idx);
+
+/**
+ * Returns the number of lines in a String
+ * @param pString String*
+ * @return size_t number of lines
+ */
+size_t string_get_num_lines(String* pString);
+
#endif \ No newline at end of file