OSDN Git Service

Merge from rev.7128:7151
[winmerge-jp/winmerge-jp.git] / Src / FileTextEncoding.cpp
1 /**
2  * @file  FileTextEncoding.cpp
3  *
4  * @brief Implementation of FileTextEncoding structure
5  */
6 // ID line follows -- this is updated by SVN
7 // $Id: FileTextEncoding.cpp 7172 2010-05-19 12:57:18Z jtuc $
8
9 #include "stdafx.h"
10 #include "unicoder.h"
11 #include "FileTextEncoding.h"
12
13 #ifdef _DEBUG
14 #define new DEBUG_NEW
15 #undef THIS_FILE
16 static char THIS_FILE[] = __FILE__;
17 #endif
18
19 FileTextEncoding::FileTextEncoding()
20 {
21         Clear();
22 }
23
24 /**
25  * @brief Forget any encoding info we have
26  */
27 void FileTextEncoding::Clear()
28 {
29         m_codepage = -1;
30         m_unicoding = ucr::NONE;
31         m_bom = false;
32         m_guessed = false;
33         m_binary = false;
34 }
35
36 /**
37  * @brief Set codepage
38  */
39 void FileTextEncoding::SetCodepage(int codepage)
40 {
41         m_codepage = codepage;
42         switch (codepage)
43         {
44         case CP_UTF8:
45                 m_unicoding = ucr::UTF8;
46                 break;
47         case 1200:
48                 m_unicoding = ucr::UCS2LE;
49                 break;
50         case 1201:
51                 m_unicoding = ucr::UCS2BE;
52                 break;
53         }
54 }
55
56 void FileTextEncoding::SetUnicoding(ucr::UNICODESET unicoding)
57 {
58         m_unicoding = unicoding;
59         switch (unicoding)
60         {
61         case ucr::NONE:
62                 switch (m_codepage)
63                 {
64                 case CP_UTF8:
65                 case 1200:
66                 case 1201:
67                         m_codepage = CP_ACP; // not sure what to do here
68                         break;
69                 }
70                 break;
71         case ucr::UTF8:
72                 m_codepage = CP_UTF8;
73                 break;
74         case ucr::UCS2LE:
75                 m_codepage = 1200;
76                 break;
77         case ucr::UCS2BE:
78                 m_codepage = 1201;
79                 break;
80         }
81 }
82
83 /**
84  * @brief Return string representation of encoding, eg "UCS-2LE", or "1252"
85  * @todo This resource lookup should be done in GUI code?
86  */
87 String FileTextEncoding::GetName() const
88 {
89         if (m_unicoding == ucr::UTF8)
90         {
91                 if (m_bom)
92                         return LoadResString(IDS_UNICODING_UTF8_BOM);
93                 else
94                         return LoadResString(IDS_UNICODING_UTF8);
95         }
96
97         if (m_unicoding == ucr::UCS2LE)
98                 return LoadResString(IDS_UNICODING_UCS2_LE);
99         if (m_unicoding == ucr::UCS2BE)
100                 return LoadResString(IDS_UNICODING_UCS2_BE);
101
102         String str;
103         if (m_codepage > -1)
104         {
105                 if (m_codepage == CP_UTF8)
106                 {
107                         // We detected codepage to be UTF-8, but unicoding was not set
108                         str = LoadResString(IDS_UNICODING_UTF8);
109                 }
110                 else
111                 {
112                         str.resize(32);
113                         LPTSTR s = &*str.begin(); //GetBuffer(32);
114                         int len = _sntprintf(s, 32, _T("%d"), m_codepage);
115                         str.resize(len);
116                 }
117         }
118         return str;
119 }
120
121 int FileTextEncoding::Collate(const FileTextEncoding & fte1, const FileTextEncoding & fte2)
122 {
123         if (fte1.m_unicoding > fte2.m_unicoding)
124                 return 1;
125         if (fte1.m_unicoding < fte2.m_unicoding)
126                 return 1;
127         if (fte1.m_codepage > fte2.m_codepage)
128                 return 1;
129         if (fte1.m_codepage < fte2.m_codepage)
130                 return 1;
131         return 0;
132 }