Date: Monday, December 19, 2005 @ 18:10:21
  Author: csaba
    Path: /cvsroot/carob/libmysequoia

   Added: include/IniParser.hpp (1.1) src/IniParser.cpp (1.1)
Modified: src/Makefile.am (1.2 -> 1.3)

Added code to parse the MySQL my.cnf configuration file.


-----------------------+
 include/IniParser.hpp |   61 +++++++++++++++++++
 src/IniParser.cpp     |  152 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/Makefile.am       |    4 -
 3 files changed, 216 insertions(+), 1 deletion(-)


Index: libmysequoia/include/IniParser.hpp
diff -u /dev/null libmysequoia/include/IniParser.hpp:1.1
--- /dev/null   Mon Dec 19 18:10:21 2005
+++ libmysequoia/include/IniParser.hpp  Mon Dec 19 18:10:21 2005
@@ -0,0 +1,61 @@
+/*
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2005 Continuent, Inc.
+ * Contact: [EMAIL PROTECTED]
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Initial developer(s): Zsolt Simon, Csaba Simon
+ * Contributor(s): 
+ */
+
+#ifndef _INIPARSER_HPP
+#define _INIPARSER_HPP
+
+#include <string>
+#include <map>
+
+class IniParser
+{
+public:
+  /**
+   * Open the file and parse it.
+   * @param name the name of the file
+   */
+  void parseFile(const char *name);
+
+       /**
+        * Get the value of the option name in the group
+        * @param group the group name
+        * @param name the option name
+        * @return the value of the option name as string
+        */
+  std::string get(std::string group, std::string name) const;
+
+private:
+       /**
+        * holds the name=value pairs in each group
+        */
+  std::map<std::string, std::map<std::string, std::string> > keys;
+
+  void removeLeadingSpaces(std::string& s);
+  void removeTrailingSpaces(std::string& s);
+  void removeComments(std::string& s);
+  void removeSpacesAndComments(std::string& s);
+
+  bool parseGroup(std::string line, std::string& group);
+  void parseName(std::string line, std::string group);
+  std::string parseValue(std::string value);
+};
+
+#endif /* _INIPARSER_HPP */
Index: libmysequoia/src/IniParser.cpp
diff -u /dev/null libmysequoia/src/IniParser.cpp:1.1
--- /dev/null   Mon Dec 19 18:10:21 2005
+++ libmysequoia/src/IniParser.cpp      Mon Dec 19 18:10:21 2005
@@ -0,0 +1,152 @@
+/*
+ * Sequoia: Database clustering technology.
+ * Copyright (C) 2005 Continuent, Inc.
+ * Contact: [EMAIL PROTECTED]
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License")
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Initial developer(s): Zsolt Simon, Csaba Simon
+ * Contributor(s): 
+ */
+ 
+#include "IniParser.hpp"
+
+#include <string>
+#include <fstream>
+
+using namespace std;
+ 
+void IniParser::parseFile(const char *name)
+{
+  string group, line;
+  bool in_group = false;
+
+  ifstream file(name);
+  
+       if (file)
+  {
+         while (getline(file, line))
+         {
+           removeSpacesAndComments(line);
+           if (line.empty())
+            continue;
+           if (parseGroup(line, group))
+           {
+             in_group = true;
+           }
+           else
+           {
+             if (in_group)
+               parseName(line, group);
+           }
+         }
+  }
+}
+ 
+string IniParser::get(std::string group, std::string name) const
+{
+  std::map<std::string, std::map<std::string, std::string> >::const_iterator 
group_iter = keys.find(group);
+  if (group_iter == keys.end())
+    return "";
+
+  std::map<std::string, std::string>::const_iterator name_iter = 
group_iter->second.find(name);
+  if (name_iter == group_iter->second.end())
+    return "";
+  
+       return name_iter->second;
+}
+
+void IniParser::removeLeadingSpaces(string& s)
+{
+  unsigned int i = 0;
+  
+  while (i != s.size() && isspace(s[i]))
+    ++i;
+    
+  s.assign(s, i, s.size() - i);
+}
+
+void IniParser::removeTrailingSpaces(string& s)
+{
+  unsigned int i = s.size() > 0 ? s.size() - 1 : 0;
+  
+  while (i >= 0 && isspace(s[i]))
+    --i;
+    
+  s.assign(s, 0, i + 1);
+}
+
+void IniParser::removeComments(string& line)
+{
+  unsigned int i = 0;
+  bool in_quote = false;
+
+       if (line[0] == '#' || line[0] == ';')
+       {
+               line.assign(line, 0, 0);
+               return;
+       }
+       
+  for (i = 0; i != line.size(); ++i)
+    {
+      if (line[i] == '\"')
+      {
+        in_quote = !in_quote;
+      }
+      else if (line[i] == '#' && !in_quote)
+      {
+        break;
+      }
+    }
+    
+    line.assign(line, 0, i);
+}  
+
+void IniParser::removeSpacesAndComments(string& line)
+{
+  removeLeadingSpaces(line);
+  removeComments(line);  
+  removeTrailingSpaces(line);
+}
+
+bool IniParser::parseGroup(string line, string& group)
+{
+  if (line[0] != '[' || line[line.size() -1] != ']')
+    return false;
+  
+  group.assign(line, 1, line.size() - 2);
+  removeLeadingSpaces(group);
+  removeTrailingSpaces(group);
+  
+  return true;
+}
+
+void IniParser::parseName(string line, string group)
+{
+  unsigned int i = line.find('=');
+  
+  if (i == string::npos)
+    return;
+  
+  string name(line, 0, i);
+  removeTrailingSpaces(name);
+  
+  keys[group][name] = parseValue(string(line, i+1));
+}
+
+string IniParser::parseValue(string value)
+{
+  removeLeadingSpaces(value);
+  
+  return value;
+}
Index: libmysequoia/src/Makefile.am
diff -u libmysequoia/src/Makefile.am:1.2 libmysequoia/src/Makefile.am:1.3
--- libmysequoia/src/Makefile.am:1.2    Mon Dec 19 09:06:58 2005
+++ libmysequoia/src/Makefile.am        Mon Dec 19 18:10:21 2005
@@ -24,7 +24,9 @@
 
 libmysequoia_la_SOURCES  = CarobMySQL.cpp \
                            MySQLAPI.cpp \
-                           Utils.cpp
+                           Utils.cpp \
+                           IniParser.cpp
+
 # remove the relative paths to carob when carob will be installable            
               
 libmysequoia_la_CXXFLAGS = -I$(top_builddir)/../carob/include @MYSQL_CFLAGS@ 
@GCOV_CFLAGS@
 libmysequoia_la_LDFLAGS  = -version-info 0:0:0 -L$(top_builddir)/../carob 
-lcarob @GCOV_LDADD@ @LOG4CXX_LDADD@

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to