| summaryrefslogtreecommitdiff | 
diff options
Diffstat (limited to 'src/tool')
| -rw-r--r-- | src/tool/CMakeLists.txt | 8 | ||||
| -rw-r--r-- | src/tool/sorted_list.c | 80 | ||||
| -rw-r--r-- | src/tool/sorted_list.h | 19 | ||||
| -rw-r--r-- | src/tool/strings.c | 300 | ||||
| -rw-r--r-- | src/tool/strings.h | 19 | ||||
| -rw-r--r-- | src/tool/strings_types.h | 15 | 
6 files changed, 0 insertions, 441 deletions
| diff --git a/src/tool/CMakeLists.txt b/src/tool/CMakeLists.txt deleted file mode 100644 index 3ad122b..0000000 --- a/src/tool/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -set( -   SRC_FILES ${SRC_FILES} -   ${CMAKE_CURRENT_SOURCE_DIR}/strings.c -   ${CMAKE_CURRENT_SOURCE_DIR}/sorted_list.c -) - -set(SRC_FILES ${SRC_FILES} PARENT_SCOPE) - diff --git a/src/tool/sorted_list.c b/src/tool/sorted_list.c deleted file mode 100644 index 5252c9e..0000000 --- a/src/tool/sorted_list.c +++ /dev/null @@ -1,80 +0,0 @@ -#include "./sorted_list.h" - -int ZoO_sorted_list_index_of -( -   ZoO_index const list_length, -   const void * const sorted_list, -   const void * const elem, -   size_t const type_size, -   int (*compare) (const void *, const void *, const void *), -   const void * const other, -   ZoO_index result [const restrict static 1] -) -{ -   int cmp; -   ZoO_index i, current_min, current_max; -   const char * sorted_list_access; - -   sorted_list_access = (char *) sorted_list; - -   /* This is a binary search. */ - -   if (list_length == 0) -   { -      *result = 0; - -      return -1; -   } - -   current_min = 0; - -   current_max = (list_length - 1); - -   for (;;) -   { -      /* FIXME: overflow-safe? */ -      /* No: (and (> current_min (/ Max 2)) (> current_max (/ Max 2))) */ -      i = ((current_min + current_max) / 2); - -      if (i == list_length) -      { -         /* FIXME: I don't see how this one can be true */ -         *result = list_length; - -         return -1; -      } - -      cmp = compare(elem, (sorted_list_access + (i * type_size)), other); - -      if (cmp > 0) -      { -         if ((current_min > current_max)) -         { -            *result = (i + 1); - -            return -1; -         } - -         /* FIXME: overflow-safe? */ -         current_min = (i + 1); -      } -      else if (cmp < 0) -      { -         if ((current_min > current_max) || (i == 0)) -         { -            *result = i; - -            return -1; -         } - -         /* overflow-safe */ -         current_max = (i - 1); -      } -      else -      { -         *result = i; - -         return 0; -      } -   } -} diff --git a/src/tool/sorted_list.h b/src/tool/sorted_list.h deleted file mode 100644 index 72bd893..0000000 --- a/src/tool/sorted_list.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef _ZoO_TOOL_SORTED_LIST_H_ -#define _ZoO_TOOL_SORTED_LIST_H_ - -#include <stdlib.h> - -#include "../pervasive.h" - -int ZoO_sorted_list_index_of -( -   ZoO_index const list_length, -   const void * const sorted_list, -   const void * const elem, -   size_t const type_size, -   int (*compare) (const void *, const void *, const void *), -   const void * const other, -   ZoO_index result [const restrict static 1] -); - -#endif diff --git a/src/tool/strings.c b/src/tool/strings.c deleted file mode 100644 index 73a5cdd..0000000 --- a/src/tool/strings.c +++ /dev/null @@ -1,300 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include <stdlib.h> -#include <string.h> -#include <stdint.h> /* defines SIZE_MAX */ - -#include "../io/error.h" - -#include "strings.h" - - -void ZoO_strings_initialize (struct ZoO_strings s [const restrict static 1]) -{ -   s->words_count = 0; -   s->words = (ZoO_char **) NULL; -   s->word_sizes = (size_t *) NULL; -} - -void ZoO_strings_finalize (struct ZoO_strings s [const restrict static 1]) -{ -   if (s->words_count != 0) -   { -      ZoO_index i; - -      for (i = 0; i < s->words_count; ++i) -      { -         free((void *) s->words[i]); -      } - -      s->words_count = 0; - -      free((void *) s->words); -      free((void *) s->word_sizes); - -      s->words = (ZoO_char **) NULL; -      s->word_sizes = (size_t *) NULL; -   } -} - -static int add_word -( -   struct ZoO_strings s [const restrict static 1], -   size_t const line_size, -   const ZoO_char line [const restrict static line_size] -) -{ -   size_t * new_s_word_sizes; -   ZoO_char * new_word, ** new_s_words; - -   if (s->words_count == ZoO_INDEX_MAX) -   { -      ZoO_S_WARNING("Data input sentence has too many words."); - -      return -1; -   } - -   /* overflow-safe, as line_size < SIZE_MAX */ -   new_word = (ZoO_char *) calloc((line_size + 1), sizeof(ZoO_char)); - -   if (new_word == (ZoO_char *) NULL) -   { -      ZoO_S_WARNING("Unable to allocate memory to extract new word."); - -      return -1; -   } - -   memcpy((void *) new_word, (const void *) line, line_size); - -   new_word[line_size] = '\0'; - -   new_s_words = -      (ZoO_char **) realloc -      ( -         (void *) s->words, -         /* XXX: (sizeof() * _) assumed overflow-safe. */ -         /* (di->words_count + 1) overflow-safe */ -         (sizeof(ZoO_char *) * (s->words_count + 1)) -      ); - -   if (new_s_words == (ZoO_char **) NULL) -   { -      ZoO_S_WARNING("Unable to reallocate memory to extract new word."); - -      free((void *) new_word); - -      return -1; -   } - -   s->words = new_s_words; - -   new_s_word_sizes = -      (size_t *) realloc -      ( -         (void *) s->word_sizes, -         /* XXX: (sizeof() * _) assumed overflow-safe. */ -         /* (di->words_count + 1) overflow-safe */ -         (sizeof(size_t) * (s->words_count + 1)) -      ); - -   if (new_s_word_sizes == (size_t *) NULL) -   { -      ZoO_S_WARNING("Unable to reallocate memory to extract new word."); - -      free((void *) new_word); - -      return -1; -   } - -   s->word_sizes = new_s_word_sizes; - -   s->words[s->words_count] = new_word; -   s->word_sizes[s->words_count] = (line_size + 1); - -   s->words_count += 1; - -   return 0; -} - -static int parse_word -( -   struct ZoO_strings s [const restrict static 1], -   ZoO_index const punctuations_count, -   const ZoO_char punctuations [const restrict static punctuations_count], -   size_t const line_size, -   ZoO_char line [const static line_size] -) -{ -   ZoO_index j; - -   if (line_size == 0) -   { -      return 0; -   } - -   for (j = 0; j < line_size; ++j) -   { -      switch (line[j]) -      { -         case 'A': -         case 'B': -         case 'C': -         case 'D': -         case 'E': -         case 'F': -         case 'G': -         case 'H': -         case 'I': -         case 'J': -         case 'K': -         case 'L': -         case 'M': -         case 'N': -         case 'O': -         case 'P': -         case 'Q': -         case 'R': -         case 'S': -         case 'T': -         case 'U': -         case 'V': -         case 'W': -         case 'X': -         case 'Y': -         case 'Z': -            line[j] = 'z' - ('Z' - line[j]); -            break; - -         default: -            break; -      } -   } - -   for (j = 0; j < punctuations_count; ++j) -   { -      /* overflow-safe: line_size > 1 */ -      if (line[line_size - 1] == punctuations[j]) -      { -         if (line_size > 1) -         { -            if -            ( -               /* overflow-safe: line_size > 1 */ -               (add_word(s, (line_size - 1), line) < 0) -               /* overflow-safe: line_size > 1 */ -               /* prevents line[restrict] */ -               || (add_word(s, 1, (line + (line_size - 1))) < 0) -            ) -            { -               return -1; -            } - -            return 0; -         } -      } -   } - -   return add_word(s, line_size, line); -} - -int ZoO_strings_parse -( -   struct ZoO_strings s [const restrict static 1], -   size_t input_size, -   ZoO_char input [const restrict], -   ZoO_index const punctuations_count, -   const ZoO_char punctuations [const restrict static punctuations_count] -) -{ -   size_t i, w_start; - -   ZoO_strings_finalize(s); - -   if (input == NULL) -   { -      return 0; -   } - -   i = 0; - -   /* overflow-safe: input is '\0' terminated. */ -   while (input[i] == ' ') -   { -      ++i; -   } - -   w_start = i; - -   if (input[i] == '\001') -   { -      /* This is an CTCP command. */ -      /* We'll remove the trailing '\001' so that only the first word */ -      /* indicates the need for CTCP (uppercase) syntax. */ - -      if ((input_size >= 1) && (input[input_size - 1] == '\001')) -      { -         input[input_size - 1] = ' '; -      } -      else -      { -         ZoO_WARNING -         ( -            "CTCP sequence '%s' did not end with a \\001 character.", -            input -         ); -      } -   } - -   for (; i < input_size; ++i) -   { -      if (input[i] == ' ') -      { -         if -         ( -            parse_word -            ( -               s, -               punctuations_count, -               punctuations, -               /* overflow-safe: w_start < i */ -               (i - w_start), -               (input + w_start) -            ) < 0 -         ) -         { -            ZoO_strings_finalize(s); - -            return -1; -         } - -         ++i; - -         /* safe, as input is terminated by '\0' */ -         while (input[i] == ' ') -         { -            ++i; -         } - -         w_start = i; -      } -   } - -   if -   ( -      parse_word -      ( -         s, -         punctuations_count, -         punctuations, -         /* overflow-safe: w_start =< i */ -         (i - w_start), -         (input + w_start) -      ) < 0 -   ) -   { -      ZoO_strings_finalize(s); - -      return -1; -   } - -   return 0; -} diff --git a/src/tool/strings.h b/src/tool/strings.h deleted file mode 100644 index 6fbecf7..0000000 --- a/src/tool/strings.h +++ /dev/null @@ -1,19 +0,0 @@ -#ifndef _ZoO_TOOL_STRINGS_H_ -#define _ZoO_TOOL_STRINGS_H_ - -#include "strings_types.h" - -void ZoO_strings_initialize (struct ZoO_strings s [const restrict static 1]); - -void ZoO_strings_finalize (struct ZoO_strings s [const restrict static 1]); - -int ZoO_strings_parse -( -   struct ZoO_strings s [const static 1], -   size_t input_size, -   ZoO_char input [const restrict], -   ZoO_index const punctuations_count, -   const ZoO_char punctuations [const restrict static punctuations_count] -); - -#endif diff --git a/src/tool/strings_types.h b/src/tool/strings_types.h deleted file mode 100644 index f74dcc8..0000000 --- a/src/tool/strings_types.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef _ZoO_TOOL_STRINGS_TYPES_H_ -#define _ZoO_TOOL_STRINGS_TYPES_H_ - -#include <stdio.h> - -#include "../pervasive.h" - -struct ZoO_strings -{ -   ZoO_index words_count; -   ZoO_char * restrict * restrict words; -   size_t * restrict word_sizes; -}; - -#endif | 


