Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Namespace Members | Compound Members | File Members

TextConverter.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------
00002 // Lamp : Open source game middleware
00003 // Copyright (C) 2004  Junpei Ohtani ( Email : junpee@users.sourceforge.jp )
00004 //
00005 // This library is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU Lesser General Public
00007 // License as published by the Free Software Foundation; either
00008 // version 2.1 of the License, or (at your option) any later version.
00009 //
00010 // This library is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public
00016 // License along with this library; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 //------------------------------------------------------------------------------
00019 
00020 /** @file
00021  * テキストコンバータ実装
00022  * @author Junpee
00023  */
00024 
00025 #include "LampBasic.h"
00026 #include "Core/InputOutput/TextConverter.h"
00027 #include "Core/System/StringMethod.h"
00028 
00029 namespace Lamp{
00030 
00031 //------------------------------------------------------------------------------
00032 // Stringからテキストへの変換
00033 String TextConverter::stringToText(const String& string){
00034     int bufferSize = string.getSize() * 2 + 3;
00035     char* buffer = new char[bufferSize];
00036     char* current = buffer;
00037     *current = '\"';
00038     current++;
00039     const char* source = string.getBytes();
00040     while(true){
00041         char character = *source;
00042         if(character == '\0'){ break; }
00043         if(character == '\n'){
00044             *current = '\\';
00045             current++;
00046             *current = 'n';
00047             current++;
00048         }else if(character == '\t'){
00049             *current = '\\';
00050             current++;
00051             *current = 't';
00052             current++;
00053         }else if(character == '\\'){
00054             *current = '\\';
00055             current++;
00056             *current = '\\';
00057             current++;
00058         }else if(character == '\"'){
00059             *current = '\\';
00060             current++;
00061             *current = '\"';
00062             current++;
00063         }else if(character == '\r'){
00064             *current = '\\';
00065             current++;
00066             *current = 'r';
00067             current++;
00068         }else if(character == '\v'){
00069             *current = '\\';
00070             current++;
00071             *current = 'v';
00072             current++;
00073         }else if(character == '\b'){
00074             *current = '\\';
00075             current++;
00076             *current = 'b';
00077             current++;
00078         }else if(character == '\f'){
00079             *current = '\\';
00080             current++;
00081             *current = 'f';
00082             current++;
00083         }else if(character == '\a'){
00084             *current = '\\';
00085             current++;
00086             *current = 'a';
00087             current++;
00088         }else{
00089             // 次の文字へ進む
00090             *current = *source;
00091             current++;
00092             const char* next = StdStrinc(source);
00093             if((next - source) == 2){// 2バイト文字
00094                 source++;
00095                 *current = *source;
00096                 current++;
00097             }
00098         }
00099         source++;
00100     }
00101     *current = '\"';
00102     current++;
00103     *current = '\0';
00104     String returnString(buffer);
00105     delete[] buffer;
00106     return returnString;
00107 }
00108 //------------------------------------------------------------------------------
00109 // テキストからStringへの変換
00110 String TextConverter::textToString(const String& text){
00111     Assert('"' == text.charAt(0));
00112     Assert('"' == text.charAt(text.getSize() - 1));
00113     int bufferSize = text.getSize();// 最低でも2文字減る。NULL文字入れても-1。
00114     char* buffer = new char[bufferSize];
00115     char* current = buffer;
00116     const char* source = text.getBytes();
00117     // 前のダブルクォーテーション分
00118     source++;
00119     while(true){
00120         char character = *source;
00121         if(character == '\0'){ break; }
00122         if(character == '\\'){
00123             // エスケープキャラクタ
00124             source++;
00125             char escapeCharacter = *source;
00126             if(escapeCharacter == 'n'){
00127                 *current = '\n';
00128                 current++;
00129             }else if(escapeCharacter == 't'){
00130                 *current = '\t';
00131                 current++;
00132             }else if(escapeCharacter == '\\'){
00133                 *current = '\\';
00134                 current++;
00135             }else if(escapeCharacter == '\"'){
00136                 *current = '\"';
00137                 current++;
00138             }else if(escapeCharacter == 'r'){
00139                 *current = '\r';
00140                 current++;
00141             }else if(escapeCharacter == 'v'){
00142                 *current = '\v';
00143                 current++;
00144             }else if(escapeCharacter == 'b'){
00145                 *current = '\b';
00146                 current++;
00147             }else if(escapeCharacter == 'f'){
00148                 *current = '\f';
00149                 current++;
00150             }else if(escapeCharacter == 'a'){
00151                 *current = '\a';
00152                 current++;
00153             }else{
00154                 AssertMessage(true,
00155                     "TextConverter::textToString 未対応エスケープシーケンス");
00156             }
00157         }else{
00158             // 非エスケープキャラクタ
00159             *current = *source;
00160             current++;
00161             const char* next = StdStrinc(source);
00162             if((next - source) == 2){// 2バイト文字
00163                 source++;
00164                 *current = *source;
00165                 current++;
00166             }
00167         }
00168         source++;
00169     }
00170     // 後ろのダブルクォーテーション分
00171     current--;
00172     *current = '\0';
00173     String returnString(buffer);
00174     delete[] buffer;
00175     return returnString;
00176 }
00177 //------------------------------------------------------------------------------
00178 } // End of namespace Lamp
00179 //------------------------------------------------------------------------------

Generated on Wed Mar 16 10:29:37 2005 for Lamp by doxygen 1.3.2