1 | #include <string> |
---|
2 | #include <iostream> |
---|
3 | #include <cstdlib> |
---|
4 | #include <cstdio> |
---|
5 | #include <strings.h> |
---|
6 | #include <clocale> |
---|
7 | #include <langinfo.h> |
---|
8 | //#include <sys/param.h> |
---|
9 | |
---|
10 | std::wstring To16Char(const std::string &asString) |
---|
11 | { |
---|
12 | std::wstring wsTemp; |
---|
13 | size_t needed = std::mbstowcs(NULL, &asString[0],0); |
---|
14 | wsTemp.resize(needed); |
---|
15 | std::mbstowcs(&wsTemp[0],&asString[0],needed); |
---|
16 | |
---|
17 | return wsTemp; |
---|
18 | } |
---|
19 | |
---|
20 | //----------------------------------------------------------------------- |
---|
21 | |
---|
22 | std::string To8Char(const std::wstring &awsString) |
---|
23 | { |
---|
24 | std::string sTemp; |
---|
25 | size_t needed = std::wcstombs(NULL,&awsString[0],0); |
---|
26 | sTemp.resize(needed); |
---|
27 | std::wcstombs(&sTemp[0],&awsString[0],needed); |
---|
28 | |
---|
29 | return sTemp; |
---|
30 | } |
---|
31 | |
---|
32 | void ReadFile(std::string asFile) |
---|
33 | { |
---|
34 | FILE *fp = fopen(asFile.c_str(),"r"); |
---|
35 | if (fp) { |
---|
36 | char str[1024]; |
---|
37 | fgets(str, 1024, fp); |
---|
38 | printf("File: %s Contents: %s\n", asFile.c_str(), str); |
---|
39 | fclose(fp); |
---|
40 | } else { |
---|
41 | std::cerr << "Error opening file:" << asFile << std::endl; |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | int main (int argc, const char* const argv[]) |
---|
47 | { |
---|
48 | if(!std::setlocale(LC_CTYPE, "")) { |
---|
49 | std::cerr << "Can't set the specified locale! Check LANG, LC_CTYPE, LC_ALL." << std::endl; |
---|
50 | return 1; |
---|
51 | } |
---|
52 | char *charset = nl_langinfo(CODESET); |
---|
53 | bool utf8_mode = (strcasecmp(charset, "UTF-8") == 0); |
---|
54 | std::cerr << "UTF-8 Charset " |
---|
55 | << std::string(utf8_mode ? "is" : "not") |
---|
56 | << " available.\nCurrent LANG is " << std::string(getenv("LANG")) |
---|
57 | << "\nCharset: " << charset <<"\n"; |
---|
58 | |
---|
59 | std::string utf8 = "Hämtningar"; |
---|
60 | std::cout << "This is a UTF-8 Conversion Test" << std::endl; |
---|
61 | |
---|
62 | std::wstring utf32 = L""; |
---|
63 | |
---|
64 | std::cout << "Initial UTF-8 " << utf8 << std::endl; |
---|
65 | utf32 = To16Char(utf8); |
---|
66 | |
---|
67 | printf("UTF-32 Version: '%ls'\n", utf32.c_str()); |
---|
68 | |
---|
69 | utf8 = To8Char(utf32); |
---|
70 | |
---|
71 | printf("UTF-8 Again: '%s'\n", utf8.c_str()); |
---|
72 | |
---|
73 | #ifdef TESTFILE |
---|
74 | ReadFile("test.txt"); |
---|
75 | |
---|
76 | char rpath[PATH_MAX]; |
---|
77 | realpath("test.txt", rpath); |
---|
78 | printf("Full Path: %s\n",rpath); |
---|
79 | ReadFile(rpath); |
---|
80 | |
---|
81 | realpath(To8Char(L"test.txt").c_str(),rpath); |
---|
82 | utf32 = To16Char(rpath); |
---|
83 | printf("Full Path: %ls\n", utf32.c_str()); |
---|
84 | utf8 = To8Char(utf32); |
---|
85 | printf("Full Path(8): %s\n",utf8.c_str()); |
---|
86 | ReadFile(utf8.c_str()); |
---|
87 | #endif |
---|
88 | return 0; |
---|
89 | } |
---|
90 | |
---|
91 | // vim: set noet ts=4: |
---|