Skip to content

Commit

Permalink
Load config files faster (#3505)
Browse files Browse the repository at this point in the history
Also fixed issue with parameter_is_number that didn't parse negative numbers properly
  • Loading branch information
xtremeqg authored Oct 3, 2024
1 parent a9efe06 commit b28f807
Show file tree
Hide file tree
Showing 10 changed files with 828 additions and 651 deletions.
13 changes: 13 additions & 0 deletions src/bflib_basics.c
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,19 @@ void make_uppercase(char * string) {
}
}

int natoi(const char * str, int len) {
int value = -1;
for (int i = 0; i < len; ++i) {
if (!isdigit(str[i])) {
return value;
} else if (value < 0) {
value = 0;
}
value = (value * 10) + (str[i] - '0');
}
return value;
}

/******************************************************************************/
#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions src/bflib_basics.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ long saturate_set_signed(long long val,unsigned short nbits);
unsigned long saturate_set_unsigned(unsigned long long val,unsigned short nbits);
void make_lowercase(char *);
void make_uppercase(char *);
int natoi(const char * str, int len); // like atoi but stops after len bytes

/**
* Converts an index number to a flag - by creating a bitmask where only the nth bit is set to 1.
Expand Down
75 changes: 75 additions & 0 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,81 @@ short find_conf_block(const char *buf,long *pos,long buflen,const char *blocknam
return -1;
}

/**
* Reads the block name from buf, starting at pos.
* Sets name and namelen to the block name and name length respectively.
* Returns true on success, false when the block name is zero.
*/
TbBool conf_get_block_name(const char * buf, long * pos, long buflen, const char ** name, int * namelen)
{
const long start = *pos;
*name = NULL;
*namelen = 0;
while (true) {
if (*pos >= buflen) {
return false;
} else if (isalpha(buf[*pos])) {
(*pos)++;
continue;
} else if (isdigit(buf[*pos])) {
(*pos)++;
continue;
} else {
if (*pos - start > 0) {
*name = &buf[start];
*namelen = *pos - start;
return true;
} else {
return false;
}
}
}
}

/**
* Searches for the next block in buf, starting at pos.
* Sets name and namelen to the block name and name length respectively.
* Returns true on success, false when no more blocks are found.
*/
TbBool iterate_conf_blocks(const char * buf, long * pos, long buflen, const char ** name, int * namelen)
{
text_line_number = 1;
*name = NULL;
*namelen = 0;
while (true) {
// Skip whitespace before block start
if (!skip_conf_spaces(buf, pos, buflen)) {
return false;
}
// Check if this line is start of a block
if (*pos >= buflen) {
return false;
} else if (buf[*pos] != '[') {
skip_conf_to_next_line(buf, pos, buflen);
continue;
}
(*pos)++;
// Skip whitespace before block name
if (!skip_conf_spaces(buf, pos, buflen)) {
return false;
}
// Get block name
if (!conf_get_block_name(buf, pos, buflen, name, namelen)) {
skip_conf_to_next_line(buf, pos, buflen);
return false;
}
// Skip whitespace after block name
if (!skip_conf_spaces(buf, pos, buflen)) {
return false;
} else if (buf[*pos] != ']') {
skip_conf_to_next_line(buf, pos, buflen);
continue;
}
skip_conf_to_next_line(buf,pos,buflen);
return true;
}
}

/**
* Recognizes config command and returns its number, or negative status code.
* @param buf
Expand Down
1 change: 1 addition & 0 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ TbBool reset_credits(struct CreditsItem *credits);
TbBool setup_campaign_credits_data(struct GameCampaign *campgn);
/******************************************************************************/
short find_conf_block(const char *buf,long *pos,long buflen,const char *blockname);
TbBool iterate_conf_blocks(const char * buf, long * pos, long buflen, const char ** name, int * namelen);
int recognize_conf_command(const char *buf,long *pos,long buflen,const struct NamedCommand *commands);
TbBool skip_conf_to_next_line(const char *buf,long *pos,long buflen);
int get_conf_parameter_single(const char *buf,long *pos,long buflen,char *dst,long dstlen);
Expand Down
Loading

0 comments on commit b28f807

Please sign in to comment.