ここでは、ソースのみ記載します。

ソースの説明はこちら を参照ください。


main.cppファイル


//

#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <vector>
#include <sstream>
#include <functional>
#include <iomanip>
#include <fstream>

#include "boost/shared_ptr.hpp"


#include "code.hpp"


using namespace std;
using namespace boost;
using namespace nana;





void test1()
{
cout << "おまけ:char*で、forで2バイトずつ分解" << endl;

//
FixedCharSet fcs(2);
const char* st = "abccdefg";
CharMCharIterator i = mcharIterator(st, &fcs);
CharMCharIterator end;

for(; i!=end; ++i){
cout << *i << ":";
}
cout << endl << endl;
};


void test2()
{
cout << "おまけ:stringで、forで2バイトずつ分解。同じようにできます。" << endl;

//
FixedCharSet fm(2);
string st("abccdefg");
StringMCharIterator i = mcharIterator(st, &fm);
StringMCharIterator end;

for(; i!=end; ++i){
cout << *i << ":";
}
cout << endl << endl;
};


void test3()
{
cout << "forで1文字ずつ分解" << endl;

//
const char* st = "abcあdいe漏fg";
CharMCharIterator i = mcharIterator(st, getCharSet("sjis")), end;

for(; i!=end; ++i){
cout << *i << ":";
}
cout << endl << endl;

};


void test4()
{
cout << "'あ'を'#'にreplaceで置換しながらコピー" << endl;

//
const char* st = "abcあdいe漏fg";
//コピー先の文字列ストリーム
ostringstream oss;
ostream_iterator<FixedMChar> oite(oss);
CharMCharIterator i = mcharIterator(st, getCharSet("sjis"));
CharMCharIterator end;

//STLのアルゴリズム
replace_copy(i, end, oite, FixedMChar("あ"), FixedMChar("#"));

cout << oss.str() << ":replace" << endl << endl;
};


void test5()
{
cout << "複数の文字いずれかを検索する" << endl;

//いずれかの文字を探す
const char* st = "abc票あdいe字fg";
const char* st2= "j\\あg";
CharMCharIterator f, i;

//STLのアルゴリズム
f = find_first_of(
mcharIterator(st), CharMCharIterator(),
mcharIterator(st2), CharMCharIterator()
);

cout << *f << " : address=" << f->pos() << endl << endl;

};


void test6()
{
cout << "文字列を検索する" << endl;

//文字列を探す
const char* st = "abc票あいdいあい唄e漏fg";
string st2= "唄e";
CharMCharIterator f, end;
int i = nana::searchStr(mcharIterator(st), strlen(st), st2);
if(i != -1){
cout << st2 << " : address=" << &st[i];
}else{
cout << "not found";
}
cout << endl << endl;
};


void test7()
{
cout << "文字数を数える" << endl;

//
const char* st = "ab票cあdいe漏fg";
CharMCharIterator i = mcharIterator(st), end;
//
int cnt = std::distance(i, end);

cout << st << " :文字数=" << cnt << endl << endl;
};

void test8()
{
cout << "ファイルにEUCにエンコードした結果を保存する" << endl << endl;

//
const char* st = "ab票cあdいe漏fg";
CharMCharIterator i = mcharIterator(st), end;
//ファイルをフルパスで指定してください
ofstream of("test.txt");
for_each(i, end, Sjis2Euc(of));
};

void test9()
{
cout << "UTF8ファイルを読み込んで1文字ずつ区切ってファイルに保存" << endl;

//ファイルをフルパスで指定してください
ifstream in("test_utf8_in.txt");
ofstream ou("test_utf8_out.txt");
//
ostringstream oss;
oss << in.rdbuf();
string str = oss.str();

StringMCharIterator i = mcharIterator(str, getCharSet("utf8")), end;
for(; i != end ; ++i){
ou << *i << ":";
}

cout << endl;
};

void test10()
{
cout << "JISファイルを読み込んで1文字ずつ区切ってファイルに保存" << endl << endl;

//
//ファイルをフルパスで指定してください
ifstream in("test_jis_in.txt");
ofstream ou("test_jis_out.txt");
//
ostringstream oss;
oss << in.rdbuf();
string str = oss.str();

StringMCharIterator i = mcharIterator(str, getCharSet("jis")), end;
//jisは特殊なのでいろいろ準備
string seq;
//区切り文字を用意しておく。
char sep[] = {0x1B,'(', 'B', ':', '\0'};
//ループ
for(; i != end ; ++i){
//制御文字を含む場合、3バイト以上になる。制御文字を保存しておく。
if(i->size() >= 3) seq = i->str().substr(0,3);
ou << *i << sep << seq;
}
};




int main(int argc, char *argv[]) {
CharSetDefault::set(getCharSet("sjis"));

test1();
test2();
test3();
test4();
test5();
test6();
test7();

//以下を試したい場合は、関数内のファイルパスを変更してください。
test8();
test9();
test10();

system("PAUSE");
return EXIT_SUCCESS;
};