namespace string  
import util.metaProperty

var IND_SEP = -1;
var IND_PATH = -2;
var IND_EQ = -3;

var splitList = function(this,str,sep,eq){
 
	var k,ind;
	var tvalue;
 
	var tlist =  split(str,sep);
    for( i,v in tlist){  
        pos = find(v,eq);
        if(pos){ 
        	k = trim(left(v,pos-1));
        	ind = ..table.push(this,k ); 
        	this[k] = trim( sub(v,pos+1) );
        }
    }	
}


class list{ 
    ctor(str,sep='\r\n',eq='=') { 
        this[IND_SEP] = sep;
        this[IND_EQ] = eq;

        if(..io.exist(str) )
        	str = ..string.load(str);
        
        if(str)	
       		splitList(this,str,sep,eq);
    };
     
    @metaProperty
}

list.metaProperty = util.metaProperty(

	save = function(filepath){ 
		filepath := owner[IND_PATH];
		owner[[IND_PATH]] = filepath; 
		
    	..string.save(filepath,tostring(owner) )
    }
    load = function(filepath){  
    
    	var sep = owner[[IND_SEP]];
    	var eq = owner[[IND_EQ]];
    	
    	owner ={ owner@ }
    	var str = ..string.load( filepath )
    	
       	owner[[IND_PATH]] = filepath;
       	owner[[IND_EQ]] = eq
       	owner[[IND_SEP]] = sep; 
       	
       	splitList(owner,str,sep,eq);
    } 
	set = function(k,v) { 
		..table.push(owner,k) 
		owner[[k]] = v; //使用[[]]操作符不会再次触发元方法，可以避免函数递归溢出。
		return owner ; 
	} 
	_tostring = function() {
		if(  not #owner ) return "";
 		var eq = owner[IND_EQ]
		var t ={}
		for( i= 1 ; #owner){
    		..table.push(t, ..string.concat( owner[i],eq,owner[owner[i] ] ) );
		} 
		return ..string.join(t,owner[IND_SEP]) ;
	};
)
 

/**intellisense()
string.list(__/*请输入字符串*/) = 创建字符串列表\n可选参数(字符串,分隔符,键值对分隔符)
!stringlist.load("__/*文件路径*/") = 加载配置文件
!stringlist.save("__/*文件路径*/") = 保存配置文件
!stringlist.save() = 保存配置文件
?string.list = !stringlist.
end intellisense**/
