ホテル管理システム
ogi
yesterday 1a1c8e71fcd14858f595029f089b2d4a00202b32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
ÿþusing System;
using System.IO;
using System.Text;
 
namespace HotelPms.SourceFactory.Util
{
    /// <summary>
    /// (uŽNÖS—_N*N‡e,g‡eöN„vx¹e_(Encoding)0
    /// Çû‹ÖS xW[‚‚^hƋÿEncoding Bit Order Mark ÿ€{™Q:NBOM ÿ 0
    /// </summary>
    ///
    public class BOMStream
    {
        public BOMStream()
        {
            //
            // TODO: (WdkYûm R„g ýQpe;‘
            //
        }
 
        public static void ConvertToUTF8(string fromFile, string toFile)
        {
            // €‹ÖS”jHh
            StreamReader sr = new StreamReader(fromFile, Encoding.Default);
            // €‹Nte P”jHhŒ_ sr.CurrentEncodinggêÕR$R·eb”jHh„vè}¼x
            sr.ReadToEnd();
 
            byte[] text = File.ReadAllBytes(fromFile);
            // MessageBox.Show(Encoding.Default.ToString());
            // byte[] convert = Encoding.Convert(Encoding.Default, Encoding.UTF8, text);//2LˆI¼x,ÃSxe1,†Onè}¼x,ÃSxeŒN,îvjè}¼x,ÃSxe N,2kè}¼xŠ‹xe
            byte[] convert = Encoding.Convert(sr.CurrentEncoding, Encoding.UTF8, text);//2LˆI¼x,ÃSxe1,†Onè}¼x,ÃSxeŒN,îvjè}¼x,ÃSxe   N,2kè}¼xŠ‹xe
            File.WriteAllText(toFile, Encoding.UTF8.GetString(convert));//o˜:yIºpUTF8Œ_,ÍNý€ckºx„vo˜:yW[2N
            // File.WriteAllBytes(toFile, convert);
            sr.Close();
        }
 
        /// <summary>
        /// ÖS—_N*N‡e,g‡eöN„vx¹e_0‚YœgàeÕl(W‡eöN4Yè~b0R gHe„vMRü[&{ ÿEncoding.Default\«ˆÔÞV0
        /// </summary>
        /// <param name="fileName">‡eöN T</param>
        ///
        public static Encoding GetEncoding(string fileName)
        {
            //return GetEncoding(fileName, Encoding.Default);
            //StreamReader sr = new StreamReader(fileName, Encoding.Default);
            StreamReader sr = new StreamReader(fileName, Encoding.Default);
            sr.ReadToEnd();
            Encoding encoding = sr.CurrentEncoding;
            sr.Close();
            if (encoding.CodePage == 932)
            {
                string filei3 = System.IO.File.ReadAllText(fileName, Encoding.Default);
                string filei4 = System.IO.File.ReadAllText(fileName, Encoding.GetEncoding(50221));
                if (filei4.Length < filei3.Length) { encoding = Encoding.GetEncoding(50221); }
            }
 
            return encoding;
        }
 
        /// <summary>
        /// ÖS—_N*N‡e,g‡eöNAm„vx¹e_0
        /// </summary>
        /// <param name="stream">‡e,g‡eöNAm</param>
        /// <returns></returns>
        ///
        public static Encoding GetEncoding(FileStream stream)
        {
            return GetEncoding(stream, Encoding.Default);
        }
 
        /// <summary>
        /// ­Š0ÖSŠ0\(u’0YY0
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static bool UnReadOnly(string filePath)
        {
            try
            {
                FileInfo fi = new FileInfo(filePath);
                if ((fi.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
                {
                    fi.Attributes = FileAttributes.Normal;
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
 
        /// <summary>
        /// ÖS—_N*N‡e,g‡eöN„vx¹e_0
        /// </summary>
        /// <param name="fileName">‡eöN T0</param>
        /// <param name="defaultEncoding">؞¤‹x¹e_0S_勹eÕlàeÕlÎN‡eöN„v4YèÖS—_ gHe„vMRü[&{öe ÿ\ԏÞVå‹x¹e_0</param>
        /// <returns></returns>
        ///
        public static Encoding GetEncoding(string fileName, Encoding defaultEncoding)
        {
            FileStream fs = new FileStream(fileName, FileMode.Open);
            Encoding targetEncoding = GetEncoding(fs, defaultEncoding);
            fs.Close();
            return targetEncoding;
        }
 
        /// <summary>
        /// ÖS—_N*N‡e,g‡eöNAm„vx¹e_0
 
        /// </summary>
        /// <param name="stream">‡e,g‡eöNAm0</param>
 
        /// <param name="defaultEncoding">؞¤‹x¹e_0S_勹eÕlàeÕlÎN‡eöN„v4YèÖS—_ gHe„vMRü[&{öe ÿ\ԏÞVå‹x¹e_0</param>
        /// <returns></returns>
        ///
        public static Encoding GetEncoding(FileStream stream, Encoding defaultEncoding)
        {
 
            Encoding targetEncoding = defaultEncoding;
 
            if (stream != null && stream.Length >= 2)
            {
                //ÝOX[‡eöNAm„vMR4*NW[‚‚
                byte byte1 = 0;
                byte byte2 = 0;
                byte byte3 = 0;
                byte byte4 = 0;
 
                //ÝOX[S_MRSeekMOn
                long origPos = stream.Seek(0, SeekOrigin.Begin);
                stream.Seek(0, SeekOrigin.Begin);
                int nByte = stream.ReadByte();
                byte1 = Convert.ToByte(nByte);
                byte2 = Convert.ToByte(stream.ReadByte());
 
                if (stream.Length >= 3)
                {
                    byte3 = Convert.ToByte(stream.ReadByte());
                }
 
                if (stream.Length >= 4)
                {
                    byte4 = Convert.ToByte(stream.ReadByte());
                }
                //9hnc‡eöNAm„vMR4*NW[‚‚$R­eEncoding
                //Unicode {0xFF, 0xFE};
                //BE-Unicode {0xFE, 0xFF};
                //UTF8 = {0xEF, 0xBB, 0xBF};
 
                if (byte1 == 0xFE && byte2 == 0xFF)//UnicodeBe
                {
                    targetEncoding = Encoding.BigEndianUnicode;
                }
 
                if (byte1 == 0xFF && byte2 == 0xFE && byte3 != 0xFF)//Unicode
                {
                    targetEncoding = Encoding.Unicode;
                }
 
                if (byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF)//UTF8
                {
                    targetEncoding = Encoding.UTF8;
                }
 
                //b` YSeekMOn
                stream.Seek(origPos, SeekOrigin.Begin);
            }
            return targetEncoding;
        }
 
 
        public static void ReplaceFileText(string file, string leftSearchWord, string rightSearchWord, string replaceWord)
        {
            BOMStream.UnReadOnly(file);
            Encoding fileEncoding = BOMStream.GetEncoding(file);
 
            string fileinfo = File.ReadAllText(file, fileEncoding);
            string tagKey = leftSearchWord;
            int beginIdx = fileinfo.IndexOf(tagKey);
            int endIdx = 0;
 
            bool isExists = false;
            if (beginIdx > -1)
            {
                beginIdx += tagKey.Length;
                endIdx = fileinfo.IndexOf(rightSearchWord, beginIdx);
                fileinfo = fileinfo.Remove(beginIdx, endIdx - beginIdx);
                fileinfo = fileinfo.Insert(beginIdx, replaceWord);
                isExists = true;
            }
 
            if (!isExists) { return; }
 
            FileInfo fi = new FileInfo(file);
            if ((fi.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            {
                fi.Attributes = FileAttributes.Normal;
            }
 
            File.Copy(file, file + ".bak");
            File.WriteAllText(file, fileinfo, fileEncoding);
        }
 
        public static string UpdateIndex(string file, int beginTabIndex, int adding)
        {
            BOMStream.UnReadOnly(file);
            Encoding fileEncoding = BOMStream.GetEncoding(file);
 
            StringBuilder result = new StringBuilder();
            string[] allInfo = File.ReadAllLines(file, fileEncoding);
 
            for (int i = 0; i < allInfo.Length; i++)
            {
                string tagKey = ".TabIndex = ";
                int beginIdx = allInfo[i].IndexOf(tagKey);
                int endIdx = 0;
 
                if (beginIdx > -1)
                {
                    beginIdx += tagKey.Length;
                    endIdx = allInfo[i].IndexOf(";", beginIdx);
 
                    int tabIdx = int.Parse(allInfo[i].Substring(beginIdx, endIdx - beginIdx));
                    if (tabIdx >= beginTabIndex)
                    {
                        allInfo[i] = allInfo[i].Remove(beginIdx, endIdx - beginIdx);
                        allInfo[i] = allInfo[i].Insert(beginIdx, ((int)(tabIdx + adding)).ToString());
                    }
                }
 
                result.AppendLine(allInfo[i]);
            }
 
            return result.ToString();
 
            //FileInfo fi = new FileInfo(file);
            //if ((fi.Attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
            //{
            //    fi.Attributes = FileAttributes.Normal;
            //}
 
            //File.Copy(file, file + ".bak");
            //File.WriteAllLines(file, allInfo, fileEncoding);
        }
    }
}