arabica/examples/Utils/transcode.cpp

158 lines
4 KiB
C++
Raw Normal View History

2003-09-02 11:23:52 +00:00
// transcode.cpp
//
// Warning! Contains ifdef hackery
//
///////////////////////////////////
2003-09-02 11:23:52 +00:00
#include <iostream>
#include <Utils/convert_adaptor.h>
#include <Utils/utf8iso88591codecvt.h>
#include <Utils/iso88591utf8codecvt.h>
#include <Utils/rot13codecvt.h>
2003-09-10 11:01:47 +00:00
#include <Utils/base64codecvt.h>
2003-09-02 11:23:52 +00:00
#include <Utils/utf8ucs2codecvt.h>
#include <Utils/utf16utf8codecvt.h>
#include <Utils/utf16beucs2codecvt.h>
#include <Utils/utf16leucs2codecvt.h>
2003-09-18 08:25:27 +00:00
using namespace Arabica::convert;
iconvert_adaptor<char> iByteConvertor(std::cin);
oconvert_adaptor<char> oByteConvertor(std::cout);
#ifndef ARABICA_NO_WCHAR_T
2003-09-18 08:25:27 +00:00
typedef iconvert_adaptor<wchar_t, std::char_traits<wchar_t>, char, std::char_traits<char> > Widener;
typedef oconvert_adaptor<wchar_t, std::char_traits<wchar_t>, char, std::char_traits<char> > Narrower;
Widener iCharAdaptor(iByteConvertor);
Narrower oCharAdaptor(oByteConvertor);
#endif
2003-09-18 08:25:27 +00:00
void transcode();
2003-09-18 08:25:27 +00:00
void imbueCodecvts(int argc, const char* argv[]);
2003-09-02 11:23:52 +00:00
int main(int argc, const char* argv[])
{
2003-09-18 08:25:27 +00:00
imbueCodecvts(argc, argv);
2003-09-02 11:23:52 +00:00
transcode();
return 0;
} // main
#ifndef ARABICA_NO_WCHAR_T
void transcode()
{
2003-09-18 08:25:27 +00:00
int count = 0;
wchar_t c = iCharAdaptor.get();
while(!iCharAdaptor.eof())
2003-09-02 11:23:52 +00:00
{
2003-09-18 08:25:27 +00:00
oCharAdaptor << c;
if(count == 1024)
{
oCharAdaptor.flush();
oByteConvertor.flush();
count = 0;
} // if ...
c = iCharAdaptor.get();
}
oCharAdaptor.flush();
oByteConvertor.flush();
} // transcode
#else
void transcode()
{
int count = 0;
char c = iByteConvertor.get();
while(!iByteConvertor.eof())
{
oByteConvertor << c;
if(count == 1024)
{
oByteConvertor.flush();
count = 0;
} // if ...
c = iByteConvertor.get();
} // while
2003-09-18 08:25:27 +00:00
oByteConvertor.flush();
} // transcode
#endif
2003-09-02 11:23:52 +00:00
2003-09-18 08:25:27 +00:00
void imbueCodecvts(int argc, const char* argv[])
{
#ifndef ARABICA_NO_WCHAR_T
2003-09-18 08:25:27 +00:00
oCharAdaptor.imbue(std::locale(oCharAdaptor.getloc(), new utf16beucs2codecvt()));
#endif
2003-09-18 08:25:27 +00:00
for(int i = 1; i < argc; ++i)
{
std::string io(argv[i]);
bool input = true;
if(io == "-i" || io == "--input")
input = true;
else if(io == "-o" || io == "--output")
input = false;
else
{
std::cerr << argv[0] << " [(-i|--input) input-encoding] [(-o|--output) output-encoding] " << std::endl;
std::exit(0);
} //
++i;
if(i >= argc)
{
std::cerr << argv[0] << " [(-i|--input) input-encoding] [(-o|--output) output-encoding] " << std::endl;
std::exit(0);
} //
std::string cvt(argv[i]);
2003-09-22 14:43:41 +00:00
if(input)
2003-09-18 08:25:27 +00:00
{
2003-09-22 14:43:41 +00:00
if(cvt == "rot13")
2003-09-18 08:25:27 +00:00
iByteConvertor.imbue(std::locale(iByteConvertor.getloc(), new rot13codecvt()));
2003-09-22 14:43:41 +00:00
else if(cvt == "base64")
iByteConvertor.imbue(std::locale(iByteConvertor.getloc(), new base64codecvt()));
#ifndef ARABICA_NO_WCHAR_T
2003-09-22 14:43:41 +00:00
else if(cvt == "utf8")
iCharAdaptor.imbue(std::locale(iCharAdaptor.getloc(), new utf8ucs2codecvt()));
else if(cvt == "utf16be")
iCharAdaptor.imbue(std::locale(iCharAdaptor.getloc(), new utf16beucs2codecvt()));
else if(cvt == "utf16le")
iCharAdaptor.imbue(std::locale(iCharAdaptor.getloc(), new utf16leucs2codecvt()));
#endif
2003-09-18 08:25:27 +00:00
else
2003-09-22 14:43:41 +00:00
{
std::cerr << cvt << " is not a valid input encoding." << std::endl;
std::exit(0);
}
2003-09-18 08:25:27 +00:00
}
2003-09-22 14:43:41 +00:00
else
2003-09-18 08:25:27 +00:00
{
2003-09-22 14:43:41 +00:00
if(cvt == "rot13")
oByteConvertor.imbue(std::locale(oByteConvertor.getloc(), new rot13codecvt()));
else if(cvt == "base64")
2003-09-18 08:25:27 +00:00
oByteConvertor.imbue(std::locale(oByteConvertor.getloc(), new base64codecvt()));
#ifndef ARABICA_NO_WCHAR_T
2003-09-22 14:43:41 +00:00
else if(cvt == "utf8")
2003-09-18 08:25:27 +00:00
oCharAdaptor.imbue(std::locale(oCharAdaptor.getloc(), new utf8ucs2codecvt()));
2003-09-22 14:43:41 +00:00
else if(cvt == "utf16be")
2003-09-18 08:25:27 +00:00
oCharAdaptor.imbue(std::locale(oCharAdaptor.getloc(), new utf16beucs2codecvt()));
2003-09-22 14:43:41 +00:00
else if(cvt == "utf16le")
2003-09-18 08:25:27 +00:00
oCharAdaptor.imbue(std::locale(oCharAdaptor.getloc(), new utf16leucs2codecvt()));
#endif
2003-09-22 14:43:41 +00:00
else
{
std::cerr << cvt << " is not a valid output encoding." << std::endl;
std::exit(0);
}
2003-09-18 08:25:27 +00:00
}
} // for ...
} // imbueCodeCvts