stripped out UTF components
This commit is contained in:
parent
d16cf33d5c
commit
82b081ff6c
@ -1,99 +0,0 @@
|
|||||||
|
|
||||||
#ifndef REFPTR_H
|
|
||||||
#define REFPTR_H REFPTR_H
|
|
||||||
|
|
||||||
/* Author: Josh Holtrop
|
|
||||||
* Purpose: Provide a reference-counting pointer-like first order
|
|
||||||
* C++ object that will free the object it is pointing to when
|
|
||||||
* all references to it have been destroyed.
|
|
||||||
* This implementation does not solve the circular reference problem.
|
|
||||||
* I was not concerned with that when developing this class.
|
|
||||||
*/
|
|
||||||
#include <stdlib.h> /* NULL */
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
class refptr
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
refptr<T>();
|
|
||||||
refptr<T>(T * ptr);
|
|
||||||
refptr<T>(const refptr<T> & orig);
|
|
||||||
refptr<T> & operator=(const refptr<T> & orig);
|
|
||||||
refptr<T> & operator=(T * ptr);
|
|
||||||
~refptr<T>();
|
|
||||||
T & operator*() const { return *m_ptr; }
|
|
||||||
T * operator->() const { return m_ptr; }
|
|
||||||
bool isNull() const { return m_ptr == NULL; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
void cloneFrom(const refptr<T> & orig);
|
|
||||||
void destroy();
|
|
||||||
|
|
||||||
T * m_ptr;
|
|
||||||
int * m_refCount;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <typename T> refptr<T>::refptr()
|
|
||||||
{
|
|
||||||
m_ptr = NULL;
|
|
||||||
m_refCount = NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> refptr<T>::refptr(T * ptr)
|
|
||||||
{
|
|
||||||
m_ptr = ptr;
|
|
||||||
m_refCount = new int;
|
|
||||||
*m_refCount = 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> refptr<T>::refptr(const refptr<T> & orig)
|
|
||||||
{
|
|
||||||
cloneFrom(orig);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> refptr<T> & refptr<T>::operator=(const refptr<T> & orig)
|
|
||||||
{
|
|
||||||
destroy();
|
|
||||||
cloneFrom(orig);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> refptr<T> & refptr<T>::operator=(T * ptr)
|
|
||||||
{
|
|
||||||
destroy();
|
|
||||||
m_ptr = ptr;
|
|
||||||
m_refCount = new int;
|
|
||||||
*m_refCount = 1;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> void refptr<T>::cloneFrom(const refptr<T> & orig)
|
|
||||||
{
|
|
||||||
this->m_ptr = orig.m_ptr;
|
|
||||||
this->m_refCount = orig.m_refCount;
|
|
||||||
if (m_refCount != NULL)
|
|
||||||
(*m_refCount)++;
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> refptr<T>::~refptr()
|
|
||||||
{
|
|
||||||
destroy();
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename T> void refptr<T>::destroy()
|
|
||||||
{
|
|
||||||
if (m_refCount != NULL)
|
|
||||||
{
|
|
||||||
if (*m_refCount <= 1)
|
|
||||||
{
|
|
||||||
delete m_ptr;
|
|
||||||
delete m_refCount;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
(*m_refCount)--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
73
serialize.cc
73
serialize.cc
@ -1,73 +0,0 @@
|
|||||||
|
|
||||||
#include "serialize.h"
|
|
||||||
#include <string.h>
|
|
||||||
#include <iconv.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <errno.h>
|
|
||||||
#include <endian.h>
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
refptr< vector<unichar_t> > deserialize(const char * encoding, istream & in)
|
|
||||||
{
|
|
||||||
const int buf_size = 200;
|
|
||||||
int num_read;
|
|
||||||
char * inbuf = new char[buf_size];
|
|
||||||
char * inbuf_ptr = (char *) &inbuf[0];
|
|
||||||
unichar_t * outbuf = new unichar_t[buf_size];
|
|
||||||
char * outbuf_ptr;
|
|
||||||
size_t chars_converted, inbytesleft = 0, outbytesleft;
|
|
||||||
const char * to_encoding
|
|
||||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
||||||
= "UCS-4LE";
|
|
||||||
#else
|
|
||||||
= "UCS-4BE";
|
|
||||||
#endif
|
|
||||||
refptr< vector<unichar_t> > ucs = new vector<unichar_t>();
|
|
||||||
|
|
||||||
iconv_t cd = iconv_open(/* to */ to_encoding, /* from */ encoding);
|
|
||||||
if (cd == (iconv_t) -1)
|
|
||||||
{
|
|
||||||
cerr << "iconv_open() error" << endl;
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
outbuf_ptr = (char *) &outbuf[0];
|
|
||||||
outbytesleft = buf_size * sizeof(outbuf[0]);
|
|
||||||
iconv(cd, NULL, NULL, &outbuf_ptr, &outbytesleft);
|
|
||||||
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
in.read(inbuf_ptr, buf_size * sizeof(inbuf[0]) - inbytesleft);
|
|
||||||
num_read = in.gcount();
|
|
||||||
if (num_read <= 0)
|
|
||||||
break;
|
|
||||||
inbytesleft += num_read;
|
|
||||||
outbuf_ptr = (char *) &outbuf[0];
|
|
||||||
outbytesleft = buf_size * sizeof(outbuf[0]);
|
|
||||||
chars_converted = iconv(cd, &inbuf_ptr, &inbytesleft,
|
|
||||||
&outbuf_ptr, &outbytesleft);
|
|
||||||
if (chars_converted == (size_t) -1)
|
|
||||||
{
|
|
||||||
perror("iconv()");
|
|
||||||
}
|
|
||||||
if (inbytesleft > 0)
|
|
||||||
{
|
|
||||||
memmove(&inbuf[0], inbuf_ptr, inbytesleft);
|
|
||||||
}
|
|
||||||
inbuf_ptr = ((char *) &inbuf[0]) + inbytesleft;
|
|
||||||
for (int i = 0;
|
|
||||||
i < (((buf_size * sizeof(outbuf[0])) - outbytesleft)
|
|
||||||
/ sizeof(outbuf[0]));
|
|
||||||
i++)
|
|
||||||
{
|
|
||||||
ucs->push_back(outbuf[i]);
|
|
||||||
}
|
|
||||||
if (in.eof())
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
delete[] inbuf;
|
|
||||||
delete[] outbuf;
|
|
||||||
iconv_close(cd);
|
|
||||||
return ucs;
|
|
||||||
}
|
|
13
serialize.h
13
serialize.h
@ -1,13 +0,0 @@
|
|||||||
|
|
||||||
#ifndef SERIALIZE_H
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <iconv.h>
|
|
||||||
#include <vector>
|
|
||||||
#include "refptr/refptr.h"
|
|
||||||
#include "unicode.h"
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
refptr< vector<unichar_t> > deserialize(const char * encoding, istream & in);
|
|
||||||
|
|
||||||
#endif
|
|
33
unicode.cc
33
unicode.cc
@ -1,33 +0,0 @@
|
|||||||
|
|
||||||
#include "unicode.h"
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
unistring & unistring::operator=(const char * ascii_str)
|
|
||||||
{
|
|
||||||
chars.clear();
|
|
||||||
const char * as_ptr = ascii_str;
|
|
||||||
while (*as_ptr != '\0')
|
|
||||||
{
|
|
||||||
chars.push_back(*as_ptr);
|
|
||||||
}
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
unistring & unistring::operator+=(const unichar_t c)
|
|
||||||
{
|
|
||||||
chars.push_back(c);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool unistring::operator==(const char * ascii_str)
|
|
||||||
{
|
|
||||||
int len = chars.size();
|
|
||||||
if (len != strlen(ascii_str))
|
|
||||||
return false;
|
|
||||||
for (int i = 0; i < len; i++)
|
|
||||||
{
|
|
||||||
if (chars[i] != ascii_str[i])
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
21
unicode.h
21
unicode.h
@ -1,21 +0,0 @@
|
|||||||
|
|
||||||
#ifndef UNICODE_H
|
|
||||||
#define UNICODE_H
|
|
||||||
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
typedef uint32_t unichar_t;
|
|
||||||
|
|
||||||
class unistring
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
unistring & operator=(const char * ascii_str);
|
|
||||||
unistring & operator+=(const unichar_t c);
|
|
||||||
bool operator==(const char * ascii_str);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
std::vector<unichar_t> chars;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
Loading…
x
Reference in New Issue
Block a user