文字列を_T()で囲むスクリプト

公開:2005-02-24 21:34
更新:2020-02-15 04:36
カテゴリ:c++,アプリ・ライブラリ・言語

Win32 GUI GenericsをUnicodeベースでコンパイルするために、文字列を_T()で囲むスクリプトを作っています。
まだ完全にはできていないのですが...。
このスクリプトで下記のソースを変換すると、

testsrc.cpp


000001  //
000002  // "Remark中の文字列"
000003  / "Remark中の文字列" / "リマーク外"
000004  #include "文字列"
000005  #import "文字列"
000006   "通常文字列1"
000007   L"Unicode文字列"
000008   "通常文字列2"
000009   _T("すでに囲まれている文字列");
000010   "通常文字ダブルコー\"テーシ\"ョンがエスケープで入っている1\"";
000011   L"Unico\"de文\"字列"
000012  /
000013      "コメント中"
000014      "コメント中"
000015      "コメント中"
000016      "コメント中"
000017      "コメント中"
000018 
/
 "リマーク外2"
000019 
000020   ("問題なく変換される文字列"
000021  "連結文字列1" "連結文字列2" _T("こんな文字列")
000022  "一行コメント" // "一行コメント"
000023 
000024 
こんな風になります。

temp.cpp


000001  //
000002  // "Remark中の文字列"
000003  / _T("Remark中の文字列") / "リマーク外"
000004  #include "文字列"
000005  #import "文字列"
000006   _T("通常文字列1")
000007   L"Unicode文字列"
000008   _T("通常文字列2")
000009   _T("すでに囲まれている文字列");
000010   _T("通常文字ダブルコー\"テーシ\"ョンがエスケープで入っている1\"");
000011   L"Unico\"de文\"字列"
000012  /
000013      "コメント中"
000014      "コメント中"
000015      "コメント中"
000016      "コメント中"
000017      "コメント中"
000018 
/
 "リマーク外2"
000019 
000020   (_T("問題なく変換される文字列")) 
000021  _T("連結文字列1""連結文字列2" _T("こんな文字列")
000022  _T("一行コメント"// "一行コメント"
000023 
000024 

うーん、まだまだだめですね...。
3,18,21行目の変換がいかんですな....

replace_text.wsf


000001  <job id="main">
000002      <script language="JScript">
000003          @cc_on
000004          @set @debug = true
000005 
000006          @if(@debug)
000007              var lib_path = "C:\testsrc";
000008          @else
000009              var lib_path = "C:\win32gui-1_6_3\win32gui-lib\win32gui";
000010          @end
000011 
000012          var backup_path = "c:\win32gui-backup";
000013          var source_ext = new Array("CPP","HPP","h");
000014      
000015   
000016          var match_comment = new RegExp("\/\/");
000017          var match_comment_start = /\/*/;
000018          var match_comment_end = new RegExp("\\/");
000019          var match_include = /(#include)|(#import)/;
000020          var match_qstring = /(\".
?[^\]\"+?)+?/;
000021          
000022          //var except_files = new Array("");
000023          var fs = new ActiveXObject("Scripting.FileSystemObject");
000024 
000025          // バックアップを取る
000026          /
000027          try {
000028              fs.CopyFolder(lib_path + "\
",backup_path,false);
000029          } catch(e) {
000030              WScript.Echo(e.description);
000031              WScript.Quit();
000032          }
000033          */

000034          var folder = fs.GetFolder(lib_path);
000035          
000036          function replace_text(folder)
000037          {
000038              var files = new Enumerator(folder.Files);
000039              for(; !files.atEnd(); files.moveNext())
000040              {
000041                  var file = files.item();
000042                  var file_name = new String(file.Name);
000043                  WScript.Echo(file_name);
000044                  var file_ext = new String(file_name.substr(file_name.length - 3,3));
000045                  WScript.Echo(file_ext);
000046                  for(var ext in source_ext)
000047                  {
000048                      if(file_ext.toUpperCase() == source_ext[ext])
000049                      {
000050                          WScript.Echo("ファイル検出:" + file.Path);
000051                      
000052                          var _text = file.OpenAsTextStream(1);
000053                          var _outtext = fs.CreateTextFile(lib_path + "\temp.txt",true);
000054                          var _replaced_source = "";
000055                          var bcomment = false;
000056                          @if(@debug) 
000057                              WScript.Echo("ファイル終了" + _text.AtEndOfStream);
000058                              var _line_number = 0;
000059                          @end                    
000060                          while(!_text.AtEndOfStream)
000061                          {
000062                              @if(@debug) 
000063                                  _line_number++;
000064                              @end                    
000065                              var _line = new String(_text.ReadLine());
000066                              if(bcomment)
000067                              {
000068                                  @if(@debug) WScript.Echo("コメント");   @end
000069                                  // コメント中の場合の処理
000070                                  if(_line.match(match_comment_end))
000071                                  {
000072                                      @if(@debug) WScript.Echo("コメント終了:"  + _line_number + ":"+  _line);  @end
000073                                      bcomment = false;
000074                                  }
000075                              } else {
000076                                  @if(@debug) WScript.Echo("コメントではない:" + _line_number + ":"+ _line);  @end
000077                                  if(_line.match(match_comment_start))
000078                                  {
000079                                                                      // コメント中の場合の処理
000080                                      if(_line.match(match_comment_end))
000081                                      {
000082                                          @if(@debug) WScript.Echo("コメント終了:"  + _line_number + ":"+  _line);  @end
000083                                          bcomment = false;
000084                                      } else {
000085                                          @if(@debug) WScript.Echo("コメント開始:" + _line_number + ":"+ _line);    @end
000086                                          bcomment = true;
000087                                      }
000088                                  }
000089                                  
000090                                  if(!_line.match(match_include))
000091                                  {
000092                                      var _mindex = _line.search(match_qstring);
000093                                      if(_mindex != -1)
000094                                      {
000095                                          @if(@debug) WScript.Echo("文字列検出:" + _mindex + ":" + _line_number + ":"+ _line); @end
000096                                          var _comment_index = _line.search(match_comment);
000097                                          @if(@debug)
000098                                              if(_comment_index != -1)
000099                                               WScript.Echo("1行コメント" + _line_number +  ": index= " + _comment_index + ":" + _line);
000100                                               WScript.Echo("コメントインデックス" + _comment_index)
000101                                               
000102                                          @end
000103                  
000104                                          if(_comment_index == -1 || _mindex < _comment_index)
000105                                          {
000106                                              @if(@debug) 
000107                                                  WScript.Echo("変換対象文字列検出:" + _line_number + ":"+ _line);
000108                                                  match_qstring.exec(_line);  
000109                                                  WScript.Echo("一致文字列" + RegExp.$1);
000110                                              @end
000111                                              if(_comment_index == -1)
000112                                              {
000113                                                  _line = replaceComment(_line,_mindex); 
000114                                              } else {
000115                                                  var _replace_string = _line.substr(0,_comment_index);
000116                                                  _line = replaceComment(_replace_string,_mindex) + _line.substr(_comment_index);
000117                                              }
000118                                              
000119                                          } else {
000120                                          @if(@debug)
000121                                              if(_comment_index != -1)
000122                                               WScript.Echo("コメント内なので変換しない" + _line_number + ":"+ _line);
000123                                          @end
000124                                      
000125                                          }
000126                                          
000127                                      }
000128                                  } else {
000129                                      @if(@debug) WScript.Echo("include検出:" + _line_number + ":"+ _line); @end
000130                                  }
000131                              }
000132                              @if(@debug)
000133                                  WScript.Echo(_line);
000134                              @end
000135                              _outtext.WriteLine(_line);
000136 
000137                          }// while(!_text.AtEndOfStream)
000138                          @if(@debug) 
000139                              WScript.Echo("ファイルを閉じる"+ _line_number + ":");
000140                          @end
000141                          _text.Close();
000142                          _outtext.Close();
000143                          //fs.CopyFile(lib_path + "\temp.txt",file.Path,true);
000144                          break;
000145                      }
000146                      
000147                      
000148                  }
000149                  
000150              }
000151              
000152              var folders = new Enumerator(folder.SubFolders);
000153              for(; !folders.atEnd(); folders.moveNext())
000154              {
000155                  replace_text(folders.item());
000156              }
000157          }
000158          
000159          function replaceComment(line,index)
000160          {
000161              if(index > 0)
000162              {
000163                  // Lが付いているかどうか
000164                  if(line.charAt(index - 1) == "L")
000165                  {
000166                      return line;
000167                  }
000168                  
000169                  // '_T(' が付いているかどうか
000170                  var _check_str = new String("") ;
000171                  while(true)
000172                  {
000173                      index--;
000174                      if(!line.charAt(index).match(/\s/))
000175                      {
000176                          _check_str = line.charAt(index) + _check_str;
000177                      }
000178                      if(_check_str.length == 3 || index == 0)
000179                      {
000180                          break;
000181                      }
000182                  }
000183                                  
000184                  if(_check_str == "_T(")
000185                  {
000186                      return line;
000187                  }
000188                  
000189                  return line.replace(match_qstring,"_T($1)");            
000190              
000191              } else {
000192                  return line.replace(match_qstring,"_T($1)");
000193              }
000194          
000195          }
000196          
000197          replace_text(folder);
000198          
000199          
000200      
000201      </script>
000202  </job>