OSDN Git Service

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