Remove i18n_get_string() function signature where we pass the 'return value' as refer...
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 12 Dec 2011 16:35:08 +0000 (17:35 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Mon, 12 Dec 2011 16:35:08 +0000 (17:35 +0100)
Grepped whole Intranator source tree, seems to be used nowhere and this simplifies the API.

src/i18n.cpp
src/i18n.h
test/test_i18n.cpp

index e0d7c44..f7dfa43 100644 (file)
@@ -113,95 +113,79 @@ string i18n_get_default_language(void)
     return lang;
 }
 
-void i18n_get_string(const char *source, string &target, const vector<string> &data) {
-    string src = string (source);
-    i18n_get_string (src, target, data);
-}
+string i18n_get_string(const char *source, const vector<string> &data)
+{
+    string::size_type p=0, slen=0;
+    string dollar="$";
 
-string i18n_get_string(const char *source, const vector<string> &data) {
-    string target;
-    string src = string (source);
-    i18n_get_string (src, target, data);
-    return target;
-}
+    // Convert char* to std::string
+    string text(source);
+
+    while ((p=text.find('$',p))!=string::npos)
+    {
+        const string *ins;
+
+        if (text.size() < p)
+        {
+            ostringstream os;
+            os << "i18n syntax error: $ without number at pos ->" << p << "<- in string ->" << text << "<-";
+            return os.str();
+        }
+
+        // find string to insert (=ins)
+        if (text.at(p+1)=='$')
+        {
+            ins=&dollar;        
+            slen=2;
+        }
+        else
+        {
+            slen=text.find_first_not_of("0123456789",p+1);
+
+            if (slen==string::npos)
+            slen=text.size();
+
+            if (slen==p+1)
+            {
+                ostringstream os;
+                os << "i18n syntax error: $ without number at pos ->" << p << "<- in string ->" << text << "<-";
+                return os.str();
+            }
+
+            slen-=p;
+            istringstream is(text.substr(p+1,slen-1));
+            unsigned int dnr;
+
+            is >> dnr;
+
+            if (is.fail())
+            {
+                ostringstream os;
+                os << "i18n syntax error: error reading number at pos ->" << p << "<- in string ->" << text << "<-";
+                return os.str();
+            }
+
+            if (dnr+1 > data.size())
+            {
+                ostringstream os;
+                os << "i18n parameter error: missing variable ->$" << dnr << "<- in string ->" << text << "<-";
+                return os.str();
+            }
+
+            ins=&(data.at(dnr));
+        }
+
+        text.replace(p,slen,*ins);
+        p=p+ins->size();
+    }
 
-string i18n_get_string(const string &source, const vector<string> &data) {
-    string target;
-    i18n_get_string (source, target, data);
-    return target;
+    return text;
 }
 
-void i18n_get_string(const string &source, string &target, const vector<string> &data)
+string i18n_get_string(const std::string &source, const vector<string> &data)
 {
-       string::size_type p=0, slen=0;
-       string dollar="$";
-
-       // custom vars
-       string text = source;
-
-       target=text;
-
-       while ((p=target.find('$',p))!=string::npos)
-       {
-               const string *ins;
-
-               if (target.size() < p)
-               {
-                       ostringstream os;
-                       os << "i18n syntax error: $ without number at pos ->" << p << "<- in string ->" << text << "<-";
-                       target = os.str();
-                       return;
-               }                       
-
-               // find string to insert (=ins)
-               if (target.at(p+1)=='$')
-               {
-                       ins=&dollar;            
-                       slen=2;
-               }
-               else
-               {
-                       slen=target.find_first_not_of("0123456789",p+1);
-
-                       if (slen==string::npos)
-               slen=target.size();
-
-                       if (slen==p+1)
-                       {
-                               ostringstream os;
-                               os << "i18n syntax error: $ without number at pos ->" << p << "<- in string ->" << text << "<-";
-                               target = os.str();
-                               return;
-                       }                       
-
-                       slen-=p;
-                       istringstream is(target.substr(p+1,slen-1));
-                       unsigned int dnr;
-
-                       is >> dnr;
-
-                       if (is.fail())
-                       {
-                               ostringstream os;
-                               os << "i18n syntax error: error reading number at pos ->" << p << "<- in string ->" << text << "<-";
-                               target = os.str();
-                               return;
-                       }                       
-
-                       if (dnr+1 > data.size())
-                       {
-                               ostringstream os;
-                               os << "i18n parameter error: missing variable ->$" << dnr << "<- in string ->" << text << "<-";
-                               target = os.str();
-                               return;
-                       }                       
-
-                       ins=&(data.at(dnr));
-               }
-
-               target.replace(p,slen,*ins);
-               p=p+ins->size();
-       }
+    // Convert std::string to const char*
+    return i18n_get_string (source.c_str(), data);
 }
 
 // convert locale to language (de_DE -> de)
index 67c4b8c..e388fd0 100644 (file)
@@ -39,9 +39,6 @@ void i18n_set_language(const std::string &lang="");
 std::string i18n_get_current_language(void);
 std::string i18n_get_default_language(void);
 
-void i18n_get_string (const char *source, std::string &target, const std::vector<std::string> &data);
-void i18n_get_string (const std::string &source, std::string &target, const std::vector<std::string> &data);
-
 std::string i18n_get_string (const char *source, const std::vector<std::string> &data);
 std::string i18n_get_string (const std::string &source, const std::vector<std::string> &data);
 
index c3cd21c..c7fa4d1 100644 (file)
@@ -86,4 +86,15 @@ BOOST_AUTO_TEST_CASE(KeepEscapedDollarSign)
     BOOST_CHECK_EQUAL("dummy $ test string", result);
 }
 
+BOOST_AUTO_TEST_CASE(StdStringInput)
+{
+    vector<string> data;
+    data.push_back("dummy");
+
+    const string input = "$0 test string";
+    string result = i18n_get_string(input, data);
+
+    BOOST_CHECK_EQUAL("dummy test string", result);
+}
+
 BOOST_AUTO_TEST_SUITE_END()