home *** CD-ROM | disk | FTP | other *** search
/ APDL Public Domain 1 / APDL_PD1A.iso / graphics / conversion / randjpeg / source / c / strings < prev   
Encoding:
Text File  |  1995-12-27  |  4.8 KB  |  254 lines

  1. /* strings.c
  2.  * MACHINE:     RISC OS 3.60
  3.  * LANGUAGE:    Acorn C v5.06
  4.  * LIBRARIES:   OSLib
  5.  * AUTHOR:      Cy Booker <cy@cheepnis.demon.co.uk>
  6.  * LICENCE:     Freeware, copyright 1995
  7.  */
  8.  
  9. #include "strings.h"
  10.  
  11.  
  12. #include <assert.h>
  13. #include <limits.h>
  14. #include <stddef.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19.  
  20. #include "OS:osfile.h"
  21. #include "OS:territory.h"
  22.  
  23.  
  24. #include "main.h"
  25.  
  26.  
  27. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  28.  */
  29.  
  30. #define EOL     "\x0a\x0d"
  31.  
  32.  
  33.  
  34. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  35.  */
  36.  
  37. extern void strings_read(
  38.                 strings         *s,
  39.                 const char      *filename) {
  40.   int                           file_size;
  41.   fileswitch_object_type        type;
  42.   char                          *file_buffer;
  43.   char                          *rove;
  44.   assert(s);
  45.   assert(filename);
  46.  
  47.   assert(s);
  48.   assert(filename);
  49.   type = osfile_read_stamped(filename, NULL, NULL, &file_size, NULL, NULL);
  50.   if (type == osfile_NOT_FOUND) {
  51.     /* cope with history file not existing */
  52.     return;
  53.   }
  54.   if (type != osfile_IS_FILE) {
  55.     osfile_make_error(filename, type);
  56.   }
  57.  
  58.   file_buffer = xmalloc(file_size+1);
  59.   osfile_load_stamped(filename, (byte *)file_buffer, NULL, NULL, NULL, NULL);
  60.   file_buffer[file_size] = '\0';
  61.  
  62.   for (rove= strtok(file_buffer, EOL); (rove); rove = strtok(NULL, EOL)) {
  63.     strings_add(s, rove);
  64.   }
  65.   free(file_buffer);
  66. }
  67.  
  68.  
  69.  
  70. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  71.  */
  72.  
  73. extern void strings_write(
  74.                 const strings   *s,
  75.                 const char      *filename) {
  76.   FILE          *file;
  77.   node          *node;
  78.   unsigned int  i;
  79.  
  80.   assert(s);
  81.   assert(filename);
  82.  
  83.   assert(s);
  84.   assert(filename);
  85.  
  86.   file = fopen(filename, "w");
  87.   assert(file);
  88.  
  89.   node = s->head;
  90.   for (i= s->count; (i > 0); i--) {
  91.     assert(node);
  92.     fputs(node->text, file);
  93.     fputs("\n", file);
  94.     node = node->next;
  95.   }
  96.   fclose(file);
  97. }
  98.  
  99.  
  100.  
  101. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  102.  */
  103.  
  104. extern void strings_clear(
  105.                 strings *s) {
  106.   unsigned int  i;
  107.   node          *next;
  108.   node          *node;
  109.  
  110.   assert(s);
  111.  
  112.   node = s->head;
  113.   for (i= s->count; (i > 0); i--) {
  114.     assert(node);
  115.     next = node->next;
  116.     free(node);
  117.     node = next;
  118.   }
  119.   s->count = 0;
  120.   s->head = NULL;
  121.   s->tail = NULL;
  122. }
  123.  
  124.  
  125. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  126.  */
  127.  
  128. extern void strings_add(
  129.                 strings         *s,
  130.                 const char      *text) {
  131.   node  *n;
  132.  
  133.   assert(s);
  134.   assert(text);
  135.  
  136.   n = xmalloc(offsetof(node, text) + strlen(text) + 1);
  137.   n->next = NULL;
  138.   strcpy(n->text, text);
  139.   strings_insert(s, n);
  140.  
  141. }
  142.  
  143.  
  144. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  145.  */
  146.  
  147. extern node *strings_find(
  148.                 const strings   *s,
  149.                 const char      *text) {
  150.   node          *node;
  151.   unsigned int  i;
  152.  
  153.   assert(s);
  154.   assert(text);
  155.  
  156.   node = s->head;
  157.   for (i= s->count; (i > 0); i--) {
  158.     assert(node);
  159.     if (territory_collate(
  160.                 territory_CURRENT,
  161.                 text,
  162.                 node->text,
  163.                 territory_IGNORE_CASE) == 0) {
  164.       return node;
  165.     }
  166.     node = node->next;
  167.   }
  168.   return NULL;
  169. }
  170.  
  171. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  172.  */
  173.  
  174. extern node *strings_get(
  175.                 const strings   *s,
  176.                 unsigned int    index) {
  177.   node          *node;
  178.   unsigned int  i;
  179.  
  180.   assert(s);
  181.   assert(index < s->count);
  182.  
  183.   node = s->head;
  184.   for (i= 0; (i < index); i++) {
  185.     assert(node);
  186.     node = node->next;
  187.   }
  188.   assert(node);
  189.   return node;
  190. }
  191.  
  192.  
  193.  
  194. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  195.  */
  196.  
  197. extern void strings_insert(
  198.                 strings         *s,
  199.                 node            *node) {
  200.   assert(s);
  201.   assert(node);
  202.   assert(node->next == NULL);
  203.  
  204.   if (s->tail) {
  205.     assert(s->count > 0);
  206.     assert(s->head);
  207.     s->tail->next = node;
  208.   } else {
  209.     s->head = node;
  210.   }
  211.   s->tail = node;
  212.   s->count++;
  213. }
  214.  
  215.  
  216.  
  217. /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  218.  */
  219.  
  220. extern void strings_remove(
  221.                 strings         *s,
  222.                 node            *n) {
  223.   node          *parent;
  224.   unsigned int  i;
  225.  
  226.   assert(s);
  227.   assert(n);
  228.  
  229.   if (n == s->head) {
  230.     s->head = n->next;
  231.     if (s->head == NULL) {
  232.       s->tail = NULL;
  233.     }
  234.   } else {
  235.     parent = s->head;
  236.     for (i= s->count; (i > 1); i--) {
  237.       assert(parent->next);
  238.       if (parent->next == n) {
  239.         break;
  240.       }
  241.       parent = parent->next;
  242.     }
  243.     assert(parent->next == n);
  244.     parent->next = n->next;
  245.     if (parent->next == NULL) {
  246.       s->tail = parent;
  247.     }
  248.   }
  249.   s->count--;
  250.   n->next = NULL;
  251. }
  252.  
  253.  
  254.