2 * Programming Language SOOPY
3 * (Simple Object Oriented Programming sYstem)
5 * Copyright (C) 2002,2003 SUZUKI Jun
7 * URL: http://sourceforge.jp/projects/soopy/
8 * License: GPL(GNU General Public License)
11 * $Id: SjisRW.cpp,v 1.28 2004/05/22 05:22:34 randy Exp $
24 * Class SjisReadEncoder
28 SpChar SjisReadEncoder::readCh()
30 SpChar c, ch, ch2, ku, ten;
35 if(eof()){ return (SpChar)NULL; }
36 c = reader->ReadChar();
37 //printf("w-code: %x\n", c);
38 if(isEOF(c)){ return (SpChar)NULL; }
40 if(!EqualCharCode(c, CodeJIS)){
44 ch = SpCharGetChar(c);
54 //printf("ku1: %x\n", (unsigned char)(0x81 + ((ku-1)>>1)));
55 ch2 = ((unsigned char)(0x81 + ((ku-1)>>1)));
57 //printf("ku2: %x\n", (unsigned char)(0xE0 + ((ku-63)>>1)));
58 ch2 = ((unsigned char)(0xE0 + ((ku-63)>>1)));
63 //printf("ten1: %x\n", (unsigned char)(ten + 0x3F));
64 buf[0] = ((unsigned char)(ten + 0x3F));
66 //printf("ten2: %x\n", (unsigned char)(ten + 0x40));
67 buf[0] = ((unsigned char)(ten + 0x40));
70 //printf("ten3: %x\n", (unsigned char)(0x9E + ten));
71 buf[0] = ((unsigned char)(0x9E + ten));
79 * Class SjisReadDecoder
83 SpChar SjisReadDecoder::readCh()
87 int ku, ten, base1, base2;
88 if(eof()){ return (SpChar)NULL; }
89 c = reader->ReadChar();
90 if(isEOF(c)){ return (SpChar)NULL; }
94 if(0xA1 <= ch1 && ch1 <= 0xDF){ // when hankaku KANA
95 // return Zenkaku KANA
98 if((0x81 <= ch1 && ch1 <= 0x9F)
99 || (0xE0 <= ch1 && ch1 <= 0xEF)) // JIS X 0208
101 if(eof()){ return (SpChar)NULL; }
102 c = reader->ReadChar();
104 base1 = ch1 < 0xA0 ? 0x80 : 0xDF;
105 base2 = ch1 < 0xA0 ? 0 : 62;
108 ku = ((ch1 - base1)<<1) - 1 + base2;
109 ten = ch2 - (ch2 < 0x7F ? 0x3F : 0x40);
111 ku = ((ch1 - base1)<<1) + base2;
116 //printf("ch: %x\n", MakeSpChar(CodeJIS, (ku<<8) | ten));
117 return MakeSpChar(CodeJIS, (ku<<8) | ten);
119 return MakeSpChar(CodeJIS, '?');
122 //printf("ch2: %x\n", MakeSpChar(CodeJIS, c));
123 return MakeSpChar(CodeJIS, c);
127 * Class SjisWriteEncoder
131 void SjisWriteEncoder::WriteChar(SpChar c)
135 //printf("w-code--: %x\n", c);
136 if(!EqualCharCode(c, CodeJIS)){
137 writeTextTronCode(c);
140 ch = SpCharGetChar(c);
144 writer->WriteChar(ten);
150 //printf("ku_1: %x\n", (unsigned char)(0x81 + ((ku-1)>>1)));
151 writer->WriteChar((unsigned char)(0x81 + ((ku-1)>>1)));
153 //printf("ku_2: %x\n", (unsigned char)(0xE0 + ((ku-63)>>1)));
154 writer->WriteChar((unsigned char)(0xE0 + ((ku-63)>>1)));
159 //printf("ten_1: %x\n", (unsigned char)(ten + 0x3F));
160 writer->WriteChar((unsigned char)(ten + 0x3F));
162 //printf("ten_2: %x\n", (unsigned char)(ten + 0x40));
163 writer->WriteChar((unsigned char)(ten + 0x40));
166 //printf("ten_3: %x\n", (unsigned char)(0x9E + ten));
167 writer->WriteChar((unsigned char)(0x9E + ten));
174 * Class SjisWriteDecoder
178 void SjisWriteDecoder::WriteChar(SpChar c)
181 int ku, ten, base1, base2;
184 if(c > 0xFF){ // not sjis
186 writer->WriteChar(c);
188 writeTextTronCode(c);
192 if(0xA1 <= ch1 && ch1 <= 0xDF){ // when hankaku KANA
193 // write Zenkaku KANA
199 writer->WriteChar(c);
205 if((0x81 <= ch1 && ch1 <= 0x9F)
206 || (0xE0 <= ch1 && ch1 <= 0xEF)) // JIS X 0208
209 base1 = ch1 < 0xA0 ? 0x80 : 0xDF;
210 base2 = ch1 < 0xA0 ? 0 : 62;
212 ku = ((ch1 - base1)<<1) - 1 + base2;
213 ten = ch2 - (ch2 < 0x7F ? 0x3F : 0x40);
215 ku = ((ch1 - base1)<<1) + base2;
220 writer->WriteChar(MakeSpChar(CodeJIS, (ku<<8) | ten));
222 writer->WriteChar(MakeSpChar(CodeJIS, '?'));
232 SpValue& sjis_openIn(SpValue& v)
234 SpValue name = v.eval();
235 if(!name.isString()){
236 throw SpException("filename is not string (Sjis.openIn)");
238 SpString* str = name.asString();
239 char* filename = (char*)str->toCStringWithEncoder();
240 ifstream* fin = new ifstream;
242 fin->open(filename, ios::binary);
248 throw SpException("can't open file");
250 FileStreamReader* sr = new FileStreamReader(fin, filename);
251 ReadEncoder* encoder = new SjisReadDecoder(sr);
252 // static SpValue result;
253 // result.setNewObject(encoder);
255 return SpObjectResult(encoder);
258 SpValue& sjis_openOut(SpValue& v)
260 SpValue name = v.eval();
261 if(!name.isString()){
262 throw SpException("filename is not string (Sjis.openOut)");
264 SpString* str = name.asString();
265 char* filename = (char*)str->toCStringWithEncoder();
266 ofstream* fout = new ofstream;
268 fout->open(filename, ios::binary);
270 fout->open(filename);
274 throw SpException("can't open file");
276 FileStreamWriter* sw = new FileStreamWriter(fout, filename);
277 WriteEncoder* encoder = new SjisWriteEncoder(sw);
278 // static SpValue result;
279 // result.setNewObject(encoder);
281 return SpObjectResult(encoder);
284 SpValue& sjis_openAppend(SpValue& v)
286 SpValue name = v.eval();
287 if(!name.isString()){
288 throw SpException("filename is not string (Sjis.openAppend)");
290 SpString* str = name.asString();
291 char* filename = (char*)str->toCStringWithEncoder();
292 ofstream* fout = new ofstream;
294 fout->open(filename, ios::app | ios::binary);
296 fout->open(filename, ios::app);
300 throw SpException("can't open file");
302 FileStreamWriter* sw = new FileStreamWriter(fout, filename);
303 WriteEncoder* encoder = new SjisWriteEncoder(sw);
304 // static SpValue result;
305 // result.setNewObject(encoder);
307 return SpObjectResult(encoder);
310 SpValue& sjis_encoderIn(SpValue& v)
312 SpValue r = v.eval();
314 throw SpException("not reader (Sjis.encoderIn)");
316 Reader* reader = r.asReader();
317 ReadEncoder* encoder = new SjisReadEncoder(reader);
318 // static SpValue result;
319 // result.setNewObject(encoder);
321 return SpObjectResult(encoder);
324 SpValue& sjis_decoderIn(SpValue& v)
326 SpValue r = v.eval();
328 throw SpException("not reader (Sjis.decoderIn)");
330 Reader* reader = r.asReader();
331 ReadEncoder* encoder = new SjisReadDecoder(reader);
332 // static SpValue result;
333 // result.setNewObject(encoder);
335 return SpObjectResult(encoder);
338 SpValue& sjis_encoderOut(SpValue& v)
340 SpValue w = v.eval();
342 throw SpException("not writer (Sjis.encoderOut)");
344 Writer* writer = w.asWriter();
345 WriteEncoder* encoder = new SjisWriteEncoder(writer);
346 // static SpValue result;
347 // result.setNewObject(encoder);
349 return SpObjectResult(encoder);
352 SpValue& sjis_decoderOut(SpValue& v)
354 SpValue w = v.eval();
356 throw SpException("not writer (Sjis.decoderOut)");
358 Writer* writer = w.asWriter();
359 WriteEncoder* encoder = new SjisWriteDecoder(writer);
360 // static SpValue result;
361 // result.setNewObject(encoder);
363 return SpObjectResult(encoder);
369 SpValue SymSjis(new SpSymbol("sjis"));
370 SpNameSpace* ns = new SpNameSpace;
372 PMainNameSpace->internConst(SymSjis, NSSjis);
374 SpValue PrimOpenIn(new SpPrim1(&sjis_openIn));
375 ns->internConst(SymOpenIn, PrimOpenIn);
376 SpValue PrimEncoderIn(new SpPrim1(&sjis_encoderIn));
377 ns->internConst(SymEncoderIn, PrimEncoderIn);
378 SpValue PrimDecoderIn(new SpPrim1(&sjis_decoderIn));
379 ns->internConst(SymDecoderIn, PrimDecoderIn);
381 SpValue PrimOpenOut(new SpPrim1(&sjis_openOut));
382 ns->internConst(SymOpenOut, PrimOpenOut);
383 SpValue PrimOpenAppend(new SpPrim1(&sjis_openAppend));
384 ns->internConst(SymOpenAppend, PrimOpenAppend);
385 SpValue PrimEncoderOut(new SpPrim1(&sjis_encoderOut));
386 ns->internConst(SymEncoderOut, PrimEncoderOut);
387 SpValue PrimDecoderOut(new SpPrim1(&sjis_decoderOut));
388 ns->internConst(SymDecoderOut, PrimDecoderOut);