OSDN Git Service

f2da980c80584f93b6f0f546028f5bf4c4ed8406
[tpi/lychee.git] / src / common / library / library.cpp
1 /*******************************************************************************\r
2   TPI - flexible but useless plug-in framework.\r
3   Copyright (C) 2002-2009 Silky\r
4 \r
5   This library is free software; you can redistribute it and/or modify it under\r
6   the terms of the GNU Lesser General Public License as published by the Free\r
7   Software Foundation; either version 2.1 of the License, or (at your option)\r
8   any later version.\r
9 \r
10   This library is distributed in the hope that it will be useful, but WITHOUT\r
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or \r
12   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License\r
13   for more details.\r
14 \r
15   You should have received a copy of the GNU Lesser General Public License along\r
16   with this library; if not, write to the Free Software Foundation, Inc.,\r
17   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA\r
18 \r
19   $Id$\r
20 *******************************************************************************/\r
21 \r
22 //******************************************************************************\r
23 //    Includes\r
24 //******************************************************************************\r
25 \r
26 #include "library.h"\r
27 \r
28 //******************************************************************************\r
29 //    Functions\r
30 //******************************************************************************\r
31 \r
32 wxString ProcessBoolLoop(bool b, wxString szCommandLine, size_t * nPos, wxString szVal, bool fIf)\r
33 {\r
34         wxString szCommandLineSend;\r
35         size_t i = * nPos;\r
36         for (i++; i < szCommandLine.Len() && szCommandLine[i] != (fIf ? wxT('|') : wxT('}')); i++)\r
37         {\r
38                 if (fIf ? b : ! b)\r
39                 {\r
40                         szCommandLineSend += szCommandLine[i] == wxT('#') ? szVal : szCommandLine[i];\r
41                 }\r
42         }\r
43         * nPos = i;\r
44         return szCommandLineSend;\r
45 }\r
46 \r
47 wxString ProcessBool(bool b, wxString szCommandLine, size_t * nPos, wxString szVal = wxEmptyString)\r
48 {\r
49         wxString szCommandLineSend;\r
50         size_t i = * nPos;\r
51         if (szCommandLine[++i] == wxT('{'))\r
52         {\r
53                 // 条件判別。\r
54                 szCommandLineSend += ProcessBoolLoop(b, szCommandLine, & i, szVal, true);\r
55                 szCommandLineSend += ProcessBoolLoop(b, szCommandLine, & i, szVal, false);\r
56         }\r
57         else\r
58         {\r
59                 // bool値を返却。\r
60                 szCommandLineSend += b ? wxT('1') : wxT('0');\r
61                 i--;\r
62         }\r
63         * nPos = i;\r
64         return szCommandLineSend;\r
65 }\r
66 \r
67 wxString MakeCommandLineSend(wxString szCommandLine, wxString szArcName, TPI_SWITCHES * swInfo, wxArrayString asFiles, wxString szResponceFileName)\r
68 {\r
69         // コマンドライン変数を差し替え。\r
70         wxString szCommandLineSend;\r
71         for (size_t i = 0; i < szCommandLine.Len(); i++)\r
72         {\r
73                 if (szCommandLine[i] == wxT('%'))\r
74                 {\r
75                         // 変数解析開始。\r
76                         switch ((wxChar) szCommandLine[++i])\r
77                         {\r
78                         case wxT('%'):\r
79                                 // 特殊文字。\r
80                                 szCommandLineSend += szCommandLine[i];\r
81                                 break;\r
82                         case wxT('9'):\r
83                                 switch ((wxChar) szCommandLine[++i])\r
84                                 {\r
85                                 // 0: 書庫名\r
86                                 // 1: 出力先\r
87                                 // 2: レスポンスファイル\r
88                                 // 3: ファイルリスト\r
89                                 // 4: 圧縮レベル\r
90                                 // 5: リカバリーレコード\r
91                                 // 6: コメント\r
92                                 case wxT('0'): szCommandLineSend += szArcName; break;\r
93                                 case wxT('1'): szCommandLineSend += swInfo->fnDestinationDirectory.GetPathWithSep(); break;\r
94                                 case wxT('2'): szCommandLineSend += szResponceFileName; break;\r
95                                 case wxT('3'):\r
96                                         for (size_t j = 0; j < asFiles.GetCount(); j++)\r
97                                         {\r
98                                                 szCommandLineSend += wxT("\"") + asFiles[j] + wxT("\" ");\r
99                                         }\r
100                                         break;\r
101                                 case wxT('4'): szCommandLineSend += wxString::Format(wxT("%d"), swInfo->nCompressLevel); break;\r
102                                 case wxT('5'): szCommandLineSend += wxString::Format(wxT("%d"), swInfo->nRecoveryRecord); break;\r
103                                 case wxT('6'): szCommandLineSend += swInfo->szComment; break;\r
104                                 }\r
105                                 break;\r
106                         case wxT('a'):\r
107                                 switch ((wxChar) szCommandLine[++i])\r
108                                 {\r
109                                 // 0: パスを有効にするか\r
110                                 // 1: SFXを作成するか\r
111                                 // 2: Solid圧縮\r
112                                 // 3: MM最適化\r
113                                 // 4: ヘッダ暗号化\r
114                                 // 5: パスワード\r
115                                 // 6: キーファイル\r
116                                 // 7: 分割サイズ\r
117                                 // 8: ヘッダ圧縮\r
118                                 case wxT('0'): szCommandLineSend += ProcessBool(swInfo->fStoreDirectoryPathes,  szCommandLine, & i); break;\r
119                                 case wxT('1'): szCommandLineSend += ProcessBool(swInfo->fMakeSFX,               szCommandLine, & i); break;\r
120                                 case wxT('2'): szCommandLineSend += ProcessBool(swInfo->fSolid,                 szCommandLine, & i); break;\r
121                                 case wxT('3'): szCommandLineSend += ProcessBool(swInfo->fMMOptimize,            szCommandLine, & i); break;\r
122                                 case wxT('4'): szCommandLineSend += ProcessBool(swInfo->fEncryptHeader,         szCommandLine, & i); break;\r
123                                 case wxT('5'): szCommandLineSend += ProcessBool(! swInfo->szPassword.IsEmpty(), szCommandLine, & i, swInfo->szPassword); break;\r
124                                 case wxT('6'): szCommandLineSend += ProcessBool(! swInfo->szKeyFile.IsEmpty(),  szCommandLine, & i, swInfo->szKeyFile); break;\r
125                                 case wxT('7'): szCommandLineSend += ProcessBool(swInfo->nSplitSize != 0,        szCommandLine, & i, wxString::Format(wxT("%d"), swInfo->nSplitSize)); break;\r
126                                 case wxT('8'): szCommandLineSend += ProcessBool(swInfo->fCompressHeader,        szCommandLine, & i); break;\r
127                                 }\r
128                                 break;\r
129                         }\r
130                 }\r
131                 else\r
132                 {\r
133                         szCommandLineSend += szCommandLine[i];\r
134                 }\r
135         }\r
136 \r
137         return szCommandLineSend;\r
138 }\r
139 \r
140 wxString MakeResponceFile(wxArrayString asFiles, bool bQuote, bool bUnicode)\r
141 {\r
142         wxFile fListFile;\r
143         wxString szFileName = wxFileName::CreateTempFileName(wxT("__listfile"), & fListFile), szBuffer;\r
144         if (szFileName.IsEmpty())\r
145         {\r
146                 return wxEmptyString;\r
147         }\r
148 \r
149         // ファイルリスト書き込み処理。\r
150         if (asFiles.IsEmpty())\r
151         {\r
152                 szBuffer = wxT("*");\r
153         }\r
154         else\r
155         {\r
156                 for (size_t i = 0; i < asFiles.GetCount(); i++)\r
157                 {\r
158                         // 最後に改行があると動作がおかしくなる場合がある模様。\r
159                         if (i != 0)\r
160                         {\r
161 #ifdef __WINDOWS__\r
162                                 szBuffer += wxT("\r\n");\r
163 #else\r
164                                 szBuffer += wxT("\n");\r
165 #endif\r
166                         }\r
167                         szBuffer += bQuote ? (wxT("\"") + asFiles[i] + wxT("\"")) : asFiles[i];\r
168                 }\r
169         }\r
170         fListFile.Write(szBuffer, bUnicode ? (const wxMBConv&) wxMBConvUTF16LE() : (const wxMBConv&) wxMBConvUTF8());\r
171         fListFile.Close();\r
172 \r
173         return szFileName;\r
174 }\r
175 \r
176 wxString MB2String(const char * sz)\r
177 {\r
178         return wxString(sz, wxConvLibc);\r
179 }\r
180 \r
181 wxString UTF82String(const char * sz)\r
182 {\r
183         return wxString(sz, wxConvUTF8);\r
184 }\r
185 \r
186 wxString WC2String(const wchar_t * sz)\r
187 {\r
188         return wxString(sz);\r
189 }\r