Make better use of the string type when parsing things. This switches
expand one from using a fixed buffer to using a string which dynamically allocates these things. Submitted by: green@ (against an earlier version of devd) Ignored for too long by: imp
This commit is contained in:
parent
c829b9d0fc
commit
6cabd675ee
@ -176,7 +176,7 @@ protected:
|
|||||||
void sort_vector(vector<event_proc *> &);
|
void sort_vector(vector<event_proc *> &);
|
||||||
void parse_one_file(const char *fn);
|
void parse_one_file(const char *fn);
|
||||||
void parse_files_in_dir(const char *dirname);
|
void parse_files_in_dir(const char *dirname);
|
||||||
void expand_one(const char *&src, char *&dst, char *eod);
|
void expand_one(const char *&src, string &dst);
|
||||||
bool is_id_char(char);
|
bool is_id_char(char);
|
||||||
bool chop_var(char *&buffer, char *&lhs, char *&rhs);
|
bool chop_var(char *&buffer, char *&lhs, char *&rhs);
|
||||||
private:
|
private:
|
||||||
@ -475,20 +475,16 @@ config::is_id_char(char ch)
|
|||||||
ch == '-'));
|
ch == '-'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// XXX
|
|
||||||
// imp should learn how to make effective use of the string class.
|
|
||||||
void
|
void
|
||||||
config::expand_one(const char *&src, char *&dst, char *)
|
config::expand_one(const char *&src, string &dst)
|
||||||
{
|
{
|
||||||
int count;
|
int count;
|
||||||
const char *var;
|
string buffer, varstr;
|
||||||
char buffer[1024];
|
|
||||||
string varstr;
|
|
||||||
|
|
||||||
src++;
|
src++;
|
||||||
// $$ -> $
|
// $$ -> $
|
||||||
if (*src == '$') {
|
if (*src == '$') {
|
||||||
*dst++ = *src++;
|
dst.append(src++, 1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -496,54 +492,52 @@ config::expand_one(const char *&src, char *&dst, char *)
|
|||||||
// Not sure if I want to support this or not, so for now we just pass
|
// Not sure if I want to support this or not, so for now we just pass
|
||||||
// it through.
|
// it through.
|
||||||
if (*src == '(') {
|
if (*src == '(') {
|
||||||
*dst++ = '$';
|
dst.append("$");
|
||||||
count = 1;
|
count = 1;
|
||||||
while (count > 0) {
|
/* If the string ends before ) is matched , return. */
|
||||||
|
while (count > 0 && *src) {
|
||||||
if (*src == ')')
|
if (*src == ')')
|
||||||
count--;
|
count--;
|
||||||
else if (*src == '(')
|
else if (*src == '(')
|
||||||
count++;
|
count++;
|
||||||
*dst++ = *src++;
|
dst.append(src++, 1);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ${^A-Za-z] -> $\1
|
// ${^A-Za-z] -> $\1
|
||||||
if (!isalpha(*src)) {
|
if (!isalpha(*src)) {
|
||||||
*dst++ = '$';
|
dst.append("$");
|
||||||
*dst++ = *src++;
|
dst.append(src++, 1);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// $var -> replace with value
|
// $var -> replace with value
|
||||||
var = src++;
|
do {
|
||||||
while (is_id_char(*src))
|
buffer.append(src++, 1);
|
||||||
src++;
|
} while (*src && isalpha(*src) || isdigit(*src) || *src == '_' ||
|
||||||
memcpy(buffer, var, src - var);
|
*src == '-');
|
||||||
buffer[src - var] = '\0';
|
buffer.append("", 1);
|
||||||
varstr = get_variable(buffer);
|
varstr = get_variable(buffer.c_str());
|
||||||
strcpy(dst, varstr.c_str());
|
dst.append(varstr);
|
||||||
dst += strlen(dst);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const string
|
const string
|
||||||
config::expand_string(const string &s)
|
config::expand_string(const string &s)
|
||||||
{
|
{
|
||||||
const char *src;
|
const char *src;
|
||||||
char *dst;
|
string dst;
|
||||||
char buffer[1024];
|
|
||||||
|
|
||||||
src = s.c_str();
|
src = s.c_str();
|
||||||
dst = buffer;
|
|
||||||
while (*src) {
|
while (*src) {
|
||||||
if (*src == '$')
|
if (*src == '$')
|
||||||
expand_one(src, dst, buffer + sizeof(buffer));
|
expand_one(src, dst);
|
||||||
else
|
else
|
||||||
*dst++ = *src++;
|
dst.append(src++, 1);
|
||||||
}
|
}
|
||||||
*dst++ = '\0';
|
dst.append("", 1);
|
||||||
|
|
||||||
return (buffer);
|
return (dst);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
Loading…
x
Reference in New Issue
Block a user