| 1 |
// --------------------------------------------------------------------------- |
| 2 |
// CVS用 文字コード変換(EUC <-> SJIS)付きフォルダ同期ツール |
| 3 |
// |
| 4 |
// Copyright (C) 2002 by Project HOS |
| 5 |
// --------------------------------------------------------------------------- |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
#include <windows.h> |
| 10 |
#include <tchar.h> |
| 11 |
#include <stdio.h> |
| 12 |
#include <stdlib.h> |
| 13 |
#include <process.h> |
| 14 |
#include "TreeSearch.h" |
| 15 |
#include "TimeStamp.h" |
| 16 |
#include "ReadConfig.h" |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
// ローカル関数 |
| 21 |
static void ReadCfgFile(LPCTSTR lpszExeFile); // 設定読み込み |
| 22 |
static BOOL FileProc(LPCTSTR lpszFile1, LPCTSTR lpszFile2, int iFlag); // ファイル処理ルーチン |
| 23 |
static BOOL File1To2(LPCTSTR lpszFile1, LPCTSTR lpszFile2); // Tree1(EUC)からTree2(SJIS)へコピー処理 |
| 24 |
static BOOL File2To1(LPCTSTR lpszFile1, LPCTSTR lpszFile2); // Tree2(SJIS)からTree1(EUC)へコピー処理 |
| 25 |
static BOOL CheckTextFile(LPCTSTR lpszFileName); // テキストファイル判定 |
| 26 |
static BOOL CheckBinaryFile(LPCTSTR lpszFileName); // バイナリファイル判定 |
| 27 |
|
| 28 |
|
| 29 |
// ローカル変数 |
| 30 |
static CAssociationVector s_avConfig; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
// メイン関数 |
| 35 |
int main(int argc, char *argv[]) |
| 36 |
{ |
| 37 |
CTreeSearch tc(FileProc); |
| 38 |
TCHAR szPath1[MAX_PATH]; |
| 39 |
TCHAR szPath2[MAX_PATH]; |
| 40 |
|
| 41 |
// 引数チェック |
| 42 |
if ( argc != 3 ) |
| 43 |
{ |
| 44 |
printf("usage: CvsSync.exe euc-path sjis-path\n"); |
| 45 |
return 1; |
| 46 |
} |
| 47 |
|
| 48 |
// 設定ファイル読み込み |
| 49 |
ReadCfgFile(argv[0]); |
| 50 |
|
| 51 |
// パスの後ろに '\' をつける |
| 52 |
lstrcpy(szPath1, argv[1]); |
| 53 |
lstrcpy(szPath2, argv[2]); |
| 54 |
if ( szPath1[lstrlen(szPath1) - 1] != _T('\\') ) |
| 55 |
{ |
| 56 |
lstrcat(szPath1, _T("\\")); |
| 57 |
} |
| 58 |
if ( szPath2[lstrlen(szPath2) - 1] != _T('\\') ) |
| 59 |
{ |
| 60 |
lstrcat(szPath2, _T("\\")); |
| 61 |
} |
| 62 |
|
| 63 |
// ディレクトリ作成の暴挙に出る |
| 64 |
if ( ::CreateDirectory(szPath1, NULL) ) |
| 65 |
{ |
| 66 |
printf("create directry \"%s\"\n", szPath1); |
| 67 |
} |
| 68 |
if ( ::CreateDirectory(szPath2, NULL) ) |
| 69 |
{ |
| 70 |
printf("create directry \"%s\"\n", szPath2); |
| 71 |
} |
| 72 |
|
| 73 |
// ツリー検索実行 |
| 74 |
tc.Search(szPath1, szPath2); |
| 75 |
|
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
// 設定ファイル読み込み |
| 82 |
void ReadCfgFile(LPCTSTR lpszExeFile) |
| 83 |
{ |
| 84 |
TCHAR szCfgFile[_MAX_PATH]; |
| 85 |
TCHAR szDrive[_MAX_DRIVE]; |
| 86 |
TCHAR szDir[_MAX_DIR]; |
| 87 |
TCHAR szName[_MAX_FNAME]; |
| 88 |
|
| 89 |
// 実行ファイルの拡張子を変換 |
| 90 |
_tsplitpath(lpszExeFile, szDrive, szDir, szName, NULL); |
| 91 |
_tmakepath(szCfgFile, szDrive, szDir, szName, _T(".cfg")); |
| 92 |
|
| 93 |
// 設定ファイル読み出し |
| 94 |
ReadConfig(szCfgFile, &s_avConfig); |
| 95 |
|
| 96 |
// デフォルトオプションを設定 |
| 97 |
s_avConfig.Add(_T("[NkfOption1]"), _T("-E -s")); // EUCを想定してSJISに変換 |
| 98 |
s_avConfig.Add(_T("[NkfOption1]"), _T("-s -E")); // SJISを想定してEUCに変換 |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
// ファイル処理ルーチン |
| 104 |
BOOL FileProc(LPCTSTR lpszFile1, LPCTSTR lpszFile2, int iFlag) |
| 105 |
{ |
| 106 |
TTimeStamp ts1, ts2; |
| 107 |
int iComp; |
| 108 |
|
| 109 |
switch ( iFlag ) |
| 110 |
{ |
| 111 |
case FILEPROC_FILE1: // ファイル1(EUC)のみが存在 |
| 112 |
File1To2(lpszFile1, lpszFile2); |
| 113 |
break; |
| 114 |
|
| 115 |
case FILEPROC_FILE2: // ファイル2(SJIS)のみが存在 |
| 116 |
File2To1(lpszFile1, lpszFile2); |
| 117 |
break; |
| 118 |
|
| 119 |
case FILEPROC_BOTH: // 両方のファイルが存在 |
| 120 |
GetFileTimeStamp(lpszFile1, &ts1); |
| 121 |
GetFileTimeStamp(lpszFile2, &ts2); |
| 122 |
iComp = CompareTimeStamp(&ts1, &ts2); |
| 123 |
if ( iComp > 0 ) |
| 124 |
{ |
| 125 |
File1To2(lpszFile1, lpszFile2); // File1が新しければ File1 -> File2 |
| 126 |
} |
| 127 |
else if ( iComp < 0 ) |
| 128 |
{ |
| 129 |
File2To1(lpszFile1, lpszFile2); // File2が新しければ File2 -> File1 |
| 130 |
} |
| 131 |
break; |
| 132 |
} |
| 133 |
|
| 134 |
return TRUE; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
// File1(EUC) を SJIS変換してFile2にコピー |
| 139 |
BOOL File1To2(LPCTSTR lpszFile1, LPCTSTR lpszFile2) |
| 140 |
{ |
| 141 |
CStringVector* psv; |
| 142 |
TCHAR szCommand[MAX_PATH * 2]; |
| 143 |
TTimeStamp ts; |
| 144 |
|
| 145 |
psv = s_avConfig.Get(_T("[NkfOption1]")); |
| 146 |
|
| 147 |
if ( CheckTextFile(lpszFile1) ) |
| 148 |
{ |
| 149 |
// テキストはnkfで変換 |
| 150 |
wsprintf(szCommand, _T("nkf32 %s -O \"%s\" \"%s\""), psv->Get(0), lpszFile1, lpszFile2); |
| 151 |
if ( _tsystem(szCommand) != 0 ) |
| 152 |
{ |
| 153 |
return FALSE; // エラー発生 |
| 154 |
} |
| 155 |
} |
| 156 |
else if ( CheckBinaryFile(lpszFile1) ) |
| 157 |
{ |
| 158 |
// バイナリファイルはそのままコピー |
| 159 |
if ( !::CopyFile(lpszFile1, lpszFile2, FALSE) ) |
| 160 |
{ |
| 161 |
return FALSE; // エラー発生 |
| 162 |
} |
| 163 |
} |
| 164 |
else |
| 165 |
{ |
| 166 |
// 未登録ファイルは無視 |
| 167 |
return TRUE; |
| 168 |
} |
| 169 |
|
| 170 |
printf("Update : %s\n", lpszFile2); |
| 171 |
|
| 172 |
// タイムスタンプのコピー |
| 173 |
GetFileTimeStamp(lpszFile1, &ts); |
| 174 |
SetFileTimeStamp(lpszFile2, &ts); |
| 175 |
|
| 176 |
// ファイル属性のコピー |
| 177 |
::SetFileAttributes(lpszFile2, GetFileAttributes(lpszFile1)); |
| 178 |
|
| 179 |
return TRUE; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
// File2(SJIS) を EUC変換してFile1にコピー |
| 184 |
BOOL File2To1(LPCTSTR lpszFile1, LPCTSTR lpszFile2) |
| 185 |
{ |
| 186 |
CStringVector* psv; |
| 187 |
TCHAR szCommand[MAX_PATH * 2]; |
| 188 |
TTimeStamp ts; |
| 189 |
|
| 190 |
psv = s_avConfig.Get(_T("[NkfOption2]")); |
| 191 |
if ( psv == NULL ) |
| 192 |
{ |
| 193 |
return FALSE; |
| 194 |
} |
| 195 |
|
| 196 |
if ( CheckTextFile(lpszFile2) ) |
| 197 |
{ |
| 198 |
// テキストはnkfで変換 |
| 199 |
wsprintf(szCommand, _T("nkf32 %s -O \"%s\" \"%s\""), psv->Get(0), lpszFile2, lpszFile1); |
| 200 |
if ( _tsystem(szCommand) != 0 ) |
| 201 |
{ |
| 202 |
return FALSE; // エラー発生 |
| 203 |
} |
| 204 |
} |
| 205 |
else if ( CheckBinaryFile(lpszFile2) ) |
| 206 |
{ |
| 207 |
// バイナリファイルはそのままコピー |
| 208 |
if ( !::CopyFile(lpszFile2, lpszFile1, FALSE) ) |
| 209 |
{ |
| 210 |
return FALSE; // エラー発生 |
| 211 |
} |
| 212 |
} |
| 213 |
else |
| 214 |
{ |
| 215 |
// 未登録ファイルは無視 |
| 216 |
return TRUE; |
| 217 |
} |
| 218 |
|
| 219 |
printf("Update : %s\n", lpszFile1); |
| 220 |
|
| 221 |
// タイムスタンプのコピー |
| 222 |
::GetFileTimeStamp(lpszFile2, &ts); |
| 223 |
::SetFileTimeStamp(lpszFile1, &ts); |
| 224 |
|
| 225 |
// ファイル属性のコピー |
| 226 |
::SetFileAttributes(lpszFile1, GetFileAttributes(lpszFile2)); |
| 227 |
|
| 228 |
return TRUE; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
// テキストファイル判定 |
| 233 |
BOOL CheckTextFile(LPCTSTR lpszFileName) |
| 234 |
{ |
| 235 |
CStringVector* psv; |
| 236 |
TCHAR szName[_MAX_FNAME]; |
| 237 |
TCHAR szExt[_MAX_EXT]; |
| 238 |
int i; |
| 239 |
|
| 240 |
// パス名を分解 |
| 241 |
_tsplitpath(lpszFileName, NULL, NULL, szName, szExt); |
| 242 |
|
| 243 |
// 拡張子なしの makefile ならテキストと(勝手に)みなす |
| 244 |
if ( lstrcmpi(szName, _T("makefile")) == 0 |
| 245 |
&& lstrcmpi(szExt, _T("")) == 0 ) |
| 246 |
{ |
| 247 |
return TRUE; |
| 248 |
} |
| 249 |
|
| 250 |
// 設定ファイルの情報を読み出し |
| 251 |
psv = s_avConfig.Get(_T("[TextFile]")); |
| 252 |
if ( psv == NULL ) |
| 253 |
{ |
| 254 |
return FALSE; |
| 255 |
} |
| 256 |
|
| 257 |
// 指定拡張子と一致したらテキストとみなす |
| 258 |
for ( i = 0; i < psv->GetCount(); i++ ) |
| 259 |
{ |
| 260 |
if ( lstrcmpi(szExt, psv->Get(i)) == 0 ) |
| 261 |
{ |
| 262 |
return TRUE; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
return FALSE; |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
// バイナリファイル判定 |
| 271 |
BOOL CheckBinaryFile(LPCTSTR lpszFileName) |
| 272 |
{ |
| 273 |
CStringVector* psv; |
| 274 |
TCHAR szExt[_MAX_EXT]; |
| 275 |
int i; |
| 276 |
|
| 277 |
// パス名を分解 |
| 278 |
_tsplitpath(lpszFileName, NULL, NULL, NULL, szExt); |
| 279 |
|
| 280 |
// 設定ファイルの情報を読み出し |
| 281 |
psv = s_avConfig.Get(_T("[BinaryFile]")); |
| 282 |
if ( psv == NULL ) |
| 283 |
{ |
| 284 |
return FALSE; |
| 285 |
} |
| 286 |
|
| 287 |
// 指定拡張子と一致したらバイナリとみなす |
| 288 |
for ( i = 0; i < psv->GetCount(); i++ ) |
| 289 |
{ |
| 290 |
if ( lstrcmpi(szExt, psv->Get(i)) == 0 ) |
| 291 |
{ |
| 292 |
return TRUE; |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
return FALSE; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
// --------------------------------------------------------------------------- |
| 301 |
// Copyright (C) 2002 by Project HOS |
| 302 |
// --------------------------------------------------------------------------- |