File: ParsedString.cc

package info (click to toggle)
htdig 1%3A3.2.0b6-21
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,292 kB
  • sloc: ansic: 49,632; cpp: 46,468; sh: 17,400; xml: 4,180; perl: 2,543; makefile: 888; php: 79; asm: 14
file content (202 lines) | stat: -rw-r--r-- 4,183 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
//
// ParsedString.cc
//
// ParsedString: Contains a string. The string my contain $var, ${var}, $(var)
//               `filename`. The get method will expand those using the
//               dictionary given in argument.
// 
// Part of the ht://Dig package   <https://htdig.sourceforge.net/>
// Copyright (c) 1999-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later 
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: ParsedString.cc,v 1.9 2004/05/28 13:15:21 lha Exp $
//

#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */

#include "ParsedString.h"

#include <ctype.h>
#include <stdio.h>


//*****************************************************************************
// ParsedString::ParsedString()
//
ParsedString::ParsedString()
{
}


//*****************************************************************************
//
ParsedString::ParsedString(const String& s)
{
    value = s;
}


//*****************************************************************************
// ParsedString::~ParsedString()
//
ParsedString::~ParsedString()
{
}


//*****************************************************************************
//
void
ParsedString::set(const String& str)
{
    value = str;
}


//*****************************************************************************
//   Return a fully parsed string.
//
//   Allowed syntax:
//       $var
//       ${var}
//       $(var)
//       `filename`
//
//   The filename can also contain variables
//
const String
ParsedString::get(const Dictionary &dict) const
{
  String		variable;
  String		parsed;
  ParsedString	*temp;
  const char		*str = value.get();
  char		delim = ' ';
  int			need_delim = 0;

  while (*str)
    {
      if (*str == '$')
        {
	  //
	  // A dollar sign starts a variable.
	  //
	  str++;
	  need_delim = 1;
	  if (*str == '{')
	    delim = '}';
	  else if (*str == '(')
	    delim = ')';
	  else
	    need_delim = 0;
	  if (need_delim)
	    str++;
	  variable.trunc();
	  while (isalnum(*str) || *str == '_' || *str == '-')
            {
	      variable << *str++;
            }
	  if (*str)
            {
	      if (need_delim && *str == delim)
                {
		  //
		  // Found end of variable
		  //
		  temp = (ParsedString *) dict[variable];
		  if (temp)
		    parsed << temp->get(dict);
		  str++;
                }
	      else if (need_delim)
                {
		  //
		  // Error.  Probably an illegal value in the name We'll
		  // assume the variable ended here.
		  //
		  temp = (ParsedString *) dict[variable];
		  if (temp)
		    parsed << temp->get(dict);
                }
	      else
                {
		  //
		  // This variable didn't have a delimiter.
		  //
		  temp = (ParsedString *) dict[variable];
		  if (temp)
		    parsed << temp->get(dict);
                }
            }
	  else
            {
	      //
	      // End of string reached.  We'll assume that this is also
	      // the end of the variable
	      //
	      temp = (ParsedString *) dict[variable];
	      if (temp)
		parsed << temp->get(dict);
            }
        }
      else if (*str == '`')
        {
	  //
	  // Back-quote delimits a filename which we need to insert
	  //
	  str++;
	  variable.trunc();
	  while (*str && *str != '`')
            {
	      variable << *str++;
            }
	  if (*str == '`')
	    str++;
	  ParsedString	filename(variable);
	  variable.trunc();
	  getFileContents(variable, filename.get(dict));
	  parsed << variable;
        }
      else if (*str == '\\')
        {
	  //
	  // Backslash escapes the next character
	  //
	  str++;
	  if (*str)
	    parsed << *str++;
        }
      else
        {
	  //
	  // Normal character
	  //
	  parsed << *str++;
        }
    }
  return parsed;
}


void
ParsedString::getFileContents(String &str, const String& filename) const
{
    FILE	*fl = fopen(filename, "r");
    char	buffer[1000];

    if (!fl)
        return;
    while (fgets(buffer, sizeof(buffer), fl))
    {
        String	s(buffer);
        s.chop("\r\n\t ");
        str << s << ' ';
    }
    str.chop(1);
    fclose(fl);
}