Freeciv21
Develop your civilization from humble roots to a global empire
inputfile.cpp File Reference
#include <cstdarg>
#include <QLoggingCategory>
#include <KFilterDev>
#include "fcintl.h"
#include "log.h"
#include "inputfile.h"
+ Include dependency graph for inputfile.cpp:

Go to the source code of this file.

Classes

struct  inputfile
 

Macros

#define INF_MAGIC   (0xabdc0132)
 A low-level object for reading a registry-format file. More...
 

Typedefs

typedef QString(* get_token_fn_t) (struct inputfile *inf)
 

Functions

static QString get_token_section_name (struct inputfile *inf)
 Returns section name in current position of inputfile. More...
 
static QString get_token_entry_name (struct inputfile *inf)
 Returns next entry name from inputfile. More...
 
static QString get_token_eol (struct inputfile *inf)
 If inputfile is at end-of-line, frees current line, and returns " ". More...
 
static QString get_token_table_start (struct inputfile *inf)
 Get flag token for table start, or nullptr if that is not next token. More...
 
static QString get_token_table_end (struct inputfile *inf)
 Get flag token for table end, or nullptr if that is not next token. More...
 
static QString get_token_comma (struct inputfile *inf)
 Get flag token comma, or nullptr if that is not next token. More...
 
static QString get_token_value (struct inputfile *inf)
 This one is more complicated; note that it may read in multiple lines. More...
 
static bool read_a_line (struct inputfile *inf)
 Read a new line into cur_line. More...
 
template<class Char >
static bool is_comment (Char c)
 Return true if c is a 'comment' character: '#' or ';'. More...
 
static void init_zeros (struct inputfile *inf)
 Set values to zeros; should have free'd/closed everything before this if appropriate. More...
 
static bool inf_sanity_check (struct inputfile *inf)
 Check sensible values for an opened inputfile. More...
 
static QString inf_filename (struct inputfile *inf)
 Return the filename the inputfile was loaded as, or "(anonymous)" if this inputfile was loaded from a stream rather than from a file. More...
 
struct inputfileinf_from_file (const QString &filename, datafilename_fn_t datafn)
 Open the file, and return an allocated, initialized structure. More...
 
struct inputfileinf_from_stream (QIODevice *stream, datafilename_fn_t datafn)
 Open the stream, and return an allocated, initialized structure. More...
 
static void inf_close_partial (struct inputfile *inf)
 Close the file and free associated memory, but don't recurse included_from files, and don't free the actual memory where the inf record is stored (ie, the memory where the users pointer points to). More...
 
void inf_close (struct inputfile *inf)
 Close the file and free associated memory, included any partially recursed included files, and the memory allocated for 'inf' itself. More...
 
static bool have_line (struct inputfile *inf)
 Return TRUE if have data for current line. More...
 
static bool at_eol (struct inputfile *inf)
 Return TRUE if current pos is at end of current line. More...
 
bool inf_at_eof (struct inputfile *inf)
 Return TRUE if current pos is at end of file. More...
 
static bool check_include (struct inputfile *inf)
 Check for an include command, which is an isolated line with: include "filename" If a file is included via this mechanism, returns 1, and sets up data appropriately: (*inf) will now correspond to the new file, which is opened but no data read, and inf->included_from is set to newly malloced memory which corresponds to the old file. More...
 
static bool stop_reading (inputfile *inf)
 Stops reading the passed file. More...
 
QString inf_log_str (struct inputfile *inf, const char *message,...)
 Return a detailed log message, including information on current line number etc. More...
 
QString inf_token (struct inputfile *inf, enum inf_token_type type)
 Returns token of given type from given inputfile. More...
 
int inf_discard_tokens (struct inputfile *inf, enum inf_token_type type)
 Read as many tokens of specified type as possible, discarding the results; returns number of such tokens read and discarded. More...
 
static QString get_token_white_char (struct inputfile *inf, char target)
 Get a flag token of a single character, with optional preceeding whitespace. More...
 

Variables

struct {
   const char *   name
 
   get_token_fn_t   func
 
tok_tab [INF_TOK_LAST]
 

Macro Definition Documentation

◆ INF_MAGIC

#define INF_MAGIC   (0xabdc0132)

A low-level object for reading a registry-format file.

original author: David Pfitzner dwp@m.nosp@m.so.a.nosp@m.nu.ed.nosp@m.u.au

This module implements an object which is useful for reading/parsing a file in the registry format of registry.c. It takes care of the low-level file-reading details, and provides functions to return specific "tokens" from the file. Probably this should really use higher-level tools... (flex/lex bison/yacc?)

When the user tries to read a token, we return a (const char*) pointing to some data if the token was found, or nullptr otherwise. The data pointed to should not be modified. The retuned pointer is valid only until another inputfile is performed. (So should be used immediately, or fc_strdup-ed etc.)

The tokens recognised are as follows: (Single quotes are delimiters used here, but are not part of the actual tokens/strings.) Most tokens can be preceeded by optional whitespace; exceptions are section_name and entry_name.

section_name: '[foo]' returned token: 'foo'

entry_name: 'foo =' (optional whitespace allowed before '=') returned token: 'foo'

end_of_line: newline, or optional '#' or ';' (comment characters) followed by any other chars, then newline. returned token: should not be used except to check non-nullptr.

table_start: '{' returned token: should not be used except to check non-nullptr.

table_end: '}' returned token: should not be used except to check non-nullptr.

comma: literal ',' returned token: should not be used except to check non-nullptr.

value: a signed integer, or a double-quoted string, or a gettext-marked double quoted string. Strings may contain raw embedded newlines, and escaped doublequotes, or . eg: '123', '-999', '"foo"', '_("foo")' returned token: string containing number, for numeric, or string starting at first doublequote for strings, but ommiting trailing double-quote. Note this does not translate escaped doublequotes etc back to normal.

Definition at line 79 of file inputfile.cpp.

Typedef Documentation

◆ get_token_fn_t

typedef QString(* get_token_fn_t) (struct inputfile *inf)

Definition at line 107 of file inputfile.cpp.

Function Documentation

◆ at_eol()

static bool at_eol ( struct inputfile inf)
static

Return TRUE if current pos is at end of current line.

Definition at line 311 of file inputfile.cpp.

Referenced by get_token_eol().

◆ check_include()

static bool check_include ( struct inputfile inf)
static

Check for an include command, which is an isolated line with: include "filename" If a file is included via this mechanism, returns 1, and sets up data appropriately: (*inf) will now correspond to the new file, which is opened but no data read, and inf->included_from is set to newly malloced memory which corresponds to the old file.

Definition at line 339 of file inputfile.cpp.

Referenced by read_a_line().

◆ get_token_comma()

static QString get_token_comma ( struct inputfile inf)
static

Get flag token comma, or nullptr if that is not next token.

Definition at line 716 of file inputfile.cpp.

◆ get_token_entry_name()

static QString get_token_entry_name ( struct inputfile inf)
static

Returns next entry name from inputfile.

Skips white spaces and comments. Sets inputfile position after entry name.

Definition at line 603 of file inputfile.cpp.

◆ get_token_eol()

static QString get_token_eol ( struct inputfile inf)
static

If inputfile is at end-of-line, frees current line, and returns " ".

If there is still something on that line, returns "".

Definition at line 653 of file inputfile.cpp.

◆ get_token_section_name()

static QString get_token_section_name ( struct inputfile inf)
static

Returns section name in current position of inputfile.

Returns nullptr if there is no section name on that position. Sets inputfile position after section name.

Definition at line 579 of file inputfile.cpp.

◆ get_token_table_end()

static QString get_token_table_end ( struct inputfile inf)
static

Get flag token for table end, or nullptr if that is not next token.

Definition at line 708 of file inputfile.cpp.

◆ get_token_table_start()

static QString get_token_table_start ( struct inputfile inf)
static

Get flag token for table start, or nullptr if that is not next token.

Definition at line 700 of file inputfile.cpp.

◆ get_token_value()

static QString get_token_value ( struct inputfile inf)
static

This one is more complicated; note that it may read in multiple lines.

Definition at line 724 of file inputfile.cpp.

◆ get_token_white_char()

static QString get_token_white_char ( struct inputfile inf,
char  target 
)
static

Get a flag token of a single character, with optional preceeding whitespace.

Definition at line 679 of file inputfile.cpp.

Referenced by get_token_comma(), get_token_table_end(), and get_token_table_start().

◆ have_line()

static bool have_line ( struct inputfile inf)
static

Return TRUE if have data for current line.

Definition at line 301 of file inputfile.cpp.

Referenced by get_token_entry_name(), get_token_eol(), get_token_section_name(), get_token_value(), get_token_white_char(), and inf_token().

◆ inf_at_eof()

bool inf_at_eof ( struct inputfile inf)

Return TRUE if current pos is at end of file.

Definition at line 323 of file inputfile.cpp.

Referenced by inf_log_str(), and secfile_from_input_file().

◆ inf_close()

void inf_close ( struct inputfile inf)

Close the file and free associated memory, included any partially recursed included files, and the memory allocated for 'inf' itself.

Should only be used on an actually open inputfile. After this, the pointer should not be used.

Definition at line 285 of file inputfile.cpp.

Referenced by secfile_from_input_file().

◆ inf_close_partial()

static void inf_close_partial ( struct inputfile inf)
static

Close the file and free associated memory, but don't recurse included_from files, and don't free the actual memory where the inf record is stored (ie, the memory where the users pointer points to).

This is used when closing an included file.

Definition at line 251 of file inputfile.cpp.

Referenced by inf_close(), and stop_reading().

◆ inf_discard_tokens()

int inf_discard_tokens ( struct inputfile inf,
enum inf_token_type  type 
)

Read as many tokens of specified type as possible, discarding the results; returns number of such tokens read and discarded.

Definition at line 563 of file inputfile.cpp.

Referenced by secfile_from_input_file().

◆ inf_filename()

static QString inf_filename ( struct inputfile inf)
static

Return the filename the inputfile was loaded as, or "(anonymous)" if this inputfile was loaded from a stream rather than from a file.

Definition at line 189 of file inputfile.cpp.

Referenced by inf_close(), inf_close_partial(), inf_from_stream(), and inf_log_str().

◆ inf_from_file()

struct inputfile* inf_from_file ( const QString &  filename,
datafilename_fn_t  datafn 
)

Open the file, and return an allocated, initialized structure.

Returns nullptr if the file could not be opened.

Definition at line 202 of file inputfile.cpp.

Referenced by check_include(), and secfile_load_section().

◆ inf_from_stream()

struct inputfile* inf_from_stream ( QIODevice *  stream,
datafilename_fn_t  datafn 
)

Open the stream, and return an allocated, initialized structure.

Returns nullptr if the file could not be opened.

Definition at line 225 of file inputfile.cpp.

Referenced by inf_from_file(), and secfile_from_stream().

◆ inf_log_str()

QString inf_log_str ( struct inputfile inf,
const char *  message,
  ... 
)

Return a detailed log message, including information on current line number etc.

Message can be nullptr: then just logs information on where we are in the file.

Definition at line 494 of file inputfile.cpp.

Referenced by entry_from_inf_token(), and secfile_from_input_file().

◆ inf_sanity_check()

static bool inf_sanity_check ( struct inputfile inf)
static

Check sensible values for an opened inputfile.

Definition at line 167 of file inputfile.cpp.

Referenced by at_eol(), check_include(), have_line(), inf_at_eof(), inf_close(), inf_close_partial(), inf_log_str(), inf_token(), and read_a_line().

◆ inf_token()

QString inf_token ( struct inputfile inf,
enum inf_token_type  type 
)

Returns token of given type from given inputfile.

Definition at line 536 of file inputfile.cpp.

Referenced by inf_discard_tokens(), and secfile_from_input_file().

◆ init_zeros()

static void init_zeros ( struct inputfile inf)
static

Set values to zeros; should have free'd/closed everything before this if appropriate.

Definition at line 146 of file inputfile.cpp.

Referenced by inf_close_partial(), and inf_from_stream().

◆ is_comment()

template<class Char >
static bool is_comment ( Char  c)
static

Return true if c is a 'comment' character: '#' or ';'.

Definition at line 137 of file inputfile.cpp.

Referenced by check_include(), get_token_eol(), and get_token_value().

◆ read_a_line()

static bool read_a_line ( struct inputfile inf)
static

Read a new line into cur_line.

Increments line_num and cur_line_pos. Returns 0 if didn't read or other problem: treat as EOF. Strips newline from input.

Definition at line 457 of file inputfile.cpp.

Referenced by get_token_value(), inf_token(), and stop_reading().

◆ stop_reading()

static bool stop_reading ( inputfile inf)
static

Stops reading the passed file.

Returns the file from which it was included, if any.

Definition at line 433 of file inputfile.cpp.

Referenced by read_a_line().

Variable Documentation

◆ func

◆ name

const char* name

Definition at line 118 of file inputfile.cpp.

Referenced by achievement_by_rule_name(), action_by_rule_name(), advance_by_rule_name(), advance_by_translated_name(), pregame_options::ailevel_change(), api_edit_create_base(), api_edit_create_city(), api_edit_create_extra(), api_edit_create_owned_extra(), api_edit_create_road(), api_edit_remove_extra(), api_methods_tile_has_base(), api_methods_tile_has_extra(), api_methods_tile_has_road(), api_specenum_create_table(), audio_select_plugin(), citybar_painter::available_vector(), boot_help_texts(), city_dialog::buy(), change_build_target(), check_include(), check_leader_names(), city_build(), city_list_find_name(), city_name_suggestion(), city_rename(), city_style_rule_name(), client_ss_by_name(), cmd_reply_no_such_conn(), cmd_reply_no_such_player(), command_named(), comment_write(), compat_load_020400(), compat_load_020500(), compat_load_020600(), complain_ambiguous(), create_city(), create_city_virtual(), freeciv::layer_terrain::create_matching_group(), cut_client_connection(), delegate_command(), download_modpack_list(), edit_buffer_copy(), entry_from_inf_token(), entry_from_token(), entry_new(), entry_set_name(), execute_orders(), extra_type_by_rule_name(), extra_type_by_translated_name(), extract_sequence_text(), fc_gethostname(), fileinfolist(), finish_city(), game_city_by_name(), generic_generator(), get_city_dialog_output_text(), get_data_dirs(), get_default_diplstate(), get_help_item_spec(), get_output_name(), get_save_dirs(), get_scenario_dirs(), get_soundplugin_list(), get_token_value(), get_unique_guest_name(), get_useable_themes_in_directory(), fcFont::getFont(), global_worklist_load(), global_worklist_new(), global_worklist_set_name(), goods_by_rule_name(), goods_by_translated_name(), government_by_rule_name(), government_by_translated_name(), freeciv::layer_terrain::group(), handle_chat_msg_req(), handle_city_name_suggestion_info(), handle_city_rename(), handle_nation_select_req(), handle_save_scenario(), handle_unit_do_action(), handle_unit_rename(), improvement_by_rule_name(), improvement_by_translated_name(), inf_token(), init_character_encodings(), freeciv::layer_terrain::initialize_cell_hex_corner(), insert_generated_text(), insert_veteran_help(), is_ascii_name(), is_default_city_name(), is_guest_name(), is_on_allowed_list(), is_safe_filename(), is_secfile_entry_name_valid(), is_valid_username(), kick_command(), load_cma_preset(), load_command(), load_nation_names(), load_ruleset_game(), load_ruleset_nations(), load_sprite(), freeciv::layer_grid::load_sprites(), freeciv::layer_units::load_sprites(), load_style_names(), lookup_option(), lookup_req_list(), lookup_resource(), lookup_terrain(), luascript_do_string(), luascript_push_args(), makeup_connection_name(), mapimg_help(), mpdb_installed_version(), mpdb_update_modpack(), multiplier_by_rule_name(), multipliers_init(), name_and_sort_items(), nation_by_rule_name(), nation_by_translated_plural(), nation_city_new(), nation_group_by_rule_name(), nation_group_new(), nation_leader_by_name(), nation_leader_new(), plr_widget::nation_selected(), nation_set_by_rule_name(), observe_command(), option_dialog::option_dialog(), option_dialog_popup(), options_init_names(), options_save(), optset_option_by_name(), simple_citybar_painter::paint(), traditional_citybar_painter::paint(), polished_citybar_painter::paint(), city_production_delegate::paint(), parse_metaserver_data(), pick_random_player_name(), player_by_name(), player_by_name_prefix(), player_by_user(), player_by_user_delegated(), plrdata_slot_init(), plrdata_slot_replace(), remove_player_command(), report_demographics(), request_do_action(), research_advance_name_translation(), freeciv::tileset_options_dialog::reset(), rscompat_names(), ruleset_load_names(), save_buildings_ruleset(), save_cities_ruleset(), city_dialog::save_cma(), save_cma_preset(), save_effects_ruleset(), save_game_ruleset(), save_governments_ruleset(), save_name_translation(), save_nations_ruleset(), save_ruleset(), save_styles_ruleset(), save_techs_ruleset(), save_terrain_ruleset(), save_units_ruleset(), script_fcdb_functions_check(), search_for_city_name(), secfile_lookup_enum_data(), secfile_lookup_enum_default_data(), secfile_section_by_name(), secfile_section_new(), section_entry_bool_new(), section_entry_by_name(), section_entry_filereference_new(), section_entry_float_new(), section_entry_int_new(), section_entry_str_new(), send_ruleset_cities(), send_ruleset_team_names(), server_player_name_is_allowed(), server_player_set_name_full(), server_setting_by_name(), server_ss_by_name(), set_ai_level(), set_ai_level_named(), citybar_painter::set_current(), freeciv::tileset_debugger::set_tile(), set_user_extra_flag_name(), set_user_tech_flag_name(), set_user_terrain_flag_name(), set_user_unit_class_flag_name(), set_user_unit_type_flag_name(), fcFont::setFont(), setting_bitwise_bit(), setting_bitwise_secfile_str(), setting_bitwise_to_str(), setting_bool_secfile_str(), setting_bool_to_str(), setting_by_name(), setting_enum_secfile_str(), setting_enum_to_str(), setting_enum_val(), setting_ruleset_one(), settings_game_load(), settings_ruleset(), mpgui::setup_list(), setup_modpack_list(), sg_load_player_city(), sg_load_player_main(), sg_load_player_unit(), sg_load_player_units(), sg_load_ruledata(), signal_callback_new(), specialist_by_rule_name(), specialist_by_translated_name(), srv_ready(), ssetv_by_rule_name(), style_by_rule_name(), take_command(), tech_class_by_rule_name(), technology_load(), technology_save(), terrain_by_rule_name(), terrain_by_translated_name(), text_tag_init_from_sequence(), tiles_lookup_sprite_tag_alt(), mr_menu::tileset_custom_load(), tileset_option_is_enabled(), freeciv::tileset_options_dialog::tileset_options_dialog(), tileset_set_option(), tileset_setup_citizen_types(), tileset_setup_options(), tilespec_reread(), tilespec_reread_frozen_refresh(), tool_init(), transfer_city_units(), unit_do_action(), unit_perform_action(), unit_type_by_rule_name(), unit_type_by_translated_name(), user_username(), valid_ruleset_filename(), and worklist_load().

◆ 

struct { ... } tok_tab[INF_TOK_LAST]
Initial value:
= {
{"section_name", get_token_section_name},
{"entry_name", get_token_entry_name},
{"end_of_line", get_token_eol},
{"table_start", get_token_table_start},
{"table_end", get_token_table_end},
{"comma", get_token_comma},
{"value", get_token_value},
}
static QString get_token_eol(struct inputfile *inf)
If inputfile is at end-of-line, frees current line, and returns " ".
Definition: inputfile.cpp:653
static QString get_token_entry_name(struct inputfile *inf)
Returns next entry name from inputfile.
Definition: inputfile.cpp:603
static QString get_token_table_end(struct inputfile *inf)
Get flag token for table end, or nullptr if that is not next token.
Definition: inputfile.cpp:708
static QString get_token_section_name(struct inputfile *inf)
Returns section name in current position of inputfile.
Definition: inputfile.cpp:579
static QString get_token_value(struct inputfile *inf)
This one is more complicated; note that it may read in multiple lines.
Definition: inputfile.cpp:724
static QString get_token_comma(struct inputfile *inf)
Get flag token comma, or nullptr if that is not next token.
Definition: inputfile.cpp:716
static QString get_token_table_start(struct inputfile *inf)
Get flag token for table start, or nullptr if that is not next token.
Definition: inputfile.cpp:700

Referenced by inf_token().