Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
rle_putcom.c
Go to the documentation of this file.
1 /*
2  * This software is copyrighted as noted below. It may be freely copied,
3  * modified, and redistributed, provided that the copyright notice is
4  * preserved on all copies.
5  *
6  * There is no warranty or other guarantee of fitness for this software,
7  * it is provided solely "as is". Bug reports or fixes may be sent
8  * to the author, who may or may not act on them as he desires.
9  *
10  * You may not include this software in a program or other software product
11  * without supplying the source, or without informing the end-user that the
12  * source is available for no extra charge.
13  *
14  * If you modify this software, you should include a notice giving the
15  * name of the person performing the modification, the date of modification,
16  * and the reason for such modification.
17  */
18 /*
19  * rle_putcom.c - Add a picture comment to the header struct.
20  *
21  * Author: Spencer W. Thomas
22  * Computer Science Dept.
23  * University of Utah
24  * Date: Mon Feb 2 1987
25  * Copyright (c) 1987, University of Utah
26  */
27 
28 #include <stdio.h>
29 #include "rle.h"
30 
31 /*****************************************************************
32  * TAG( match )
33  *
34  * Match a name against a test string for "name=value" or "name".
35  * If it matches name=value, return pointer to value part, if just
36  * name, return pointer to NUL at end of string. If no match, return NULL.
37  *
38  * Inputs:
39  * n: Name to match. May also be "name=value" to make it easier
40  * to replace comments.
41  * v: Test string.
42  * Outputs:
43  * Returns pointer as above.
44  * Assumptions:
45  * [None]
46  * Algorithm:
47  * [None]
48  */
49 static char *
50 match( n, v )
51 register char *n;
52 register char *v;
53 {
54  for ( ; *n != '\0' && *n != '=' && *n == *v; n++, v++ )
55  ;
56  if (*n == '\0' || *n == '=') {
57  if ( *v == '\0' )
58  return v;
59  else if ( *v == '=' )
60  return ++v;
61  }
62 
63  return NULL;
64 }
65 
66 /*****************************************************************
67  * TAG( rle_putcom )
68  *
69  * Put a comment into the header struct.
70  * Inputs:
71  * value: Value to add to comments.
72  * the_hdr: Header struct to add to.
73  * Outputs:
74  * the_hdr: Modified header struct.
75  * Returns previous value;
76  * Assumptions:
77  * value pointer can be used as is (data is NOT copied).
78  * Algorithm:
79  * Find match if any, else add at end (realloc to make bigger).
80  */
81 CONST_DECL char *
83 CONST_DECL char * value;
84 rle_hdr * the_hdr;
85 {
86  register CONST_DECL char ** cp, ** old_comments;
87  CONST_DECL char * v;
88  int i;
89 
90  if ( the_hdr->comments == NULL )
91  {
92  the_hdr->comments = (CONST_DECL char **)malloc( 2 * sizeof(char *) );
93  the_hdr->comments[0] = value;
94  the_hdr->comments[1] = NULL;
95  }
96  else
97  {
98  for ( i = 2, cp = the_hdr->comments; *cp != NULL; i++, cp++ )
99  if ( match( value, *cp ) != NULL )
100  {
101  v = *cp;
102  *cp = value;
103  return v;
104  }
105  /* Not found */
106  /* Can't realloc because somebody else might be pointing to this
107  * comments block. Of course, if this were true, then the
108  * assignment above would change the comments for two headers.
109  * But at least, that won't crash the program. Realloc will.
110  * This would work a lot better in C++, where hdr1 = hdr2
111  * could copy the pointers, too.
112  */
113  old_comments = the_hdr->comments;
114  the_hdr->comments = (CONST_DECL char **)malloc(i * sizeof(char *) );
115  the_hdr->comments[--i] = NULL;
116  the_hdr->comments[--i] = value;
117  for ( i--; i >= 0; i-- )
118  the_hdr->comments[i] = old_comments[i];
119  }
120 
121  return NULL;
122 }
123 
124 /*****************************************************************
125  * TAG( rle_delcom )
126  *
127  * Delete a comment from header struct.
128  * Inputs:
129  * name: Name of comment to delete.
130  * the_hdr: Header to delete comment from.
131  * Outputs:
132  * the_hdr: Modified header struct.
133  * Returns original comment value.
134  * Assumptions:
135  * [None]
136  * Algorithm:
137  * [None]
138  */
139 CONST_DECL char *
141 CONST_DECL char * name;
142 rle_hdr *the_hdr ;
143 {
144  register CONST_DECL char ** cp;
145  CONST_DECL char * v = NULL;
146 
147  if ( the_hdr->comments == NULL )
148  return NULL;
149  else
150  {
151  for ( cp = the_hdr->comments; *cp != NULL; cp++ )
152  if ( match( name, *cp ) != NULL )
153  {
154  v = *cp;
155  for ( ; *cp != NULL; cp++ )
156  *cp = cp[1];
157  break;
158  }
159  /* Not found */
160  }
161 
162  if ( *the_hdr->comments == NULL )
163  the_hdr->comments = NULL;
164 
165  return v;
166 }
const char ** comments
Definition: rle.h:113
const char * rle_delcom(char *name, the_hdr) const
Definition: rle_putcom.c:140
#define CONST_DECL
Definition: rle_config.h:42
static char * match(char *n, char *v)
Definition: rle_putcom.c:50
const char * rle_putcom(char *value, rle_hdr *the_hdr) const
Definition: rle_putcom.c:82