site

A custom static site generator written in C.
git clone https://unitehenry.com/git/site.git
Log | Files | Refs | README

strings.c (3320B)


      1 #include "strings.h"
      2 #include <ctype.h>
      3 #include <stddef.h>
      4 #include <stdlib.h>
      5 #include <string.h>
      6 
      7 StringList *init_string_list(size_t size) {
      8   StringList *string_list = malloc(sizeof(StringList));
      9 
     10   string_list->strings = malloc((size * sizeof(char *)));
     11 
     12   string_list->count = 0;
     13 
     14   string_list->size = size;
     15 
     16   if (size > 0) {
     17     string_list->strings[0] = NULL;
     18   }
     19 
     20   return string_list;
     21 }
     22 
     23 void string_list_append(StringList *string_list, char *string) {
     24   if (string_list->count + 1 > string_list->size) {
     25     string_list->strings =
     26         realloc(string_list->strings, (string_list->size * 2) * sizeof(char *));
     27     string_list->size = string_list->size * 2;
     28   }
     29 
     30   size_t len = strlen(string) + 1;
     31   char *str = malloc(len);
     32 
     33   strcpy(str, string);
     34 
     35   string_list->strings[string_list->count] = str;
     36   string_list->count++;
     37   string_list->strings[string_list->count] = NULL;
     38 }
     39 
     40 char *str_replace(const char *str, const char *old_sub, const char *new_sub) {
     41   size_t old_len = strlen(old_sub);
     42 
     43   if (old_len == 0) {
     44     return strdup(str);
     45   }
     46 
     47   size_t new_len = strlen(new_sub);
     48 
     49   size_t str_len = strlen(str);
     50 
     51   size_t count = 0;
     52 
     53   const char *p = str;
     54 
     55   while ((p = strstr(p, old_sub)) != NULL) {
     56     count++;
     57     p += old_len;
     58   }
     59 
     60   if (count == 0) {
     61     return strdup(str);
     62   }
     63 
     64   size_t new_str_len = str_len + count * (new_len - old_len) + 1;
     65 
     66   char *result = malloc(new_str_len);
     67 
     68   if (!result) {
     69     return NULL;
     70   }
     71 
     72   char *dest = result;
     73 
     74   const char *src = str;
     75 
     76   while (*src) {
     77     if (strstr(src, old_sub) == src) {
     78       strcpy(dest, new_sub);
     79 
     80       dest += new_len;
     81 
     82       src += old_len;
     83     } else {
     84       *dest++ = *src++;
     85     }
     86   }
     87 
     88   *dest = '\0';
     89 
     90   return result;
     91 }
     92 
     93 char *trim_whitespace(const char *str) {
     94   if (!str)
     95     return NULL;
     96 
     97   size_t len = strlen(str);
     98 
     99   if (len == 0)
    100     return strdup("");
    101 
    102   size_t start = 0;
    103 
    104   while (start < len && isspace(str[start]))
    105     start++;
    106 
    107   if (start == len)
    108     return strdup("");
    109 
    110   size_t end = len - 1;
    111 
    112   while (end > start && isspace(str[end]))
    113     end--;
    114 
    115   size_t new_len = end - start + 1;
    116 
    117   char *result = malloc(new_len + 1);
    118 
    119   if (!result)
    120     return NULL;
    121 
    122   memcpy(result, str + start, new_len);
    123 
    124   result[new_len] = '\0';
    125 
    126   return result;
    127 }
    128 
    129 StringMap *string_map_create(void) {
    130   StringMap *map = malloc(sizeof(StringMap));
    131 
    132   if (!map)
    133     return NULL;
    134 
    135   map->head = NULL;
    136 
    137   return map;
    138 }
    139 
    140 void string_map_put(StringMap *map, char *key, char *value) {
    141   StringMapEntry *entry = map->head;
    142 
    143   while (entry) {
    144     if (strcmp(entry->key, key) == 0) {
    145       free(entry->value);
    146       entry->value = strdup(value);
    147       return;
    148     }
    149     entry = entry->next;
    150   }
    151 
    152   StringMapEntry *new_entry = malloc(sizeof(StringMapEntry));
    153 
    154   new_entry->key = strdup(key);
    155   new_entry->value = strdup(value);
    156   new_entry->next = map->head;
    157   map->head = new_entry;
    158 }
    159 
    160 char *string_map_get(StringMap *map, char *key) {
    161   StringMapEntry *entry = map->head;
    162 
    163   while (entry) {
    164     if (strcmp(entry->key, key) == 0) {
    165       return entry->value;
    166     }
    167     entry = entry->next;
    168   }
    169 
    170   return NULL;
    171 }
    172 
    173 bool string_map_contains(StringMap *map, char *key) {
    174   StringMapEntry *entry = map->head;
    175 
    176   while (entry) {
    177     if (strcmp(entry->key, key) == 0) {
    178       return true;
    179     }
    180     entry = entry->next;
    181   }
    182 
    183   return false;
    184 }