doc_and_tools/tools/apps/cvstr/cvstr.cpp

260 lines
5.0 KiB
C++

// cvstr.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <Windows.h>
#include <file/file_util.h>
#include <coding/coding.h>
#include <stdio.h>
#include <iostream>
#include <string>
static bool conver_define_str(const wchar_t* file);
int _tmain(int argc, _TCHAR* argv[])
{
size_t size = 1024 * 1024 * 1024 * 1;
char *buf = new char[size];
buf = new char[size];
if (argc < 2)
{
wchar_t *pe = wcsrchr(argv[0], L'\\');
if (pe++ == NULL)
pe = argv[0];
if (wcsrchr(pe, L'.'))
wcsrchr(pe, L'.')[0] = 0;
std::wcout << L" Usage: " << pe << L" <path-file>\r\n";
}
for (int i = 1; i < argc; ++i)
{
std::wstring str(argv[i]);
STR_TO_ABSOLUTE_PATH(str);
conver_define_str(str.c_str());
}
return 0;
}
INTER_MODULE_CALLBACK(got_str)
{
*((std::string*)param) += std::string(data, len);
return inter_module_data::SET_RESULT_CONTINUE;
}
INTER_MODULE_CALLBACK(got_wstr)
{
*((std::wstring*)param) += std::wstring((wchar_t*)data, len / 2);
return inter_module_data::SET_RESULT_CONTINUE;
}
std::wstring get_line(const std::wstring& src, int start)
{
std::wstring line(src.substr(start));
start = line.find(L"\n");
if (start++ != std::wstring::npos)
line.erase(start);
return line;
}
std::wstring find_hz(std::wstring& src, int* off)
{
std::wstring mark(L"#define"), ret(L"");
size_t pos = src.find(mark);
if (pos == 0)
{
pos = src.find(L"\"");
if (pos++ != std::wstring::npos)
{
bool ascii = true;
size_t end = pos;
while (src[end])
{
if (src[end] == L'\\')
end++;
else if (src[end] == L'\"')
{
ret = src.substr(pos, end - pos);
if (off)
*off = pos;
break;
}
end++;
}
for (size_t i = 0; i < ret.length(); ++i)
{
if (ret[i] > 0x7f)
{
ascii = false;
break;
}
}
if (ascii)
{
ret = L"";
}
}
}
return ret;
}
std::wstring to_oct(const std::string& str)
{
std::wstring oct(L"");
for (int i = 0; i < str.length(); ++i)
{
wchar_t buf[8] = { 0 };
if ((unsigned char)str[i] <= 0x7f)
buf[0] = str[i];
else
swprintf_s(buf, _countof(buf) - 1, L"\\%03o", (unsigned char)str[i]);
oct += buf;
}
return oct;
}
bool is_oct(const wchar_t* str)
{
bool yes = true;
for (int i = 0; i < lstrlenW(str); ++i)
{
if ((unsigned short)str[i] > 0x7f)
{
yes = false;
break;
}
}
return yes;
}
std::string revert_oct(const wchar_t* str)
{
std::wstring org(L"");
std::string ansi("");
for (int i = 0; i < lstrlenW(str); ++i)
{
if (str[i] == L'\\')
{
if (i + 3 < lstrlenW(str))
{
char ch = str[i + 1] - L'0';
ch *= 8;
ch += str[i + 2] - L'0';
ch *= 8;
ch += str[i + 3] - L'0';
i += 3;
ansi.append(1, ch);
}
}
else
{
if (ansi.length())
{
coding_util::utf8_2_unicode(ansi.c_str(), got_wstr, &org);
ansi = "";
}
org.append(1, str[i]);
}
}
if (ansi.length())
{
coding_util::utf8_2_unicode(ansi.c_str(), got_wstr, &org);
ansi = "";
}
org.append(2, 0);
coding_util::unicode_2_ansi(org.c_str(), got_str, &ansi);
return ansi;
}
static bool conver_define_str(const wchar_t* file)
{
std::wstring cont(L""), start(L"#define");
std::string org("");
size_t pos = 0;
bool unic = false, utf8 = false;
int changed = 0;
if (file_util::load_file(file, got_str, &org))
{
coding_util::unicode_2_ansi(file, got_str, &org);
std::cout << "Load file '" << org.c_str() << "' failed.\r\n";
if (is_oct(file))
{
std::cout << "Origin: " << revert_oct(file).c_str() << std::endl;
}
else
{
org = "";
coding_util::unicode_2_utf8(file, got_str, &org);
cont = to_oct(org);
org = "";
coding_util::unicode_2_ansi(cont.c_str(), got_str, &org);
std::cout << "Oct: " << org.c_str() << std::endl;
}
return false;
}
coding_util::bom::to_unicode(org.c_str(), org.length(), got_wstr, &cont);
unic = coding_util::bom::is_unicode(org.c_str(), NULL);
utf8 = coding_util::bom::is_utf8(org.c_str());
pos = cont.find(start);
while (pos != std::wstring::npos)
{
if (pos == 0 || cont[pos - 1] == L'\n')
{
int off = 0;
std::wstring line(get_line(cont, pos)),
hz(find_hz(line, &off));
if (hz.length())
{
int len = line.length();
std::string str_utf8("");
std::wstring oct(L"");
coding_util::unicode_2_utf8(hz.c_str(), got_str, &str_utf8);
oct = to_oct(str_utf8);
line.replace(off, hz.length(), oct);
cont.insert(pos, L"// ");
pos += len + 3;
cont.insert(pos, line);
changed++;
}
}
pos = cont.find(L"#define", pos + start.length());
}
std::wcout << L"changed " << changed << " HZ in file: " << file << std::endl;
if (changed)
{
org = "";
if (unic)
coding_util::bom::from_unicode(cont.c_str(), cont.length() * 2, got_str, &org);
else if (utf8)
{
std::string t("");
coding_util::unicode_2_utf8(cont.c_str(), got_str, &t);
coding_util::bom::from_utf8(t.c_str(), t.length(), got_str, &org);
}
else
coding_util::unicode_2_ansi(cont.c_str(), got_str, &org);
file_util::save_2_file(org.c_str(), org.length(), file);
}
}