"Fossies" - the Fresh Open Source Software Archive

Member "regexxer-0.10/src/translation.cc" (6 Oct 2011, 2732 Bytes) of package /linux/privat/old/regexxer-0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "translation.cc" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (c) 2002-2007  Daniel Elstner  <daniel.kitta@gmail.com>
    3  *
    4  * This file is part of regexxer.
    5  *
    6  * regexxer is free software; you can redistribute it and/or modify
    7  * it under the terms of the GNU General Public License as published by
    8  * the Free Software Foundation; either version 2 of the License, or
    9  * (at your option) any later version.
   10  *
   11  * regexxer is distributed in the hope that it will be useful,
   12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   14  * GNU General Public License for more details.
   15  *
   16  * You should have received a copy of the GNU General Public License
   17  * along with regexxer; if not, write to the Free Software Foundation,
   18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
   19  */
   20 
   21 #include <config.h>
   22 #if ENABLE_NLS
   23 # include <libintl.h>
   24 #endif
   25 
   26 #include "translation.h"
   27 
   28 #include <glib.h>
   29 #include <glibmm.h>
   30 #include <cstring>
   31 
   32 #if ENABLE_NLS
   33 void Util::initialize_gettext(const char* domain, const char* localedir)
   34 {
   35   bindtextdomain(domain, localedir);
   36 # if HAVE_BIND_TEXTDOMAIN_CODESET
   37   bind_textdomain_codeset(domain, "UTF-8");
   38 # endif
   39   textdomain(domain);
   40 }
   41 #else
   42 void Util::initialize_gettext(const char*, const char*)
   43 {}
   44 #endif /* !ENABLE_NLS */
   45 
   46 const char* Util::translate(const char* msgid)
   47 {
   48 #if ENABLE_NLS
   49   return gettext(msgid);
   50 #else
   51   return msgid;
   52 #endif
   53 }
   54 
   55 Glib::ustring Util::compose_argv(const char* format, int argc, const Glib::ustring *const * argv)
   56 {
   57   const std::string::size_type format_size = std::strlen(format);
   58   std::string::size_type       result_size = format_size;
   59 
   60   // Guesstimate the final string size.
   61   for (int i = 0; i < argc; ++i)
   62     result_size += argv[i]->raw().size();
   63 
   64   std::string result;
   65   result.reserve(result_size);
   66 
   67   const char* start = format;
   68 
   69   while (const char* stop = std::strchr(start, '%'))
   70   {
   71     if (stop[1] == '%')
   72     {
   73       result.append(start, stop - start + 1);
   74       start = stop + 2;
   75     }
   76     else
   77     {
   78       const int index = Glib::Ascii::digit_value(stop[1]) - 1;
   79 
   80       if (index >= 0 && index < argc)
   81       {
   82         result.append(start, stop - start);
   83         result += argv[index]->raw();
   84         start = stop + 2;
   85       }
   86       else
   87       {
   88         const char *const next = (stop[1] != '\0') ? g_utf8_next_char(stop + 1) : (stop + 1);
   89 
   90         // Copy invalid substitutions literally to the output.
   91         result.append(start, next - start);
   92 
   93         g_warning("invalid substitution \"%s\" in format string \"%s\"",
   94                   result.c_str() + result.size() - (next - stop), format);
   95         start = next;
   96       }
   97     }
   98   }
   99 
  100   result.append(start, format + format_size - start);
  101 
  102   return result;
  103 }