~ubuntu-branches/ubuntu/natty/steam/natty

« back to all changes in this revision

Viewing changes to config.pike

  • Committer: Bazaar Package Importer
  • Author(s): Alain Schroeder
  • Date: 2005-05-14 16:33:35 UTC
  • Revision ID: james.westby@ubuntu.com-20050514163335-5v7lbxibmlww15dx
Tags: upstream-1.6.3
ImportĀ upstreamĀ versionĀ 1.6.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000-2004  Thomas Bopp, Thorsten Hampel, Ludger Merkens
 
2
 *
 
3
 *  This program is free software; you can redistribute it and/or modify
 
4
 *  it under the terms of the GNU General Public License as published by
 
5
 *  the Free Software Foundation; either version 2 of the License, or
 
6
 *  (at your option) any later version.
 
7
 *
 
8
 *  This program is distributed in the hope that it will be useful,
 
9
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
 *  GNU General Public License for more details.
 
12
 *
 
13
 *  You should have received a copy of the GNU General Public License
 
14
 *  along with this program; if not, write to the Free Software
 
15
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
16
 * 
 
17
 * $Id: config.pike,v 1.1.1.1 2005/02/23 14:47:21 cvs Exp $
 
18
 */
 
19
 
 
20
constant cvs_version="$Id: config.pike,v 1.1.1.1 2005/02/23 14:47:21 cvs Exp $";
 
21
 
 
22
 
 
23
#define METHOD_GETS     1
 
24
#define METHOD_READLINE 0
 
25
 
 
26
static         mapping vars = ([ ]);
 
27
private static string      sContext;
 
28
static         int           method;
 
29
Stdio.Readline readln = Stdio.Readline(Stdio.stdin);
 
30
 
 
31
/**
 
32
 *
 
33
 *  
 
34
 * @param 
 
35
 * @return 
 
36
 * @author <a href="mailto:astra@upb.de">Thomas Bopp</a>) 
 
37
 * @see 
 
38
 */
 
39
mapping handle_options(array argv, mapping options)
 
40
{
 
41
    mapping mOptions = ([ "options": ({ }) ]);
 
42
 
 
43
    foreach ( argv, string arg ) {
 
44
        string oname, oval;
 
45
        if ( sscanf(arg, "--%s=%s", oname, oval) == 2 ) {
 
46
            mOptions[oname] = oval;
 
47
        }
 
48
        else if ( sscanf(arg, "--%s", oname) == 1 )
 
49
            mOptions[oname] = 1;
 
50
        else if ( sscanf(arg, "-%s", oname) == 1 ) {
 
51
            if ( stringp(options[oname]) )
 
52
                mOptions[options[oname]] = 1;
 
53
            else
 
54
                werror("Unknown Option: " + arg + "\n");
 
55
        }
 
56
        else
 
57
            mOptions["options"] += ({ arg });
 
58
    }
 
59
    return mOptions;
 
60
}
 
61
 
 
62
 
 
63
/**
 
64
 *
 
65
 *  
 
66
 * @param 
 
67
 * @return 
 
68
 * @author Thomas Bopp (astra@upb.de) 
 
69
 * @see 
 
70
 */
 
71
mixed read_input(string desc, mixed def_value, void|mixed ... values)
 
72
{
 
73
    string                               str;
 
74
    mixed                              value;
 
75
    int                               ok = 0;
 
76
 
 
77
    while ( !ok ) {
 
78
      if ( method == METHOD_GETS ) {
 
79
        write(desc+" ["+def_value+"]: ");
 
80
        str = Stdio.stdin.gets();
 
81
      }
 
82
      else {
 
83
        str = readln->read(desc + " ["+def_value+"]: ");
 
84
      }
 
85
 
 
86
      if ( !stringp(str) || strlen(str) == 0 ) {
 
87
        value = def_value;
 
88
        ok = 1; 
 
89
      }
 
90
      else {
 
91
        if ( sscanf(str, "%d", value) != 1 || str != (string)value )
 
92
          value = str;
 
93
        
 
94
        if ( stringp(def_value) && stringp(value) )
 
95
          ok = 1;
 
96
        else if ( intp(def_value) && intp(value) )
 
97
          ok = 1;
 
98
        if ( arrayp(values) && sizeof(values) > 0 && 
 
99
             search(values, value) < 0 )
 
100
          ok = 0;
 
101
      }
 
102
    }
 
103
    return value;
 
104
}
 
105
 
 
106
 
 
107
/**
 
108
 *
 
109
 *  
 
110
 * @param fname - a file that should be detected at the path
 
111
 * @return 
 
112
 * @author Thomas Bopp (astra@upb.de) 
 
113
 * @see 
 
114
 */
 
115
string read_path(string desc, string def_value, string fname)
 
116
{
 
117
    string server_path;
 
118
    int         ok = 0;
 
119
    Stdio.File       f;
 
120
 
 
121
    while ( !ok ) {
 
122
        server_path = read_input(desc, def_value);
 
123
        if ( server_path[strlen(server_path)-1] != '/' )
 
124
            server_path += "/";
 
125
        mixed err = catch {
 
126
            f = Stdio.File(server_path + fname, "r");
 
127
        };
 
128
        if ( objectp(f) ) {
 
129
            ok = 1;
 
130
            f->close();
 
131
        }
 
132
    }
 
133
    return server_path;
 
134
}
 
135
 
 
136
/**
 
137
 *
 
138
 *  
 
139
 * @param 
 
140
 * @return 
 
141
 * @author Thomas Bopp (astra@upb.de) 
 
142
 * @see 
 
143
 */
 
144
static mixed exchange_vars(Parser.HTML p, string data)
 
145
{
 
146
    if ( stringp(data) && strlen(data) > 0 )
 
147
    {
 
148
        if ( data[0] == '$' && 
 
149
             zero_type(vars[data[1..]]) != 1 ) {
 
150
            data = (string)vars[data[1..]];
 
151
        }
 
152
    }
 
153
    return ({ data });
 
154
}
 
155
 
 
156
/**
 
157
 *
 
158
 *  
 
159
 * @param 
 
160
 * @return 
 
161
 * @author Thomas Bopp (astra@upb.de) 
 
162
 * @see 
 
163
 */
 
164
void copy_config_file(string from, string to, object parser)
 
165
{
 
166
    Stdio.File   f;
 
167
    string content;
 
168
 
 
169
    f = Stdio.File(from, "r");
 
170
    content = f->read();
 
171
    f->close();
 
172
    parser->feed(content);
 
173
    parser->finish();
 
174
    content = parser->read();
 
175
    f = Stdio.File(to, "wct");
 
176
    f->write(content);
 
177
    f->close();
 
178
}
 
179
 
 
180
/**
 
181
 *
 
182
 *  
 
183
 * @param 
 
184
 * @return 
 
185
 * @author Thomas Bopp (astra@upb.de) 
 
186
 * @see 
 
187
 */
 
188
private static int cb_context(Parser.HTML p, string tag)
 
189
{
 
190
    tag = p->parse_tag_name(tag);
 
191
    tag = tag[1..strlen(tag)-2];
 
192
    sContext = replace(tag,":", "_");
 
193
    return 0;
 
194
}
 
195
 
 
196
/**
 
197
 *
 
198
 *  
 
199
 * @param 
 
200
 * @return 
 
201
 * @author Thomas Bopp (astra@upb.de) 
 
202
 * @see 
 
203
 */
 
204
private static int read_config(Parser.HTML p, string data)
 
205
 
206
    int d;
 
207
 
 
208
    if ( sContext[0] == '/' ) return 0;
 
209
    
 
210
    // empty config file ?
 
211
    if ( stringp(data) && data[0] == '$' )
 
212
        return 0;
 
213
    
 
214
    if ( sscanf(data, "%d", d) == 1 )
 
215
        vars[sContext] = d;
 
216
    else
 
217
        vars[sContext] = data;
 
218
    return 0;
 
219
}
 
220
 
 
221
static void read_configs(string fname)
 
222
{
 
223
    string content;
 
224
 
 
225
    if ( !Stdio.exist(fname) )
 
226
        error("Configuration file " + fname +  " not found.");
 
227
    
 
228
    content = Stdio.read_file(fname);
 
229
    object p = Parser.HTML();
 
230
    p->_set_tag_callback(cb_context);
 
231
    p->_set_data_callback(read_config);
 
232
    p->feed(content);
 
233
    p->finish();
 
234
}
 
235