1 
  2 
  3 /**
  4  * @name	CeL file function
  5  * @fileoverview
  6  * 本檔案包含了 file functions。
  7  * @since
  8  * @see
  9  * <a href="http://dev.w3.org/2006/webapi/FileAPI/" accessdate="2010/6/20 14:49">File API</a>	
 10  */
 11 
 12 
 13 if (typeof CeL === 'function')
 14 CeL.setup_module('application.storage.file',
 15 {
 16 require : '',
 17 code : function(library_namespace, load_arguments) {
 18 
 19 //	requiring
 20 //var ;
 21 eval(library_namespace.use_function(this));
 22 
 23 
 24 
 25 
 26 /**
 27  * null module constructor
 28  * @class	檔案操作相關之 function。
 29  */
 30 CeL.application.storage.file
 31 = function() {
 32 	//	null module constructor
 33 };
 34 
 35 /**
 36  * for JSDT: 有 prototype 才會將之當作 Class
 37  */
 38 CeL.application.storage.file
 39 .prototype = {
 40 };
 41 
 42 
 43 
 44 
 45 //	path處理	-------------------------------------------------------
 46 
 47 //	path,mode=1:去除檔名,只餘目錄,如輸入http://hostname/aaa/bbb/ccc得到http://hostname/aaa/bbb/	尚未處理:: * ?
 48 //reducePath[generateCode.dLK]='dirSp,dirSpR';
 49 function reducePath(p,m){
 50  //alert(typeof p+'\n'+p);
 51  if(!(p=''+p))return;
 52  var t;
 53  if(t=p.match(/^"([^"]*)/))p=t[1];
 54  if(t=p.match(/(.*)\|<>/))p=t[1];
 55  //Windows environment variables在真實path前,尚未測試!
 56  if(typeof WinEnvironment=='object'&&(t=p.match(/%(.+)%/g)))for(i in t)
 57   if(WinEnvironment[i])p.replace(new RegExp(i,"ig"),WinEnvironment[i]);
 58  p=p.replace(new RegExp(dirSp=='/'?'\\\\':'/',"g"),dirSp);
 59  if(m&&(t=p.lastIndexOf(dirSp))!=-1&&t+1!=p.length)p=p.slice(0,t+1);//去除檔名:假如輸入sss/ddd,會把ddd除去!需輸入sss/ddd/以標示ddd為目錄
 60  //p=p.replace(new RegExp(dirSp+dirSp,'g'),dirSp);	//	\\→\,未考慮到'\\pictures\scenic\canyon.bmp'的情況
 61  return p.replace(new RegExp('^(\\.'+dirSpR+')+'),'')	//	.\→''
 62  .replace(new RegExp(dirSpR+'(\\.'+dirSpR+')+','g'),dirSp)	//	\.\→\
 63  .replace(new RegExp('[^.'+dirSpR+']+'+dirSpR+'\\.\\.'+dirSpR,'g'),'');	//	xx\..\→''
 64 }
 65 //alert(reducePath('http://hostname/../aaa/bbb/../ccc/../ddd',1));
 66 
 67 //	去除hostname等,如輸入http://hostname/aaa/bbb/ccc得到aaa/bbb/ccc/
 68 //	假如輸入的格式不正確,可能得出不預期的回應值!
 69 /*	對dirSp.length>1的情形(嚴謹)
 70 function getPathOnly(p){
 71  //discard hash & search
 72  var i=p.lastIndexOf('?'),j=p.lastIndexOf('#'),dirSpL=dirSp.length;
 73  if(i==-1)i=j;else if(j!=-1&&i>j)i=j;if(i!=-1)p=p.slice(0,i);
 74  //	去除http://hostname/等
 75  if(p.slice(0,5)=='file:///')p=p.substr('file:///'.length);	//	對file:///特別處理!
 76  else if((i=p.indexOf(':'+dirSp+dirSp))!=-1&&(i=p.indexOf(dirSp,i+(':'+dirSp+dirSp).length))!=-1))p=p.substr(i+dirSpL);	//	http://hostname/path→path
 77  else if(p.slice(0,dirSpL)==dirSp)
 78  //	/usr/local/→usr/local/
 79  if(p.substr(dirSpL,dirSpL)!=dirSp)p=p.substr(dirSpL);
 80  //	去除\\hostname\
 81  else if((i=p.indexOf(dirSp,dirSpL+dirSpL))>dirSpL+dirSpL)p=p.substr(i+dirSpL);
 82  //	\\\zzzz的情形:不合法的路徑
 83  else if(i!=-1)throw new Error(1,'illegal path:'+p);
 84  return p;
 85 }
 86 */
 87 //	對dirSp.length==1的情形簡化
 88 //getPathOnly[generateCode.dLK]='dirSp';//,isFile
 89 function getPathOnly(p){
 90  //discard hash & search
 91  var i=p.lastIndexOf('?'),j=p.lastIndexOf('#');
 92  if(i==-1)i=j;else if(j!=-1&&i>j)i=j;if(i!=-1)p=p.slice(0,i);
 93  //	去除http://hostname/等
 94  if(p.slice(0,8)=='file:///')p=p.substr(8);	//	對file:///(應該是file:)特別處理!
 95  else if((i=p.indexOf(':'+dirSp+dirSp))!=-1&&(i=p.indexOf(dirSp,i+3)!=-1))p=p.substr(i+1);	//	http://hostname/path→path
 96  else if(p.charAt(0)==dirSp)
 97   //	/usr/local/→usr/local/
 98   if(p.charAt(1)!=dirSp)p=p.substr(1);
 99   //	去除\\hostname\	不去除:.replace(/[^\\]+$/,'')
100   else if((i=p.indexOf(dirSp,2))>2)p=p.substr(i+1);
101   //	\\\zzzz的情形:不合法的路徑
102   else if(i!=-1)throw new Error(1,'illegal path:'+p);
103  if(typeof isFile=='function'&&isFile(p))	//	!isWeb()&&~
104   p=p.replace(new RegExp(dirSpR+'[^'+dirSpR+']+$'),dirSp);
105  return p;
106 }
107 
108 
109 
110 
111 /*	2003/10/1 15:57
112 	pn(path now)相對於bp(base path)之path(增加../等)
113 */
114 //relative_path[generateCode.dLK]='reducePath,is_absolute_path,same_length,dirSp,dirSpR';
115 //,WScript,WshShell
116 function relative_path(bp,pn){
117  if(!pn)pn=typeof location=='object'?location.href:typeof WScript=='object'?WScript.ScriptFullName:'';
118  if(!bp)bp=typeof location=='object'?location.href:typeof WshShell=='object'?WshShell.CurrentDirectory:typeof WScript=='object'?WScript.ScriptFullName:'';
119  //alert('relative_path: parse 1\n'+bp+'\n'+pn);
120  var p=reducePath(pn);
121  if(!p)return;
122  var d=reducePath(bp,1);
123  if(!d||!is_absolute_path(d))return p;	//	bp需要是絕對路徑
124 
125  //alert('relative_path: parse 2\n'+d+'\n'+p);
126  if(!is_absolute_path(p)){	//	p非絕對路徑時先處理成絕對路徑
127   var q=p.indexOf(dirSp,1);	//	預防第一字元為dirSp
128   if(q==-1)q=p;else q=p.slice(0,q);	//	取得第一識別用目錄名
129   //alert('relative_path: parse 3\n'+d+'\n'+q);
130   q=d.indexOf(q);
131   if(q==-1)return p;
132   p=d.slice(0,q)+p;
133 
134 /*
135   var i=0,q=p.split(dirSp),s=new Array(q.length),a=-1,P,bigPC=0,bigP;
136   //	找出最大連續相同路徑:尚未最佳化
137   for(i=0;i<q.length;i++){
138    if(a==-1)P=q[i];else P+=dirSp+q[i];
139    if(d.indexOf(P)==-1){if(a!=-1&&s[a]>bigPC)bigPC=s[a],bigP=P;a=-1;}
140    else{if(a==-1)a=i;++s[a];}
141   }
142   d=d.indexOf(bigP);
143 */
144  }
145  var s=same_length(p,d);
146 
147  //alert('dirSp:	'+dirSp+'\npath now:\n	'+p+'\nbase path:\n	'+d+'\nsame:	'+s);
148  //alert(p+'\n'+d+'\n'+s+'\n'+d.substr(s)+'\n'+d.substr(s).match(new RegExp(dirSp,'g')).length);
149  //pLog(d.charAt(s-1)+','+d.slice(0,s)+':'+s+','+d.slice(0,s).lastIndexOf(dirSp));
150  if(s>0&&d.charAt(s-1)!=dirSp)s=d.slice(0,s).lastIndexOf(dirSp)+1;
151  return s>0?d.substr(s).replace(new RegExp('([^'+dirSpR+']+'+dirSpR+')','g'),'..'+dirSp)+p.substr(s):p;
152 }
153 //	想要保持 Protocol,但卻是不同機器時	http://nedbatchelder.com/blog/200710.html#e20071017T215538
154 //alert(relative_path('//lyrics.meicho.com.tw/game/game.pl?seg=diary21','cgi-bin/game/photo/'));WScript.Quit();
155 
156 
157 
158 CeL.application.storage.file
159 .
160 /**
161  * determine base path.
162  * 給定 base path 的結構後,藉由 path_now 推測 base path 的 full path.
163  * cf. 
164  * @param {String} base_path_structure	base path 的範本結構
165  * @param {String} path_now
166  * @return	{String}	推測的 base path full path
167  * @example
168  * alert(determine_base_path('kanashimi/www/cgi-bin/game/'));
169  * @requres	reducePath,getPathOnly,dirSp,dirSpR
170  * @memberOf	CeL.application.storage.file
171  */
172 determine_base_path=function (base_path_structure, path_now) {
173 	if (!path_now)
174 		path_now = library_namespace.get_base_path();
175 
176 	var p = reducePath(path_now, 1);
177 	if (!p)
178 		return;
179 	if (!base_path_structure)
180 		return p;
181 
182 	var i, j, k, t,
183 	// or use .split()
184 	d = getPathOnly(reducePath(base_path_structure, 1))
185 		.match(new RegExp('([^' + dirSpR + ']+' + dirSpR + ')', 'g'));
186 	if (!d)
187 		return;
188 
189 	for (i = 0, t = ''; i < d.length; i++)
190 		if (p.lastIndexOf(dirSp + d[i]) !== -1) {
191 			t = dirSp;
192 			while (d[i] && (k = p.lastIndexOf(t + d[i])) !== -1)
193 				j = k, t += d[i++];
194 			while (d[i])
195 				t += d[i++];
196 			break;
197 		}
198 	if (!t)
199 		//alert("Can't find base directory of this file!\n" + path_name + '\n\nTreat base directory as:\n' + p);
200 		return p;
201 
202 	//alert('determine_base_path:\nbp='+bp+'\npn='+pn+'\n\n'+p.slice(0,j)+'\n'+t+'\n'+(t.replace(new RegExp('([^'+dirSpR+']+'+dirSpR+')','g'),' ').length-1));
203 	return p.slice(0, j) + t;
204 };
205 
206 
207 CeL.application.storage.file
208 .
209 /**
210  * cf: getFN()
211  * @param {String} path	path name
212  * @return
213  * @memberOf	CeL.application.storage.file
214  */
215 parse_path=function (path) {
216 	if (typeof path !== 'string' || !path)
217 		return;
218 
219 	var path_data = {
220 		oInput : path
221 	}, m;
222 
223 	if (m = path.match(/^(([A-Za-z]):\\)(([^\\]+\\)*)([^\\]+)?$/))
224 		path_data.drive = m[2],
225 		path_data.path_name = m[3],
226 		path_data.file_name = m[5];
227 	else if (m = path
228 			.match(/^file:\/\/\/([A-Za-z]):\/(([^\/]+\/)*)([^\/]+)?$/))
229 		path_data.drive = m[1],
230 		path_data.path_name = m[2].replace(/\//g, '\\'),
231 		path_data.file_name = m[4].replace(/\//g, '\\');
232 
233 	path_data.path = path_data.path_name + path_data.file_name;
234 	path_data.location = path_data.drive + ':\\' + path_data.path;
235 	path_data.directory = path_data.drive + ':\\' + path_data.path_name;
236 
237 	return path_data;
238 };
239 
240 
241 CeL.application.storage.file
242 .
243 /**
244  * is absolute or relative path, not very good solution
245  * @param {String} path
246  * @return
247  * @requires	dirSp,dirSpR
248  * @memberOf	CeL.application.storage.file
249  */
250 is_absolute_path=function (path) {
251 	//alert(typeof path + '\n' + path);
252 	return path
253 		&& (dirSp === '/' && path.charAt(0) === dirSp || new RegExp(
254 		'^(\\\\|[A-Za-z]+:)' + dirSpR).test(path))
255 		// ?true:false
256 		;
257 };
258 
259 
260 //	轉成path(加'\')
261 function turnToPath(p){return p?p+(p.slice(-1)=='\\'?'':'\\'):'';}
262 //	僅取得path部分(包括 dirSp),不包括檔名。
263 //getFilePath[generateCode.dLK]='dirSp';
264 function getFilePath(p){
265  var i=p.lastIndexOf(dirSp);
266  if(i==-1)p+=dirSp;	//	相對路徑?
267  else if(i<p.length-1)p=p.slice(0,i+1);	//	取得path部分
268  return p;
269 }
270 /*	傳回包括檔名之絕對/相對路徑,假如是資料夾,也會回傳資料夾路徑。可包含'.','..'等	the return value include ? #
271 	在Win/DOS下輸入'\'..會加上base driver
272 	若只要相對路徑,可用reducePath()。取得如'..\out'的絕對路徑可用getFP('../out',1)
273 */
274 //getFP[generateCode.dLK]='determine_base_path,reducePath,is_absolute_path,getPathOnly,relative_path';
275 function getFP(p,m,bp){	//	path,mode=0:傳回auto(維持原狀),1:傳回絕對路徑,2:傳回相對路徑,base path
276  //old:	return (p.lastIndexOf('\\')==-1&&p.lastIndexOf('/')==-1?getFolder(getScriptFullName()):'')+p;//getF
277  if(!p)return'';
278  if(p.charAt(0)=='\\'&&determine_base_path(bp).match(/^(\\\\|[A-Za-z]+:)/))p=RegExp.$1+p;
279  p=reducePath(p);
280  if(m==1){
281   if(!is_absolute_path(p))p=reducePath((bp?getPathOnly(bp):determine_base_path())+p);	//	當為相對路徑時前置base path
282  }else if(m==2&&is_absolute_path(p))p=relative_path(determine_base_path(bp),p);
283  return p;
284 }
285 //	傳回檔名部分,the return value include ? #
286 //getFN[generateCode.dLK]='getFP,dirSp';
287 function getFN(p,bp,m){	//	path,base path,mode=0:檔名,1:(當輸入為不可信賴的字串時)去除檔名中不允許的字元,割掉? #等
288  p=getFP(p,0,bp);
289  p=p.slice(p.lastIndexOf(dirSp)+1);	//	不能用.substr(p.lastIndexOf(dirSp))+dirSp,因為p.lastIndexOf(dirSp)可能==-1	//	比起(m=p.lastIndexOf(dirSp))==-1?p:p.substr(m+1);此法比較直接,不過感覺多一道手續…
290  if(m){
291   if(p.match(/[#?]/))p=p.substr(0,RegExp.lastIndex-1);
292   p=p.replace(/[\\\/:*?"<>|]/g,'_');//[ \.]	//	去除檔名中不允許的字元
293  }
294  return p;
295 }
296 //	傳回檔案/資料夾物件	FileSystemObjectのバグ(制限)で、環境によっては2G以上の領域を認識できません。WSH5.6ではこのバグが修正されています。
297 //getF[generateCode.dLK]='isFile,dealShortcut,getFP,dirSp,getFolder,initWScriptObj';
298 function getF(p,m,bp){	//	path,mode=0:auto(維持原狀),1:絕對路徑,2:相對路徑,base path
299  try{return isFile(p=dealShortcut(getFP(p,m,bp),1))?fso.GetFile(p):fso.GetFolder(p);}
300  catch(e){return p.indexOf(dirSp)==-1?getF(getFolder(WScript.ScriptFullName)+p,m,bp):null;}
301 }
302 //alert(getFP('\program files\\xxx\\xxx.exe',2));
303 
304 
305 
306 
307 
308 
309 
310 return (
311 	CeL.application.storage.file
312 );
313 }
314 
315 
316 });
317 
318