• Skip to content
  • Skip to link menu
KDE 4.4 API Reference
  • KDE API Reference
  • KDE Support
  • Sitemap
  • Contact Us
 

strigi/src/streams

inputstreamreader.cpp

Go to the documentation of this file.
00001 /* This file is part of Strigi Desktop Search
00002  *
00003  * Copyright (C) 2006 Jos van den Oever <jos@vandenoever.info>
00004  *
00005  * This library is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU Library General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * This library 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 GNU
00013  * Library General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU Library General Public License
00016  * along with this library; see the file COPYING.LIB.  If not, write to
00017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019  */
00020 
00021 #ifdef HAVE_CONFIG_H
00022 # include "config.h"
00023 #endif
00024 
00025 #include "inputstreamreader.h"
00026 #include <strigi/strigiconfig.h>
00027 #include <cerrno>
00028 
00029 #ifdef ICONV_SECOND_ARGUMENT_IS_CONST
00030      #define ICONV_CONST const
00031 #else
00032      #define ICONV_CONST
00033 #endif
00034 
00035 using namespace Strigi;
00036 
00037 InputStreamReader::InputStreamReader(InputStream* i, const char* enc) {
00038     m_status = Ok;
00039     finishedDecoding = false;
00040     input = i;
00041     if (enc == 0) enc = "UTF-8";
00042 #ifdef _LIBICONV_H
00043     if (sizeof(wchar_t) == 4) {
00044         converter = iconv_open("UCS-4-INTERNAL", enc);
00045     } if (sizeof(wchar_t) == 2) {
00046         converter = iconv_open("UCS-2-INTERNAL", enc);
00047 #else
00048     if (sizeof(wchar_t) > 1) {
00049         converter = iconv_open("WCHAR_T", enc);
00050 #endif
00051     } else {
00052         converter = iconv_open("ASCII", enc);
00053     }
00054 
00055     // check if the converter is valid
00056     if (converter == (iconv_t) -1) {
00057         m_error = "conversion from '";
00058         m_error += enc;
00059         m_error += "' not available.";
00060         m_status = Error;
00061         return;
00062     }
00063     charbuf.setSize(262);
00064     //mark(262);
00065     charsLeft = 0;
00066 }
00067 InputStreamReader::~InputStreamReader() {
00068     if (converter != (iconv_t) -1) {
00069         iconv_close(converter);
00070     }
00071 }
00072 int32_t
00073 InputStreamReader::decode(wchar_t* start, int32_t space) {
00074     // decode from charbuf
00075     ICONV_CONST char *inbuf = charbuf.readPos;
00076     size_t inbytesleft = charbuf.avail;
00077     size_t outbytesleft = sizeof(wchar_t)*space;
00078     char *outbuf = (char*)start;
00079     size_t r = iconv(converter, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
00080     int32_t nwritten;
00081     if (r == (size_t)-1) {
00082         switch (errno) {
00083         case EILSEQ: //invalid multibyte sequence
00084             m_error = "Invalid multibyte sequence.";
00085             m_status = Error;
00086             return -1;
00087         case EINVAL: // last character is incomplete
00088             // move from inbuf to the end to the start of
00089             // the buffer
00090             std::memmove(charbuf.start, inbuf, inbytesleft);
00091             charbuf.readPos = charbuf.start;
00092             charbuf.avail = inbytesleft;
00093             nwritten = ((wchar_t*)outbuf) - start;
00094             break;
00095         case E2BIG: // output buffer is full
00096             charbuf.readPos += charbuf.avail - inbytesleft;
00097             charbuf.avail = inbytesleft;
00098             nwritten = space;
00099             break;
00100         default:
00101             char tmp[10];
00102             snprintf(tmp, 10, "%i", errno);
00103             m_error = "inputstreamreader error: ";
00104             m_error.append(tmp);
00105             fprintf(stderr, "inputstreamreader::error %d\n", errno);
00106             m_status = Error;
00107             return -1;
00108         }
00109     } else { //input sequence was completely converted
00110         charbuf.readPos = charbuf.start;
00111         charbuf.avail = 0;
00112         nwritten = ((wchar_t*)outbuf) - start;
00113         if (input == 0) {
00114             finishedDecoding = true;
00115         }
00116     }
00117     return nwritten;
00118 }
00119 int32_t
00120 InputStreamReader::fillBuffer(wchar_t* start, int32_t space) {
00121     // fill up charbuf
00122     if (input && charbuf.readPos == charbuf.start) {
00123         const char *begin;
00124         int32_t numRead;
00125         numRead = input->read(begin, 1, charbuf.size - charbuf.avail);
00126         //printf("filled up charbuf\n");
00127         if (numRead < -1) {
00128             m_error = input->error();
00129             m_status = Error;
00130             input = 0;
00131             return numRead;
00132         }
00133         if (numRead < 1) {
00134             // signal end of input buffer
00135             input = 0;
00136             if (charbuf.avail) {
00137                 m_error = "stream ends on incomplete character";
00138                 m_status = Error;
00139             }
00140             return -1;
00141         }
00142         // copy data into other buffer
00143         std::memmove(charbuf.start + charbuf.avail, begin, numRead);
00144         charbuf.avail = numRead + charbuf.avail;
00145     }
00146     // decode
00147     int32_t n = decode(start, space);
00148     //printf("decoded %i\n", n);
00149     return n;
00150 }

strigi/src/streams

Skip menu "strigi/src/streams"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

KDE Support

Skip menu "KDE Support"
  • akonadi
  • Decibel
  • grantlee
  • kdewin
  • phonon
  •     Backend
  • polkit-qt
  • qca
  • qimageblitz
  • soprano
  • strigi
  •     searchclient
  •     streamanalyzer
  •     streams
Generated for KDE Support by doxygen 1.5.9-20090814
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal