Logo
  • Main Page
  • Related Pages
  • Modules
  • Classes
  • Files

mmsconverter.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider,             *
00003  *                           Matthias Hardt, Guido Madaus                  *
00004  *                                                                         *
00005  *   Copyright (C) 2007-2008 BerLinux Solutions GbR                        *
00006  *                           Stefan Schwarzer & Guido Madaus               *
00007  *                                                                         *
00008  *   Copyright (C) 2009-2013 BerLinux Solutions GmbH                       *
00009  *                                                                         *
00010  *   Authors:                                                              *
00011  *      Stefan Schwarzer   <stefan.schwarzer@diskohq.org>,                 *
00012  *      Matthias Hardt     <matthias.hardt@diskohq.org>,                   *
00013  *      Jens Schneider     <jens.schneider@diskohq.org>,                   *
00014  *      Guido Madaus       <guido.madaus@diskohq.org>,                     *
00015  *      Patrick Helterhoff <patrick.helterhoff@diskohq.org>,               *
00016  *      René Bählkow       <rene.baehlkow@diskohq.org>                     *
00017  *                                                                         *
00018  *   This library is free software; you can redistribute it and/or         *
00019  *   modify it under the terms of the GNU Lesser General Public            *
00020  *   License version 2.1 as published by the Free Software Foundation.     *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU Lesser General Public      *
00028  *   License along with this library; if not, write to the                 *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
00031  **************************************************************************/
00032 
00033 #include "mmstools/mmsconverter.h"
00034 #include "mmstools/tools.h"
00035 #include <errno.h>
00036 #include <string.h>
00037 
00038 map<string, iconv_t> MMSConverter::trans;
00039 
00040 MMSConverter::MMSConverter(string codepage) {
00041     map<string, iconv_t>::iterator it;
00042     iconv_t newtrans;
00043 
00044     it = trans.find(codepage);
00045     if(it==trans.end()) {
00046         // TODO: memory is never freed by calling iconv_close()
00047         newtrans = iconv_open("UTF-8",codepage.c_str());
00048         if(newtrans !=(iconv_t)(-1)) {
00049             trans.insert(make_pair(codepage, newtrans));
00050         }else {
00051             throw MMSConverterError(0,"cannot create translation descriptor");
00052         }
00053     }
00054 
00055 }
00056 
00057 MMSConverter::~MMSConverter() {
00058 
00059 }
00060 
00061 string MMSConverter::convert(string frompage, string buffer) {
00062     map<string, iconv_t>::iterator it;
00063     char *lineptr;
00064     char *retlineptr;
00065     char line[32000];
00066     char retline[32000];
00067     size_t sizein, sizeout, sizeret;
00068     mutex.lock();
00069     it=trans.find(frompage);
00070     if(it!=trans.end()) {
00071         sizeout=32000;
00072         retlineptr=retline;
00073         memset(line,0,32000);
00074         memset(retline,0,32000);
00075         sprintf(line,"%s",buffer.c_str());
00076         sizein = strlen(line);
00077         lineptr=line;
00078         sizeret=iconv(it->second,&lineptr, &sizein,&retlineptr,&sizeout);
00079         if(sizeret == (size_t)-1) {
00080             DEBUGMSG("MMSConverter", "Error converting " + buffer + "(" + strerror(errno) + ")");
00081         }
00082         mutex.unlock();
00083         return retline;
00084     } else {
00085         mutex.unlock();
00086         throw MMSConverterError(0,"have no translation descriptor");
00087     }
00088     mutex.unlock();
00089     return "";
00090 }
00091 
00092 MMSMutex MMSConverter::mutex;

Generated by doxygen