📄 code_init_file.html
字号:
<html><title>The cFile, cInitFile, and cGenesis classes</title><body bgcolor="#FFFFFF" text="#000000" link="#0000AA" alink="#0000FF" vlink="#000044"><h2 align=center>The cFile, cInitFile, and cGenesis classes</h2><p>All configuration methods in Avida involve reading information from afile. When we examined events, we didn't have to worry about dealing withthe file because all of that source code was automatically generatedfor us. It is not critical to fully understand the file system if you arenot going to directly use it, but it will aid you in putting other objectsinto perspective.<p>The <font color="#880000">cFile</font> class is a base class that handlesopening a file, closing it, and reading from it one line at a time.The <font color="#880000">cInitFile</font> class <b>extends</b> cFile (thatis, it is derived from cFile) adding special functions to load thefile into memory, remove comments from it, and search through it forkeywords. Finally, the <font color="#880000">cGenesis</font> class furtherextends cInitFile, including new functions that assume each line of the filehas the format "KEYWORD VALUE" to allow the programmer to pass in a keywordand get back a value of the appropriate form.<p>All three of these classes are defined in the files "<tt>file.hh</tt>" and"<tt>file.cc</tt>", located in the directory"<tt>current/source/tools/</tt>". See also thecDataFile class, which is more focused on writing to a file than reading fromit. Here, we'll step through each cFile-based class in order.<p><h3>The <font color="#880000">cFile</font> class</h3><p>This is the fundamental file loading class. Here is a mildly editedversion of its class declaration:<pre> class <font color="#880000">cFile</font> { protected: <font color="#880000">fstream</font> <font color="#000088">fp</font>; <font color="#886600">// An input stream associated with this file.</font> <font color="#880000">cString</font> <font color="#000088">filename</font>; <font color="#886600">// The name of the file we're working with.</font> <font color="#880000">bool</font> <font color="#000088">is_open</font>; <font color="#886600">// Have we successfully opened this file?</font> <font color="#880000">bool</font> <font color="#000088">verbose</font>; <font color="#886600">// Should file be verbose about warnings to users?</font> public: <font color="#008800">cFile</font>(<font color="#880000">cString</font> <font color="#000088">_fname</font>) : <font color="#000088">filename</font>(""), <font color="#000088">is_open</font>(false) { <font color="#008800">Open</font>(<font color="#000088">_fname</font>); } <font color="#008800">~cFile</font>() { if (<font color="#000088">is_open</font> == true) <font color="#000088">fp</font>.<font color="#008800">close</font>(); <font color="#000088">filename</font> = ""; } <font color="#886600">// Manipulators</font> <font color="#880000">bool</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="#008800">ios</font>::<font color="#000088">in</font>|<font color="#008800">ios</font>::<font color="#000088">nocreate</font>)); <font color="#880000">bool</font> <font color="#008800">Close</font>(); <font color="#880000">bool</font> <font color="#008800">ReadLine</font>(<font color="#880000">cString</font> & <font color="#000088">in_string</font>); <font color="#886600">// Tests</font> <font color="#880000">bool</font> <font color="#008800">IsOpen</font>() const { return <font color="#000088">is_open</font>; } <font color="#880000">bool</font> <font color="#008800">Good</font>() const { return (<font color="#000088">fp</font>.<font color="#008800">good</font>()); } <font color="#880000">bool</font> <font color="#008800">Eof</font>() const { return (<font color="#000088">fp</font>.<font color="#008800">eof</font>()); } <font color="#886600">// Accessors</font> void <font color="#008800">SetVerbose</font>(<font color="#880000">bool</font> <font color="#000088">_v</font>=true) { <font color="#000088">verbose</font> = <font color="#000088">_v</font>; } const <font color="#880000">cString</font> & <font color="#008800">GetFilename</font>() const { return <font color="#000088">filename</font>; } };</pre><p>To use this class, you create a cFile object and pass in a filename. Thiswill automatically run <font color="#008800">Open()</font> on the file. Atany point thereafter you have the option to <fontcolor="#008800">Close()</font> the file and open a new one. The<font color="#008800">IsOpen()</font> method allows you to test if a file iscurrently open, and the <font color="#008800">Good()</font> method tests ifthere are any problems with the file (i.e., it was deleted out from under theuser.)<p>You can obtain one line at a time from an open file by using the<font color="#008800">ReadLine()</font> method. When you call this method,you must give it a string that the method will modify. The method attemptsto copy the next line in the file into that string, and then themethod returns <tt>true</tt> or <tt>false</tt> indicating if the copy wassuccessful. Failure (a return value of <tt>false</tt>) typically indicatesthat the user is at the end of the file, or something about the file hasfailed.<p>As an example of what a method description looks like, here is thecode body for the Open() method:<p><pre> <font color="#880000">bool</font> <font color="#880000">cFile</font>::<font color="#008800">Open</font>(<font color="#880000">cString</font> <font color="#000088">_fname</font>, <font color="#880000">int</font> <font color="#000088">flags</font>) { if ( <font color="#008800">IsOpen</font>() ) <font color="#008800">Close</font>(); <font color="#886600">// If a file is already open, close it first.</font> <font color="#000088">fp</font>.<font color="#008800">open</font>(<font color="#000088">_fname</font>(), <font color="#000088">flags</font>); <font color="#886600">// Open the new file.</font> <font color="#886600">// Test if there was an error, and if so, try again!</font> <font color="#880000">int</font> <font color="#000088">err_id</font> = <font color="#000088">fp</font>.<font color="#008800">fail</font>(); if( <font color="#000088">err_id</font> != 0 ){ <font color="#000088">fp</font>.<font color="#008800">clear</font>(); <font color="#000088">fp</font>.<font color="#008800">open</font>(<font color="#000088">_fname</font>(), <font color="#000088">flags</font>); } <font color="#886600">// If there is still an error, determine its type and report it.</font> <font color="#000088">err_id</font> = <font color="#000088">fp</font>.<font color="#008800">fail</font>(); if ( <font color="#000088">err_id</font> != 0 ){ <font color="#880000">cString</font> <font color="#000088">error_desc</font> = "?? Unknown Error??"; <font color="#886600">// See if we can determine a more exact error type.</font> if (<font color="#000088">err_id</font> == EACCES) <font color="#000088">error_desc</font> = "Access denied"; else if (<font color="#000088">err_id</font> == EINVAL) <font color="#000088">error_desc</font> = "Invalid open flag or access mode"; else if (<font color="#000088">err_id</font> == ENOENT) <font color="#000088">error_desc</font> = "File or path not found"; <font color="#886600">// Print the error.</font> <font color="#000088">cerr</font> << "Unable to open file '" << <font color="#000088">_fname</font> << "' : " << <font color="#000088">error_desc</font> << endl; return false; } <font color="#000088">filename</font> = <font color="#000088">_fname</font>; <font color="#000088">is_open</font> = true; <font color="#886600">// Return true only if there were no problems...</font> return( <font color="#000088">fp</font>.<font color="#008800">good</font>() && !<font color="#000088">fp</font>.<font color="#008800">fail</font>() ); }</pre><p>To specify a method (when not actively running it on an object of theappropriate class, as when we're defining it), the format used is"cClassName::MethodName()". While this method opens the file, it checks eachstep of the way for potential problems. If there is a problem, it tries tofix it and, failing that, simply reports it to the user. My goal inincluding this method here is merely to give you an idea of what such amethod looks like.<h3>The <font color="#880000">cInitFile</font> class</h3><p>The cFile class alone allows a user to open a file and collect informationfrom it one line at a time, but that's it. There are no helpfulfunctions that enable us to accomplish specific goals. The cInitFile class,however, builds on top of cFile, and gives us methods that will be usefulfor a variety of initialization files.<p>Here is an edited version of its declaration:<p><pre> class <font color="#880000">cInitFile</font> : public <font color="#880000">cFile</font> { private: <font color="#880000">cStringList</font> <font color="#000088">line_list</font>; public: <font color="#008800">cInitFile</font>(<font color="#880000">cString</font> <font color="#000088">in_filename</font>); <font color="#008800">~cInitFile</font>(); void <font color="#008800">Load</font>(); <font color="#886600">// Load the file into memory so we can manipulate it.</font>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -