"Fossies" - the Fresh Open Source Software Archive

Member "netxms-3.8.166/src/libnetxms/iconv.cpp" (23 Feb 2021, 2520 Bytes) of package /linux/misc/netxms-3.8.166.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 "iconv.cpp" see the Fossies "Dox" file reference documentation.

    1 /*
    2  ** NetXMS - Network Management System
    3  ** Copyright (C) 2003-2020 Raden Solutions
    4  **
    5  ** This program is free software; you can redistribute it and/or modify
    6  ** it under the terms of the GNU Lesser General Public License as published
    7  ** by the Free Software Foundation; either version 3 of the License, or
    8  ** (at your option) any later version.
    9  **
   10  ** This program is distributed in the hope that it will be useful,
   11  ** but WITHOUT ANY WARRANTY; without even the implied warranty of
   12  ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   13  ** GNU General Public License for more details.
   14  **
   15  ** You should have received a copy of the GNU Lesser General Public License
   16  ** along with this program; if not, write to the Free Software
   17  ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   18  **
   19  ** File: iconv.cpp
   20  **
   21  **/
   22 
   23 #include "libnetxms.h"
   24 
   25 #if HAVE_ICONV_H
   26 #include <iconv.h>
   27 #endif
   28 
   29 #if !defined(__DISABLE_ICONV)
   30 
   31 /**
   32  * iconv descriptor
   33  */
   34 class IconvDescriptor
   35 {
   36 public:
   37    char *from;
   38    char *to;
   39    iconv_t cd;
   40    bool busy;
   41 
   42    IconvDescriptor(iconv_t _cd, const char *_from, const char *_to)
   43    {
   44       cd = _cd;
   45       from = strdup(_from);
   46       to = strdup(_to);
   47       busy = true;
   48    }
   49 
   50    ~IconvDescriptor()
   51    {
   52       free(from);
   53       free(to);
   54       iconv_close(cd);
   55    }
   56 };
   57 
   58 /**
   59  * iconv descriptor cache
   60  */
   61 static ObjectArray<IconvDescriptor> s_cache(16, 16, Ownership::True);
   62 
   63 /**
   64  * Cache access mutex
   65  */
   66 static Mutex s_cacheLock;
   67 
   68 /**
   69  * Open descriptor
   70  */
   71 iconv_t IconvOpen(const char *to, const char *from)
   72 {
   73    iconv_t cd = (iconv_t)(-1);
   74 
   75    s_cacheLock.lock();
   76 
   77    for(int i = 0; i < s_cache.size(); i++)
   78    {
   79       IconvDescriptor *d = s_cache.get(i);
   80       if (!d->busy && !strcmp(from, d->from) && !strcmp(to, d->to))
   81       {
   82          d->busy = true;
   83          cd = d->cd;
   84          break;
   85       }
   86    }
   87 
   88    if (cd == (iconv_t)(-1))
   89    {
   90       cd = iconv_open(to, from);
   91       if (cd != (iconv_t)(-1))
   92       {
   93          IconvDescriptor *d = new IconvDescriptor(cd, from, to);
   94          s_cache.add(d);
   95       }
   96    }
   97 
   98    s_cacheLock.unlock();
   99    return cd;
  100 }
  101 
  102 /**
  103  * Close descriptor
  104  */
  105 void IconvClose(iconv_t cd)
  106 {
  107    s_cacheLock.lock();
  108    for(int i = 0; i < s_cache.size(); i++)
  109    {
  110       IconvDescriptor *d = s_cache.get(i);
  111       if (d->cd == cd)
  112       {
  113 #if HAVE_ICONV_STATE_RESET
  114          iconv(cd, NULL, NULL, NULL, NULL);
  115 #endif
  116          d->busy = false;
  117          break;
  118       }
  119    }
  120    s_cacheLock.unlock();
  121 }
  122 
  123 #endif /* !defined(__DISABLE_ICONV) */