1 /*
2  * This file is part of EasyRPG Player
3  *
4  * Copyright (c) 2017 EasyRPG Project. All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 package org.easyrpg.player.game_browser;
26 
27 import java.io.BufferedReader;
28 import java.io.BufferedWriter;
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileOutputStream;
32 import java.io.IOException;
33 import java.io.InputStreamReader;
34 import java.io.OutputStreamWriter;
35 import java.util.ArrayList;
36 
37 /**
38  * Reads the encoding from the RPG_RT.ini file
39  */
40 public class IniEncodingReader {
41 	private ArrayList<String> lines = new ArrayList<String>();
42 	private File iniFile;
43 
44 	/**
45 	 * Reads the encoding from the RPG_RT.ini file
46 	 *
47 	 * @param iniFile Ini file
48 	 * @throws IOException On any IO error
49 	 */
IniEncodingReader(File iniFile)50 	public IniEncodingReader(File iniFile) throws IOException {
51 		this.iniFile = iniFile;
52 		BufferedReader br = null;
53 		try {
54 			br = new BufferedReader(new InputStreamReader(new FileInputStream(iniFile)));
55 			String line;
56 			while ((line = br.readLine()) != null) {
57 				//Log.v("Ini", line);
58 				lines.add(line);
59 			}
60 		} finally {
61 			if (br != null) {
62 				br.close();
63 			}
64 		}
65 	}
66 
67 	/**
68 	 * Gets the encoding of the file.
69 	 *
70 	 * @return encoding
71 	 */
getEncoding()72 	public String getEncoding() {
73 		boolean sectionFound = false;
74 
75 		for (String line : lines) {
76 			if (!sectionFound && line.trim().equalsIgnoreCase("[EasyRPG]")) {
77 				sectionFound = true;
78 				continue;
79 			}
80 			if (sectionFound) {
81 				//Log.v("Ini", line);
82 				if (!line.isEmpty() && line.trim().charAt(0) == '[') {
83 					sectionFound = false;
84 				} else {
85 					//Log.v("Ini", line);
86 					String[] entry = line.toLowerCase().split("=", 2);
87 					if (entry[0].trim().equals("encoding")) {
88 						//Log.v("Ini", "Enc" + entry[1]);
89 						return entry[1];
90 					}
91 				}
92 			}
93 		}
94 		//Log.v("Ini", "Enc1252");
95 		return null;
96 	}
97 }
98