00001 /* 00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net) 00003 This file is part of Beesnest. 00004 00005 Beesnest is free software; you can redistribute it and/or modify 00006 it under the terms of the GNU General Public License as published by 00007 the Free Software Foundation; either version 2 of the License, or 00008 (at your option) any later version. 00009 00010 Beesnest is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00013 GNU General Public License for more details. 00014 00015 You should have received a copy of the GNU General Public License 00016 along with Beesnest; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00020 /* DcParameters.h: interface for the CdcParameters class. */ 00021 00022 00023 #ifndef CDC_PARAMETERS 00024 #define CDC_PARAMETERS 00025 00026 #include "DcGlobals.h" 00027 #include "DcExceptionFile.h" 00028 #include _GET_INCLUDE_PATH (DcFileUtil.h) 00029 00030 /** 00031 * Reads, Parses and holds the Beesnest configuration. <br> 00032 * 00033 * <p><b>That was silly - I Need to work with a regular XML parser or not with 00034 * an XML format at all.</b></p> 00035 * 00036 * This class contains just static data members and functions. it reads 00037 * parameters from a XML-like file, and stores them for the use of this server. 00038 * 00039 * <p> 00040 * The format of the Parameters file is similar to a normal XML format, BUT:<br> 00041 * - From a # sign to the end of the line, the text is ignored (a comment).<br> 00042 * - You cannot insert a list of nodes with the same name.<br> 00043 * - A node cannot be self closed, like "<Node />".<br> 00044 * - Beesnest ignores node attributes, but you can put them in the file.<br> 00045 * - There is no XML header in the beginning of the page.<br> 00046 * - There are no standard XML comments.<br> 00047 * 00048 * <p> 00049 * All the parameters are stored as strings. There is a function to convert a 00050 * string to an integer. 00051 * 00052 * <p> 00053 * The parameters get stored in a map (hash table), and a tree. the name of each 00054 * parameter is 'field1'->'field2'... <br> 00055 * e.g. if the file look like this: <br> 00056 * <pre> 00057 * <Beesnest> 00058 * <Server> 00059 * <Name> Val </Name> 00060 * </Server> 00061 * </Beesnest> 00062 * </pre> 00063 * The value 'Val' will be stored under the name: "Server->Name" 00064 * 00065 * The user can look for a value under some point in the tree as well. 00066 * 00067 * @author Erez Bibi 00068 * @version 1.0 00069 */ 00070 class CdcParameters 00071 { 00072 private: 00073 00074 class CdcObject 00075 { }; 00076 00077 public: 00078 /** Node in the tree. */ 00079 class CdcNode : public CdcObject 00080 { 00081 public: 00082 00083 typedef list <CdcObject*> t_node_list; 00084 t_node_list oNodes; /* List of child nodes. */ 00085 CdcNode* up; /* Pointer to the father. */ 00086 CdcString name; /* The name of this node. */ 00087 CdcString value; /* The value of this node. */ 00088 CdcNode (const CdcString& n, CdcNode* u) /* Constructor. */ 00089 :name (n), up (u) {} 00090 }; 00091 00092 private: 00093 typedef map <string, CdcNode*> t_obj_map; 00094 00095 /** The hash table that holds the nodes (parameters). */ 00096 static t_obj_map mParam; 00097 00098 /** No Constructor or Destructor available. */ 00099 CdcParameters() {} 00100 virtual ~CdcParameters() {} 00101 00102 /** 00103 * This function looks for a value under some node in the tree. This is a 00104 * recursive function. 00105 * <p> 00106 * @param node The node to start to look under. 00107 * @param value The value to look for. 00108 * @param partly True if looking for part of a value. False is 00109 * the default. 00110 * @return (bool) True if this value exists under this node. 00111 */ 00112 static bool IsUnder (CdcParameters::CdcNode* node, 00113 const CdcString& value, bool partly); 00114 00115 /** 00116 * This function looks for a node name in the tree. This is a recursive 00117 * function. 00118 * <p> 00119 * @param name The node name to look for. 00120 * @param node The node to start the search at. 00121 * @param append_name True if the return value should be the node name 00122 * appended to the recursive call result. False if the return 00123 * value is just the recursive call result. 00124 * @return The full name of this parameter. 00125 */ 00126 static CdcString FindName (const CdcString& name, 00127 CdcParameters::CdcNode* node, bool append_name); 00128 00129 /** 00130 * Build a CdcString buffer from all the text files in the path <b>path</b> 00131 * and with extension <b>ext</b>. 00132 * <p> 00133 * @param buff The string buffer to fill. 00134 * @param path The path to take the files from (w/o sub directories). 00135 * @param ext The extension of the files to take. 00136 */ 00137 static void BuildParamBuffer (CdcString& buff, 00138 const CdcString& path, const CdcString& ext); 00139 00140 /** 00141 * Read a text file into CdcString. It reads the file without lines that 00142 * start with '#' or empty lines. 00143 * <p> 00144 * @param buff The string buffer to fill. 00145 * @param file_name The file to read. 00146 */ 00147 static void ReadParamFile (CdcString& buff, const CdcString& file_name); 00148 00149 public: 00150 00151 /** 00152 * This static function reads the Parameters file, parses it, and fills the 00153 * values of the class parameters (map). 00154 * <p> 00155 * @param file_ext The files extension to read, as string. 00156 * @return (bool) True if the Parameters file format is Well-Formed 00157 * (according to my rules...). 00158 * @throw (CdcException) if the Parameters file format is wrong. 00159 */ 00160 static bool ReadParams (const char* file_ext) throw (CdcException); 00161 00162 /** 00163 * Return a string from the parameters map. 00164 * <p> 00165 * @param name The name of the parameter. 00166 * @return (CdcString) the value of the parameter (as string) or an empty 00167 * string. 00168 */ 00169 static CdcString GetParam (const CdcString& name); 00170 static CdcString GetParam (const char* name) 00171 { return GetParam (CdcString (name)); } 00172 00173 /** 00174 * Return a pointer to the node that name is pointing to. 00175 * <p> 00176 * @param name The name of the parameter (node). 00177 * @return (CdcNode) Pointer to a node or NULL. 00178 */ 00179 static CdcNode* GetNodePointer (const char* name); 00180 00181 /** 00182 * This function looks for some value under a point in the parameters tree 00183 * (node). 00184 * <p> 00185 * @param name The point to start look for the value. It should be built as 00186 * "Field1->Field2->...". 00187 * @param value The value to look for. 00188 * @param partly True if looking for part of a value. False is the default. 00189 * @return (bool) True if this value exists under this point. 00190 */ 00191 static bool IsUnder (const CdcString& name, const CdcString& value, 00192 bool partly = false); 00193 static bool IsUnder (const char* name, const CdcString& value, 00194 bool partly = false) 00195 { return IsUnder (CdcString (name), value, partly); } 00196 00197 00198 /** 00199 * This function returns the full parameter name of a parameter with direct 00200 * name of <b>name</b>. 00201 * <p> 00202 * E.g. if <b>name</b> is "Format" and <b>start_at</b> is "Server", the 00203 * returned value will be "Server->Log-File->Format". 00204 * <p> 00205 * @param name The parameter name to look for. 00206 * @param start_at The place to start the search at. 00207 * @return (CdcString) The full name of this parameter. 00208 */ 00209 static CdcString GetFullName (const CdcString& name, 00210 const CdcString& start_at); 00211 00212 /** 00213 * Try to convert a string to an integer. 00214 * <p> 00215 * @param str The string to convert. 00216 * @return (int) The value of the string or 0 if it cannot be converted 00217 */ 00218 static int ToInt (const CdcString& str); 00219 00220 /** 00221 * Every adapter/engine gets a pointer to this function, so it can get 00222 * values from the configuration database. 00223 * <p> 00224 * @param name The name (path) of the required value. 00225 * @param val Buffer to put the value in. 00226 * @param max_len The size of this buffer. 00227 * @return True if the parameter was found in the database. 00228 */ 00229 friend bool ExtGetParam (const char* name, char* val, int max_len); 00230 00231 }; 00232 00233 #endif /* CDC_PARAMETERS */ 00234