Skip to content

Commit

Permalink
Config files load faster (#3516)
Browse files Browse the repository at this point in the history
This time surely without game breaking bugs.
  • Loading branch information
xtremeqg authored Oct 10, 2024
1 parent df0bb1a commit cc4e46a
Show file tree
Hide file tree
Showing 19 changed files with 1,214 additions and 1,173 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
81 changes: 79 additions & 2 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 Expand Up @@ -439,6 +514,8 @@ int recognize_conf_command(const char *buf,long *pos,long buflen,const struct Na
}
i++;
}
const int len = strcspn(&buf[(*pos)], " \n\r\t");
CONFWRNLOG("Unrecognized command '%.*s'", len, &buf[(*pos)]);
return ccr_unrecognised;
}

Expand Down Expand Up @@ -1338,9 +1415,9 @@ short load_configuration(void)
CONFWRNLOG("Invalid API port '%s' in %s file.",COMMAND_TEXT(cmd_num),config_textname);
}
break;
case 0: // comment
case ccr_comment:
break;
case -1: // end of buffer
case ccr_endOfFile:
break;
default:
CONFWRNLOG("Unrecognized command in %s file.",config_textname);
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
16 changes: 8 additions & 8 deletions src/config_campaigns.c
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ short parse_campaign_common_blocks(struct GameCampaign *campgn,char *buf,long le
// Finding command number in this line
int cmd_num = recognize_conf_command(buf, &pos, len, cmpgn_common_commands);
// Now store the config item in correct place
if (cmd_num == -3) break; // if next block starts
if (cmd_num == ccr_endOfBlock) break; // if next block starts
int i = 0, n = 0;
char word_buf[32];
switch (cmd_num)
Expand Down Expand Up @@ -686,9 +686,9 @@ short parse_campaign_common_blocks(struct GameCampaign *campgn,char *buf,long le
campgn->assignCpuKeepers = 1;
}
break;
case 0: // comment
case ccr_comment:
break;
case -1: // end of buffer
case ccr_endOfFile:
break;
default:
CONFWRNLOG("Unrecognized command (%d) in [%s] block of %s %s file.",
Expand Down Expand Up @@ -729,7 +729,7 @@ short parse_campaign_strings_blocks(struct GameCampaign *campgn,char *buf,long l
campgn->default_language = (cmd_num >= 0) ? cmd_num : 0;
}
// Now store the config item in correct place
if (cmd_num == -3) break; // if next block starts
if (cmd_num == ccr_endOfBlock) break; // if next block starts
if (cmd_num <= 0)
{
if ((cmd_num != 0) && (cmd_num != -1))
Expand Down Expand Up @@ -769,7 +769,7 @@ short parse_campaign_speech_blocks(struct GameCampaign *campgn,char *buf,long le
// Finding command number in this line
int cmd_num = recognize_conf_command(buf, &pos, len, lang_type);
// Now store the config item in correct place
if (cmd_num == -3) break; // if next block starts
if (cmd_num == ccr_endOfBlock) break; // if next block starts
if (cmd_num <= 0)
{
if ((cmd_num != 0) && (cmd_num != -1))
Expand Down Expand Up @@ -829,7 +829,7 @@ short parse_campaign_map_block(long lvnum, unsigned long lvoptions, char *buf, l
// Finding command number in this line
int cmd_num = recognize_conf_command(buf, &pos, len, cmpgn_map_commands);
// Now store the config item in correct place
if (cmd_num == -3) break; // if next block starts
if (cmd_num == ccr_endOfBlock) break; // if next block starts
int n = 0;
char word_buf[32];
switch (cmd_num)
Expand Down Expand Up @@ -995,9 +995,9 @@ short parse_campaign_map_block(long lvnum, unsigned long lvoptions, char *buf, l
COMMAND_TEXT(cmd_num),block_buf,config_textname);
}
break;
case 0: // comment
case ccr_comment:
break;
case -1: // end of buffer
case ccr_endOfFile:
break;
default:
CONFWRNLOG("Unrecognized command (%d) in [%s] block of '%s' file.",
Expand Down
30 changes: 15 additions & 15 deletions src/config_compp.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ TbBool parse_computer_player_common_blocks(char *buf, long len, const char *conf
// Finding command number in this line
int cmd_num = recognize_conf_command(buf, &pos, len, compp_common_commands);
// Now store the config item in correct place
if (cmd_num == -3) break; // if next block starts
if (cmd_num == ccr_endOfBlock) break; // if next block starts
int n = 0;
char word_buf[COMMAND_WORD_LEN];
switch (cmd_num)
Expand Down Expand Up @@ -363,9 +363,9 @@ TbBool parse_computer_player_common_blocks(char *buf, long len, const char *conf
}
}
break;
case 0: // comment
case ccr_comment:
break;
case -1: // end of buffer
case ccr_endOfFile:
break;
default:
CONFWRNLOG("Unrecognized command (%d) in [%s] block of %s file.",
Expand Down Expand Up @@ -400,7 +400,7 @@ short parse_computer_player_process_blocks(char *buf, long len, const char *conf
// Finding command number in this line
int cmd_num = recognize_conf_command(buf, &pos, len, compp_process_commands);
// Now store the config item in correct place
if (cmd_num == -3) break; // if next block starts
if (cmd_num == ccr_endOfBlock) break; // if next block starts
if ((flags & CnfLd_ListOnly) != 0) {
// In "List only" mode, accept only name command
if (cmd_num > 2) {
Expand Down Expand Up @@ -539,9 +539,9 @@ short parse_computer_player_process_blocks(char *buf, long len, const char *conf
COMMAND_TEXT(cmd_num),block_buf,config_textname);
}
break;
case 0: // comment
case ccr_comment:
break;
case -1: // end of buffer
case ccr_endOfFile:
break;
default:
CONFWRNLOG("Unrecognized command (%d) in [%s] block of %s file.",
Expand Down Expand Up @@ -590,7 +590,7 @@ short parse_computer_player_check_blocks(char *buf, long len, const char *config
// Finding command number in this line
int cmd_num = recognize_conf_command(buf, &pos, len, compp_check_commands);
// Now store the config item in correct place
if (cmd_num == -3) break; // if next block starts
if (cmd_num == ccr_endOfBlock) break; // if next block starts
if ((flags & CnfLd_ListOnly) != 0) {
// In "List only" mode, accept only name command
if (cmd_num > 2) {
Expand Down Expand Up @@ -680,9 +680,9 @@ short parse_computer_player_check_blocks(char *buf, long len, const char *config
COMMAND_TEXT(cmd_num),block_buf,config_textname);
}
break;
case 0: // comment
case ccr_comment:
break;
case -1: // end of buffer
case ccr_endOfFile:
break;
default:
CONFWRNLOG("Unrecognized command (%d) in [%s] block of %s file.",
Expand Down Expand Up @@ -731,7 +731,7 @@ short parse_computer_player_event_blocks(char *buf, long len, const char *config
// Finding command number in this line
int cmd_num = recognize_conf_command(buf, &pos, len, compp_event_commands);
// Now store the config item in correct place
if (cmd_num == -3) break; // if next block starts
if (cmd_num == ccr_endOfBlock) break; // if next block starts
if ((flags & CnfLd_ListOnly) != 0) {
// In "List only" mode, accept only name command
if (cmd_num > 2) {
Expand Down Expand Up @@ -847,9 +847,9 @@ short parse_computer_player_event_blocks(char *buf, long len, const char *config
COMMAND_TEXT(cmd_num),block_buf,config_textname);
}
break;
case 0: // comment
case ccr_comment:
break;
case -1: // end of buffer
case ccr_endOfFile:
break;
default:
CONFWRNLOG("Unrecognized command (%d) in [%s] block of %s file.",
Expand Down Expand Up @@ -909,7 +909,7 @@ short parse_computer_player_computer_blocks(char *buf, long len, const char *con
// Finding command number in this line
int cmd_num = recognize_conf_command(buf, &pos, len, compp_computer_commands);
// Now store the config item in correct place
if (cmd_num == -3) break; // if next block starts
if (cmd_num == ccr_endOfBlock) break; // if next block starts
if ((flags & CnfLd_ListOnly) != 0) {
// In "List only" mode, accept only name command
if (cmd_num > 1) {
Expand Down Expand Up @@ -1067,9 +1067,9 @@ short parse_computer_player_computer_blocks(char *buf, long len, const char *con
COMMAND_TEXT(cmd_num),block_buf,config_textname);
}
break;
case 0: // comment
case ccr_comment:
break;
case -1: // end of buffer
case ccr_endOfFile:
break;
default:
CONFWRNLOG("Unrecognized command (%d) in [%s] block of %s file.",
Expand Down
Loading

0 comments on commit cc4e46a

Please sign in to comment.