home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2306 / StrTable.c < prev    next >
C/C++ Source or Header  |  1990-12-28  |  5KB  |  164 lines

  1. /*
  2.  * Author: Jason Baietto, jason@ssd.csd.harris.com
  3.  * xdiary Copyright 1990 Harris Corporation
  4.  *
  5.  * Permission to use, copy, modify, and distribute, this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided that
  7.  * the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation, and that the name of the copyright holder be used in
  10.  * advertising or publicity pertaining to distribution of the software with
  11.  * specific, written prior permission, and that no fee is charged for further
  12.  * distribution of this software, or any modifications thereof.  The copyright
  13.  * holder makes no representations about the suitability of this software for
  14.  * any purpose.  It is provided "as is" without express or implied warranty.
  15.  *
  16.  * THE COPYRIGHT HOLDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  17.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, AND IN NO
  18.  * EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  19.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM ITS USE,
  20.  * LOSS OF DATA, PROFITS, QPA OR GPA, WHETHER IN AN ACTION OF CONTRACT,
  21.  * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH
  22.  * THE USE OR PERFORMANCE OF THIS SOFTWARE.
  23.  */
  24.  
  25. #include <stdio.h>
  26. #include <X11/StringDefs.h>
  27. #include <X11/IntrinsicP.h>
  28. #include "StrTableP.h"
  29.  
  30. /* Copy a string, replacing two consecutive double quotes with a single. */
  31. static void string_copy(dest_string, source_string, length)
  32. char * dest_string;
  33. char * source_string;
  34. int length;
  35. {
  36.    char * dest_ptr = dest_string;
  37.    char * src_ptr = source_string;
  38.    char * end_ptr = src_ptr + length;
  39.  
  40.    while (src_ptr < end_ptr) {
  41.       if (*src_ptr == QUOTE) {
  42.          src_ptr++;
  43.       }
  44.       *dest_ptr = *src_ptr;
  45.       dest_ptr++;
  46.       src_ptr++;
  47.    }
  48.    
  49.    /* Null terminate the string */
  50.    *dest_ptr = NULL;
  51. }
  52.  
  53.  
  54. /* Convert a string containing quoted strings into a string table.   */
  55. /* Two consecutive quotes inside a "string" become a single quote.   */
  56. /* This algorithm just ignores all characters between each "string". */
  57. /* Not the most robust thing I ever wrote, but it'll do the job.     */
  58.  
  59. /*ARGSUSED*/
  60. void StringTableConverter(args, num_args, fromVal, toVal)
  61. XrmValuePtr args;
  62. Cardinal    *num_args;
  63. XrmValuePtr fromVal;
  64. XrmValuePtr toVal;
  65. {
  66.    static char ** array;
  67.    int num_allocated;
  68.    int token_len;
  69.    char * beg_ptr = (char *) fromVal->addr;
  70.    char * cur_ptr = beg_ptr;
  71.    int token_num = 0;
  72.    int in_token = FALSE;
  73.  
  74.    num_allocated = INIT_SIZE;
  75.    array = (char **) XtMalloc(num_allocated*sizeof(char *));
  76.  
  77.    while (*cur_ptr) {
  78.       if (*cur_ptr == QUOTE) {
  79.          if (in_token) {
  80.             if (*(cur_ptr+sizeof(char)) == QUOTE) {
  81.                /* Two consecutive quotes become one quote. */
  82.                cur_ptr++;
  83.             } else {
  84.                /* Finished with this token, ignore final quote. */
  85.                token_len = cur_ptr - beg_ptr;
  86.                array[token_num] = (char*)XtMalloc((token_len+1)*sizeof(char));
  87.                string_copy(array[token_num], beg_ptr, token_len);
  88.                in_token = FALSE;
  89.                token_num++;
  90.  
  91.                /* Allocate more array space if necessary. */
  92.                if (token_num == num_allocated) {
  93.                   num_allocated += GROW_BY;
  94.                   array = (char**)XtRealloc(array, num_allocated*sizeof(char*));
  95.                }
  96.             }
  97.  
  98.          } else {
  99.             /* We've got a new token, skip leading quote */
  100.             beg_ptr = cur_ptr;
  101.             beg_ptr++;
  102.             in_token = TRUE;
  103.          }
  104.       }
  105.       cur_ptr++;
  106.    }
  107.  
  108.    if (in_token) {
  109.       /* Exited above loop still in a token. */
  110.       /*VARARGS*/
  111.       fprintf(stderr,
  112.          "StringTableConverter: Unmatched quote in resource string.\n"
  113.       );
  114.       toVal->addr = NULL;
  115.       toVal->size = 0;
  116.    } else {
  117.       /* Null terminate the array. */
  118.       array[token_num] = NULL;
  119.  
  120.       /* Load the return value. */
  121.       toVal->addr = (caddr_t) &array;
  122.       toVal->size = sizeof(array);
  123.    }
  124. }
  125.  
  126.  
  127. /* Return the number of strings in a string table. */
  128. int StringTableNumber(table)
  129. StringTable table;
  130. {
  131.    int number = -1;
  132.    while (table[++number]);
  133.    return number;
  134. }
  135.  
  136.  
  137. /* Dump the specified string table.  For debugging only. */
  138. void StringTableDump(table)
  139. StringTable table;
  140. {
  141.    int number = -1;
  142.    while (table[++number]) {
  143.       /*VARARGS*/
  144.       printf("%d: %s\n", number, table[number]);
  145.    }
  146. }
  147.  
  148.  
  149.  
  150. /* Create new array with old contents. */
  151. StringTable StringTableCopy(old)
  152. StringTable old;
  153. {
  154.    int i;
  155.    int length = StringTableNumber(old) + 1;
  156.    StringTable new = (StringTable) XtMalloc(length * sizeof(char *));
  157.  
  158.    for (i=0; i < length; i++) {   
  159.       new[i] = old[i];
  160.    }
  161.  
  162.    return new;
  163. }
  164.