/********************************************
Copyright Pavel Potapov, 2013-2016
distributed under GNU General Public License
********************************************/

/***********************CHECK********************************/

if(doesfunctionexist("replacefragmentintext112"))
	{
	result("\nfunctions "+"Strings112"+" already installed\n");
	throw("")
	}	
result("\ninstall functions "+"Strings112")

/*****************************************************
Replace certain fragments in text with the new ones
*****************************************************/

string ReplaceFragmentInText112(string OldText,string Marker1, string Marker2,string Replace)
{		//	OLD:	####MARKER1$$$$$MARKER2####
		//	NEW:	####REPLACE####
number length = len(OldText)
number entrance1 = find(OldText,Marker1)
if(entrance1 ==(-1)) return OldText
//result("\nl "+length+" e1 "+entrance1)
string Before = left(OldText,entrance1)
string After = right(OldText,length-entrance1-len(Marker1))
if(Marker2!="") After = right(OldText,length-entrance1-find(After,Marker2)-len(Marker1)-len(Marker2))

return Before + Replace + ReplaceFragmentInText112(After,Marker1,Marker2,Replace)
							//reccurent function
}

/**********************************************************************
Read string till deliminator (may consequently)
**********************************************************************/
string ReadStringWithDeliminator112(string &DataString,string delim)
{
number pos = DataString.find(Delim)
//result("\n"+pos)
if(pos!=(-1)) 
	{
	string fragm = left(DataString,pos)
	DataString = DataString.right(len(DataString)-pos-1)
	return fragm
	}
else return DataString	
}

/**********************************************************************
Stripe string from identical symbols from left or from right
**********************************************************************/
String StripeString112( string in,string symb,string side)
{ 
if(len(in)==0) return in
	
if(left(side,1)=="L") for(number i=1;i<=len(in);i++)
	{
	if(left(in,1)==symb) in = right(in,len(in)-1)
	else break
	}
		
if(right(side,1)=="R")for(number i=1;i<=len(in);i++)
	{
	if(right(in,1)==symb) in = left(in,len(in)-1)
	else break
	}
		
return in
}

/**********************************************************************
Add symbols to string  from left or from right to extend it till desired width
**********************************************************************/
string AdaptStringWidth112(string in,string symb,number W,string side)
	{
	string add
	number L = len(in)
	number diff = W-L
	if(diff>0)
		{
		for(number i=1;i<=diff;i++) add+=symb
		if(left(side,1)=="L") in = add + in
		if(right(side,1)=="R")in = in + add
		if(side=="LR") 
			{
			in = left(in,len(in)-round(diff/2))
			in = right(in,len(in)-round(diff/2-0.1))
			}
		}
		
	return in
	}

/**********************************************************************
Function: Constract string of sequential number with fixed length
**********************************************************************/
string SequentialNumber112(number i,number digits)
	{
	string SeqNum = ""+i
	for(number dig=1;dig<=(digits-1);dig++)
		{
		if(i<=(10**dig-1)) SeqNum = "0"+ SeqNum
		}
	
	return SeqNum
	}
	
/**********************************************************************
Function: Extract the Head name in batch format ("HeadName-01","HeadName-02",...)
**********************************************************************/
string ExtractBatchName112(string Name, number dig)
	{
	string ext = PathExtractExtension(Name,0)
	string tail = right(name,len(ext)+ dig + 2)	//piece after BatchName
	string BatchName = left(Name,len(Name)-len(Tail))
	tail = left(tail,len(tail)-len(ext)-1)
//result("\n#"+BatchName+"#"+tail+"#"+ext+"#")

	if(left(tail,1)=="-") return BatchName
	else return "X"
	}