diff options
| author | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-09-28 20:28:40 +0200 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-09-28 20:28:40 +0200 |
| commit | 485e5ebf340857db07b1c8ecb5c63dcf3a908377 (patch) | |
| tree | ceca7fd0e20e20b4452c2ecc67563546a1e23730 /src/strops.cpp | |
| parent | a2299b0bae21c8f05f091732a78fc250cbd5e016 (diff) | |
move string function out of openAI.cpp into strops.cpp
Diffstat (limited to 'src/strops.cpp')
| -rw-r--r-- | src/strops.cpp | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/strops.cpp b/src/strops.cpp index 2c5f0b9..28f9bcc 100644 --- a/src/strops.cpp +++ b/src/strops.cpp @@ -14,6 +14,7 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#include <stdlib.h> #include <string.h> #include <ctype.h> #include <stdio.h> @@ -107,4 +108,87 @@ void strops_replace_float(char *buf, size_t buf_size, const char *search, float char num_buf[200]; snprintf(num_buf, 200, "%.*f", decimals, number); strops_replace(buf, buf_size, search, num_buf); +} + +char* strops_get_json_value(const char *json, const char *key, char *out, size_t out_size, int skip) +{ + char pattern[128]; + snprintf(pattern, sizeof(pattern), "\"%s\"", key); + const char *pos = strstr(json, pattern); + while(skip > 0) { + pos = strstr(pos+1, pattern); + skip--; + } + if (!pos) return NULL; + pos = strchr(pos, ':'); + if (!pos) return NULL; + pos++; + + // Skip whitespace and quotes + while (*pos == ' ' || *pos == '\"') pos++; + + size_t i = 0; + while (*pos && !(*pos == '\"' && *(pos-1) != '\\') && i < out_size - 1) { + out[i++] = *pos++; + } + out[i] = '\0'; + return out; +} + +char* strops_get_filename(const char* path) +{ + char* filename = (char*)strrchr(path, '/'); // for Unix-style paths + if (filename) return filename + 1; // skip the '/' + filename = (char*)strrchr(path, '\\'); // for Windows-style paths + if (filename) return filename + 1; + return (char*)path; // no slashes found, path itself is filename +} + +char* strops_prep_str_for_json(const char *input, size_t buffer_size) +{ + if (!input) return NULL; + + char *result = (char*)malloc(buffer_size * 2 + 1); + if (!result) return NULL; + + const char *src = input; + char *dst = result; + + while (*src) { + if (*src == '"') { + *dst++ = '\\'; + *dst++ = '"'; + } + else if (*src == '\n') { + // empty + } + else { + *dst++ = *src; + } + src++; + } + *dst = '\0'; + + return result; +} + +char* strops_unprep_str_from_json(char *input) +{ + if (!input) return NULL; + + char *src = input; + char *dst = input; + + while (*src) { + if (*src == '\\' && *(src+1) == '"') { + src++; + } + else if (*src == '\\' && *(src+1) == 'n') { + src++;src++; + } + *dst++ = *src++; + } + *dst = '\0'; + + return input; }
\ No newline at end of file |
