1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "common/file.h"
24 
25 #include "sludge/newfatal.h"
26 
27 namespace Sludge {
28 
29 bool allowAnyFilename = true;
30 
writeString(Common::String s,Common::WriteStream * stream)31 void writeString(Common::String s, Common::WriteStream *stream) {
32 	int a, len = s.size();
33 	stream->writeUint16BE(len);
34 	for (a = 0; a < len; a++) {
35 		stream->writeByte(s[a] + 1);
36 	}
37 }
38 
readString(Common::SeekableReadStream * stream)39 Common::String readString(Common::SeekableReadStream *stream) {
40 	int len = stream->readUint16BE();
41 	Common::String res = "";
42 	for (int a = 0; a < len; a++) {
43 		res += (char)(stream->readByte() - 1);
44 	}
45 	return res;
46 }
47 
encodeFilename(const Common::String & nameIn)48 Common::String encodeFilename(const Common::String &nameIn) {
49 	Common::String newName = "";
50 	if (nameIn.empty())
51 		return newName;
52 	if (allowAnyFilename) {
53 		for (uint i = 0; i < nameIn.size(); ++i) {
54 			switch (nameIn[i]) {
55 			case '<':
56 				newName += '_';
57 				newName += 'L';
58 				break;
59 			case '>':
60 				newName += '_';
61 				newName += 'G';
62 				break;
63 			case '|':
64 				newName += '_';
65 				newName += 'P';
66 				break;
67 			case '_':
68 				newName += '_';
69 				newName += 'U';
70 				break;
71 			case '\"':
72 				newName += '_';
73 				newName += 'S';
74 				break;
75 			case '\\':
76 				newName += '_';
77 				newName += 'B';
78 				break;
79 			case '/':
80 				newName += '_';
81 				newName += 'F';
82 				break;
83 			case ':':
84 				newName += '_';
85 				newName += 'C';
86 				break;
87 			case '*':
88 				newName += '_';
89 				newName += 'A';
90 				break;
91 			case '?':
92 				newName += '_';
93 				newName += 'Q';
94 				break;
95 
96 			default:
97 				newName += nameIn[i];
98 				break;
99 			}
100 		}
101 	} else {
102 		newName.clear();
103 		newName = nameIn;
104 		for (uint i = 0; i < newName.size(); ++i) {
105 			if (newName[i] == '\\')
106 				newName.setChar('/', i);
107 		}
108 	}
109 	return newName;
110 }
111 
decodeFilename(const Common::String & nameIn)112 Common::String decodeFilename(const Common::String &nameIn) {
113 	Common::String newName ="";
114 	if (allowAnyFilename) {
115 		for (uint i = 0; i < nameIn.size(); ++i) {
116 			if (nameIn[i] == '_') {
117 				++i;
118 				switch (nameIn[i]) {
119 				case 'L':
120 					newName += '<';
121 					break;
122 				case 'G':
123 					newName += '>';
124 					break;
125 				case 'P':
126 					newName += '|';
127 					break;
128 				case 'U':
129 					newName += '_';
130 					break;
131 				case 'S':
132 					newName += '\"';
133 					break;
134 				case 'B':
135 					newName += '\\';
136 					break;
137 				case 'F':
138 					newName += '/';
139 					break;
140 				case 'C':
141 					newName += ':';
142 					break;
143 				case 'A':
144 					newName += '*';
145 					break;
146 				case 'Q':
147 					newName += '?';
148 					break;
149 				default:
150 					newName += '_';
151 					--i;
152 					break;
153 				}
154 			} else {
155 				newName += nameIn[i];
156 			}
157 		}
158 		return newName;
159 	} else {
160 		newName.clear();
161 		newName = nameIn;
162 	}
163 	return newName;
164 }
165 
createCString(const Common::String & s)166 char *createCString(const Common::String &s) {
167 	uint n = s.size() + 1;
168 	char *res = new char[n];
169 	if (!checkNew(res)) {
170 		fatal("createCString : Unable to copy String");
171 		return NULL;
172 	}
173 	memcpy(res, s.c_str(), n);
174 	return res;
175 }
176 
177 } // End of namespace Sludge
178