📄 code_init_file.html
字号:
void <font color="#008800">Compress</font>(); <font color="#886600">// Remove all Comments and Whitespace.</font> void <font color="#008800">AddLine</font>(<font color="#880000">cString</font> & <font color="#000088">in_string</font>); <font color="#886600">// Add a line to beginning of memory.</font> <font color="#880000">cString</font> <font color="#008800">RemoveLine</font>(); <font color="#886600">// Remove first line from memory.</font> <font color="#880000">cString</font> <font color="#008800">GetLine</font>(<font color="#880000">int</font> <font color="#000088">line_num</font>=0); <font color="#886600">// Get specified line from memory.</font> <font color="#880000">int</font> <font color="#008800">GetNumLines</font>() const { return <font color="#000088">line_list</font>.<font color="#008800">GetSize</font>(); } <font color="#886600">// Find a keyword in the specified column. Stop at first occurrence and // set in_string to the full line it was found in.</font> <font color="#880000">bool</font> <font color="#008800">Find</font>(<font color="#880000">cString</font> & <font color="#000088">in_string</font>, const <font color="#880000">cString</font> & <font color="#000088">keyword</font>, <font color="#880000">int</font> <font color="#000088">col</font>) const; <font color="#886600">// Find an entry the keyword in first column. Return the *remainder* of // the line. If not found, just return the default value given.</font> <font color="#880000">cString</font> <font color="#008800">ReadString</font>(const <font color="#880000">cString</font> & <font color="#000088">keyword</font>, <font color="#880000">cString</font> <font color="#000088">def</font>="") const; };</pre><p>The cInitFile class has a <font color="#880000">cStringList</font> datamember called <font color="#000088">line_list</font> that can contain acollection of "strings", in this case lines loaded from the file in question.The <font color="#008800">Load()</font> method is the one that copies all ofthe lines from the file into line_list, and then the<font color="#008800">Compress()</font> method will remove all comments(in this case, anything after a <tt>#</tt> on a line), compress allsequential spaces and tabs down to a single space, and completely remove anylines that then have nothing left on them. The<font color="#008800">AddLine()</font> and<font color="#008800">RemoveLine()</font> methods allow you to directlychange the contents of memory, while, <font color="#008800">GetLine()</font>allows you to retrieve a line at a specified position.The next method in this group is <font color="#008800">GetNumLines()</font>,which is exactly what it sounds -- it will return the number of linescurrently in memory.<p>Finally, we have a pair of methods that allow us to search through the memoryin a more useful manner. The <font color="#008800">Find()</font> methodseeks out a keyword in a specific column from the loaded file. It returns <tt>true</tt> or <tt>false</tt> depending on if that keywordwas found, and it will fill out the input variable<font color="#000088">in_string</font> with the contents of the full linethat the keyword was found in. The other method,<font color="#008800">ReadString()</font>, is only a slight variation onFind(). It takes a keyword, searches for it as the first word on any linein the file, and then returns the remainder of the line. If the keyword wasnowhere to be found in the file, it will instead return a default valuespecified by the user. Below is the definition of these two functions,as found in <tt>file.cc</tt>.<p><pre> <font color="#880000">bool</font> <font color="#880000">cInitFile</font>::<font color="#008800">Find</font>(<font color="#880000">cString</font> & <font color="#000088">in_string</font>, const <font color="#880000">cString</font> & <font color="#000088">keyword</font>, <font color="#880000">int</font> <font color="#000088">col</font>) const { <font color="#886600">// Loop through all of the lines until we find our keyword.</font> <font color="#880000">cStringIterator</font> <font color="#000088">list_it</font>(<font color="#000088">line_list</font>); while ( <font color="#000088">list_it</font>.<font color="#008800">AtEnd</font>() == false ) { <font color="#000088">list_it</font>.<font color="#008800">Next</font>(); <font color="#880000">cString</font> <font color="#000088">cur_string</font> = <font color="#000088">list_it</font>.<font color="#008800">Get</font>(); if (<font color="#000088">cur_string</font>.<font color="#008800">GetWord</font>(<font color="#000088">col</font>) == <font color="#000088">keyword</font>) { <font color="#000088">in_string</font> = <font color="#000088">cur_string</font>; return true; } } return false; <font color="#886600">// Not Found...</font> } <font color="#880000">cString</font> <font color="#880000">cInitFile</font>::<font color="#008800">ReadString</font>(const <font color="#880000">cString</font> & <font color="#000088">name</font>, <font color="#880000">cString</font> <font color="#000088">def</font>) const { <font color="#886600">// See if we definitely can't find the keyword.</font> if (<font color="#000088">name</font> == "" || <font color="#008800">IsOpen</font>() == false) return <font color="#000088">def</font>; <font color="#886600">// Search for the keyword.</font> <font color="#880000">cString</font> <font color="#000088">cur_line</font>; if ( <font color="#008800">Find</font>(<font color="#000088">cur_line</font>, <font color="#000088">name</font>, 0) == false ) { if (<font color="#000088">verbose</font> == true) { <font color="#000088">cerr</font> << "Warning: " << <font color="#000088">name</font> << " not in \"" << <font color="#008800">GetFilename</font>() << "\", defaulting to: " << <font color="#000088">def</font> << endl; } return <font color="#000088">def</font>; } <font color="#886600">// Pop off the keyword, and return the remainder of the line.</font> <font color="#000088">cur_line</font>.<font color="#008800">PopWord</font>(); return <font color="#000088">cur_line</font>; }</pre><p>This ReadString() method is one of the main ones that we use whendealing with the genesis file. In order to find the value of a specificsetting in genesis, we can run ReadString() with the name of that setting asa keyword, and its value will be returned. We can specify a defaultfor everything in case the user does not fill out genesis fully. This willall be explained in more detail in the next document.<h3>The <font color="#880000">cGenesis</font> class</h3><p>The cGenesis class extends cInitFile to add additional functionality that is specific to reading in the genesis file. It does not contain new data, but it does have new methods. Here is a simplified version of theclass declaration:<pre> class <font color="#880000">cGenesis</font> : public <font color="#880000">cInitFile</font> { public: <font color="#008800">cGenesis</font>(const <font color="#880000">cString</font> & <font color="#000088">_fname</font>); <font color="#008800">~cGenesis</font>(); <font color="#886600">// This Open command will run Open() from cInitFile, but then it will // also automatically Load() and Compress() the contents.</font> <font color="#880000">int</font> <font color="#008800">Open</font>(<font color="#880000">cString</font> <font color="#000088">_fname</font>, <font color="#880000">int</font> <font color="#000088">mode</font>=(<font color="#880000">ios</font>::<font color="#000088">in</font>|<font color="#880000">ios</font>::<font color="#000088">nocreate</font>)); <font color="#886600">// Functions to add more data to a loaded genesis file (they use AddLine())</font> void <font color="#008800">AddInput</font>(const <font color="#880000">cString</font> & <font color="#000088">in_name</font>, <font color="#880000">int</font> <font color="#000088">in_value</font>); void <font color="#008800">AddInput</font>(const <font color="#880000">cString</font> & <font color="#000088">in_name</font>, const <font color="#880000">cString</font> & <font color="#000088">in_value</font>); <font color="#886600">// Functions to read in integer or floating point values set in genesis.</font> <font color="#880000">int</font> <font color="#008800">ReadInt</font>(const <font color="#880000">cString</font> & <font color="#000088">name</font>, <font color="#880000">int</font> <font color="#000088">base</font>=0, <font color="#880000">bool</font> <font color="#000088">warn</font>=true) const; <font color="#880000">double</font> <font color="#008800">ReadFloat</font>(const <font color="#880000">cString</font> & <font color="#000088">name</font>, <font color="#880000">float</font> <font color="#000088">base</font>=0.0, <font color="#880000">bool</font> <font color="#000088">warn</font>=true) const;};</pre><p>In this class, the <font color="#008800">Open()</font> method overloads theone from cInitFile such that it will automatically also call<font color="#880000">cInitFile</font>::<font color="#008800">Load()</font>and<font color="#880000">cInitFile</font>::<font color="#008800">Compress()</font>inside of it.<p>The two <font color="#008800">AddInput</font>() methods are used to add newvalues to the memory image of the genesis file that will take precedence overany loaded directly from the file. This is made use of when the useractivates Avida with a command line option that has higher priority than agenesis setting. Notethat even though both of these methods go by the identical name, C++ knowswhich one you are intending to call by the variables passed intoit. In this case, either an integer or a string will be used as thesecond argument.<p>Finally, the <font color="#008800">ReadInt</font>() and<font color="#008800">ReadFloat</font>() methods are used to obtain settingsfrom the genesis file that are in integer or floating point form respectively.All these really do are to call<font color="#880000">cInitFile</font>::<font color="#008800">ReadString</font>()and then convert the resulting string into the desired type.<p>So how do we use this cGenesis class to load in and make use ofthe initialization file by the same name? For that, you'll need to readabout the cConfig class in the next document.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -