home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 2 / 2307 / template.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-28  |  4.3 KB  |  166 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. /*
  26.    template.c -- routines to read and manage a diary template.
  27.  
  28.    Author: Jason Baietto
  29.      Date: November 9, 1990
  30. */
  31.  
  32.  
  33. /*---------------------------------------------------------------------------*/
  34. /*                               Header Files                                */
  35. /*---------------------------------------------------------------------------*/
  36. #include <stdio.h>
  37. #include <ctype.h>
  38. #include <values.h>
  39. #include <X11/Intrinsic.h>
  40.  
  41.  
  42. /*---------------------------------------------------------------------------*/
  43. /*                          Global Types And Macros                          */
  44. /*---------------------------------------------------------------------------*/
  45. #define START_LENGTH 1000
  46. #define GROW_LENGTH 1000
  47. #define EMPTYSTRING ""
  48.  
  49.  
  50.  
  51. static template_length;
  52.  
  53. /*---------------------------------------------------------------------------*/
  54. /*                              Static Routines                              */
  55. /*---------------------------------------------------------------------------*/
  56. static char * read_template(file_name)
  57. char * file_name;
  58. {
  59.    int i = 0;
  60.    int c = 0;
  61.    char * char_ptr;
  62.    long current_length = START_LENGTH;
  63.    FILE * file;
  64.  
  65.    char_ptr = (char *) XtMalloc(current_length);
  66.  
  67.    file = fopen(file_name, "r");
  68.    if (!file) {
  69.       return ((char *) NULL);
  70.    }
  71.  
  72.    /* Just read the whole thing into memory to minimize file I/O time */
  73.    c = getc(file);
  74.    while (c != EOF) {
  75.       char_ptr[i++] = c;
  76.  
  77.       if (i >= current_length) {
  78.          current_length += GROW_LENGTH;
  79.          char_ptr = (char *) XtRealloc(char_ptr, current_length);
  80.       }
  81.  
  82.       c = getc(file);
  83.    }
  84.  
  85.    char_ptr[i] = NULL;   
  86.    template_length = i;
  87.  
  88.    fclose(file);
  89.    return(char_ptr);
  90. }
  91.  
  92.  
  93.  
  94.  
  95. static char * compressed_template;
  96.  
  97. static char * compress(uncompressed)
  98. char * uncompressed;
  99. {
  100.    char * buffer = (char *) XtCalloc(template_length+1, sizeof(char));
  101.    char * compressed = buffer;
  102.    
  103.    while (*uncompressed) {
  104.       if (isspace(*uncompressed)) {
  105.          uncompressed++;
  106.       } else {
  107.          *compressed = *uncompressed;
  108.          compressed++;
  109.          uncompressed++;
  110.       }
  111.    }
  112.  
  113.    return buffer;
  114.  
  115. }
  116.  
  117.  
  118.  
  119.  
  120.  
  121. /*---------------------------------------------------------------------------*/
  122. /*                              Global Routines                              */
  123. /*---------------------------------------------------------------------------*/
  124. char * template;
  125.  
  126.  
  127.  
  128.  
  129. int init_template(file_name)
  130. char * file_name;
  131. {
  132.    template = read_template(file_name);
  133.  
  134.    if (!template) {
  135.       /* The template specified does not exist. */
  136.       return FALSE;
  137.    }
  138.  
  139.    compressed_template = compress(template);
  140.    return TRUE;
  141. }
  142.  
  143.  
  144.  
  145.  
  146. int entry_modified(entry)
  147. char * entry;
  148. {
  149.    char * temp = compressed_template;
  150.  
  151.    while(*entry && *temp) {
  152.       if (isspace(*entry)) {
  153.          entry++;
  154.       } else {
  155.          if (*entry != *temp) {
  156.             return TRUE;
  157.          } else {
  158.             entry++;
  159.             temp++;
  160.          }
  161.       }
  162.    }
  163.    
  164.    return FALSE;
  165. }
  166.