// $Id: parse.js 4432 2006-09-14 07:45:55Z shacka $
/*
 * <pre>
 * Copyright (c) 2006 by SouthProjects
 * http://www.southprojects.com
 * Santiago, Chile,
 * Version 1.2 Dec 2006.
 * All rights reserved.
 * </pre>
 */

southprojects.Parse=function(str2parse, delim)
{
this.init()
this.arr=[]
if (arguments.length > 1)
{
	this.str2parse=this.trim(str2parse)
	this.delim=delim
	this.parse(this.str2parse, this.delim)
}

}

southprojects.Parse.prototype.init=function() {
}

southprojects.Parse.prototype.arr_clear=function() {
	this.arr=[]
}

southprojects.Parse.prototype.parse=function(str2parse, delim) 
{
var i
this.arr=str2parse.split(this.delim)
for (i=0; i<this.arr.length; i++)
	this.arr[i]=this.trim(this.arr[i])
}

// return number fields
southprojects.Parse.prototype.NF=function(s) { return this.arr.length }

// check if field number is in range, 0 to N-1
southprojects.Parse.prototype.validField=function(iFld) { return iFld >=0 && iFld <=this.NF()-1 }

// get the field based on the field number
southprojects.Parse.prototype.getField=function(iFld) { 
if (!this.validField(iFld)) return undefined
return this.arr[iFld]
}

// set the field based on the field number, field number must be valid
southprojects.Parse.prototype.setField=function(iFld, value) { 
if (!validField(iFld)) return undefined
this.arr[iFld]=value
}

// push field to end of list
southprojects.Parse.prototype.pushField=function(value) { this.arr.push(value) }

// pop field off array
southprojects.Parse.prototype.popField=function(value) { return this.arr.pop() }

// Add field into list a,b,d,e addField(2,0,c) --> a,b,c,d,e
southprojects.Parse.prototype.insertField=function(iFld, value) { this.arr.splice(iFld,0,value) }


// trim Leading white spaces
southprojects.Parse.prototype.pretrim = function(s) {
  // Convert to string
  s += '';
  return s.replace(/^\s?/, '');
};

// trim Trailing white spaces
southprojects.Parse.prototype.posttrim = function(s) {
  // Convert to string
  s += '';
  return s.replace(/\s?$/, '');
};

// trim Leading and Trailing white spaces
southprojects.Parse.prototype.trim=function(s)
{
	s=this.pretrim(s)
	s=this.posttrim(s)
	return s
}


// For given array, flatfile with delim as separator
southprojects.Parse.prototype.flatfile=function(delimOut) 
{
var strFld, i, strFlat=''
for (i=0; i<this.NF(); i++)
	{
	strFld=this.getField(i) || ''
	strFld=strFld.replace(/"/, "\"")
	if (strFld.indexOf(delimOut) != -1) 
		// if delim in field then enclose in quotes
		strFld='"' + strFld + '"'
	strFlat+=strFld
	if (i<this.NF()-1)	
		// last field?, no delim
		strFlat += delimOut
	}

	return strFlat
}
