OSDN Git Service

Fix the issue where the Apache Tika plugin becomes enabled again when reopening the...
[winmerge-jp/winmerge-jp.git] / Src / FileTextEncoding.cpp
1 /**
2  * @file  FileTextEncoding.cpp
3  *
4  * @brief Implementation of FileTextEncoding structure
5  */
6
7 #include "pch.h"
8 #include "FileTextEncoding.h"
9 #include "unicoder.h"
10
11 FileTextEncoding::FileTextEncoding()
12 {
13         Clear();
14 }
15
16 /**
17  * @brief Forget any encoding info we have
18  */
19 void FileTextEncoding::Clear()
20 {
21         m_codepage = -1;
22         m_unicoding = ucr::NONE;
23         m_bom = false;
24 }
25
26 /**
27  * @brief Set codepage
28  */
29 void FileTextEncoding::SetCodepage(int codepage)
30 {
31         m_codepage = codepage;
32         switch (codepage)
33         {
34         case ucr::CP_UTF_8:
35                 m_unicoding = ucr::UTF8;
36                 break;
37         case ucr::CP_UCS2LE:
38                 m_unicoding = ucr::UCS2LE;
39                 break;
40         case ucr::CP_UCS2BE:
41                 m_unicoding = ucr::UCS2BE;
42                 break;
43         default:
44                 m_unicoding = ucr::NONE;
45                 break;
46         }
47 }
48
49 void FileTextEncoding::SetUnicoding(ucr::UNICODESET unicoding)
50 {
51         m_unicoding = unicoding;
52         switch (unicoding)
53         {
54         case ucr::NONE:
55                 switch (m_codepage)
56                 {
57                 case ucr::CP_UTF_8:
58                 case ucr::CP_UCS2LE:
59                 case ucr::CP_UCS2BE:
60                         m_codepage = 0; // not sure what to do here
61                         break;
62                 }
63                 break;
64         case ucr::UTF8:
65                 m_codepage = ucr::CP_UTF_8;
66                 break;
67         case ucr::UCS2LE:
68                 m_codepage = ucr::CP_UCS2LE;
69                 break;
70         case ucr::UCS2BE:
71                 m_codepage = ucr::CP_UCS2BE;
72                 break;
73         }
74 }
75
76 /**
77  * @brief Return string representation of encoding, eg "UCS-2LE", or "1252"
78  * @todo This resource lookup should be done in GUI code?
79  */
80 String FileTextEncoding::GetName() const
81 {
82         if (m_unicoding == ucr::UTF8)
83         {
84                 if (m_bom)
85                         return _T("UTF-8 (B)");
86                 else
87                         return _T("UTF-8");
88         }
89
90         if (m_unicoding == ucr::UCS2LE)
91                 return _T("UCS-2 LE");
92         if (m_unicoding == ucr::UCS2BE)
93                 return _T("UCS-2 BE");
94
95         if (m_codepage <= 0)
96                 return _T("");
97
98         if (m_codepage == ucr::CP_UTF_8)
99         {
100                 // We detected codepage to be UTF-8, but unicoding was not set
101                 return _T("UTF-8");
102         }
103
104         return strutils::to_str(m_codepage);
105 }
106
107 int FileTextEncoding::Collate(const FileTextEncoding & fte1, const FileTextEncoding & fte2)
108 {
109         if (fte1.m_unicoding > fte2.m_unicoding)
110                 return 1;
111         if (fte1.m_unicoding < fte2.m_unicoding)
112                 return -1;
113         if (fte1.m_codepage > fte2.m_codepage)
114                 return 1;
115         if (fte1.m_codepage < fte2.m_codepage)
116                 return -1;
117         if (fte1.m_bom > fte2.m_bom)
118                 return 1;
119         if (fte1.m_bom < fte2.m_bom)
120                 return -1;
121         return 0;
122 }