#include "stdafx.h"
#include <stdio.h>
#include <math.h>
#include "stringfun.h"

using namespace std;
//tag!=' ',else flase!!! normally tag==','
int getNumArgs(string s,char tag)
{
	int count=0;
	int i=0;
	bool flag=false;
	for(i=0;i<s.length();i++){
		if(s[i]==' ')
			;
		else if(s[i]==tag){
			if(flag==true){
				count++;
				flag=false;
			}
		}
		else flag=true;
	}

	if(flag==true)
		count++;

	return count;
}

string getArg( int index, string s, char tag)
{
	string value;
	int count=0;
	int i=0,j=0;
	bool flag=false;
	for(i=0;i<s.length();i++){
		if(s[i]==' ')
			;
		else if(s[i]==tag){
			if(flag==true){
				count++;
				flag=false;
				if(index==count){
					value.assign(s,j,i-j);
					return value;
				}
			}
			j=i+1;
		}
		else flag=true;
	}

	if(flag==true){
		count++;
		if(index==count){
			value.assign(s,j,i-j);
		}
	}
	return value;
}

bool CompareStart(string s, string begin)
{
	return !s.compare(0,begin.length(),begin);
}
string stripWhiteSpace( string s )
{
	string value;
	int i,j;
	int length=s.length();
	for(i=0;i<length&&s[i]==' ';i++)
		;
	for(j=length-1;j>=0&&s[j]==' ';j--)
		;
	value.assign(s,i,j-i+1);		
	return value;
}

double StringToFloat(string s)
{
	char * str;
	double value;
	int i;
	str=new char[s.length()+1];
	for(i=0;i<s.length();i++)
		str[i]=s[i];
	str[i]='\0';
	value=atof(str);
	delete str;
	return value;
}


string FloatToString(double value)
{
	char str[36];
	string s;
	sprintf(str,"%f",value);
	s=str;
	return s;
}

string GetFileExt(string& fileName){
	int i;
	i=fileName.length()-1;
	while(fileName[i--]!='.');
	return fileName.substr(i+1,fileName.length()-i-1);
}

void fNameAddNum(string& fileName,int num)
{
	char str[100];
	sprintf(str,"%d",num);
	addFilePost(fileName,str);
}

void fNameToNum(string& fileName,int number)
{
	string strnum,fileExt,result;
	int i,j,k,length;
	int FormatWidth;
	char strbuffer[100];
	length=fileName.length();
	i=length;

	fileExt=GetFileExt(fileName);

	while(fileName[i--]!='.'&&i>=0);
	j=i+1;
	while(isdigit(fileName[i])&&i>=0)
		i--;
	i++;//prefix characters number
	if(i!=0)
		result=fileName.substr(0,i);

	FormatWidth=j-i;
	if(FormatWidth<log10((float)number))
		FormatWidth = log10((float)number);

	switch(FormatWidth){
	case 2:
		sprintf(strbuffer,"%.2d",number);
		break;
	case 3:
		sprintf(strbuffer,"%.3d",number);
		break;
	case 4:
		sprintf(strbuffer,"%.4d",number);
		break;
	case 5:
		sprintf(strbuffer,"%.5d",number);
		break;
	case 6:
		sprintf(strbuffer,"%.6d",number);
		break;
	case 7:
		sprintf(strbuffer,"%.7d",number);
		break;
	case 8:
		sprintf(strbuffer,"%.8d",number);
		break;
	case 9:
		sprintf(strbuffer,"%.9d",number);
		break;
	case 10:
		sprintf(strbuffer,"%.10d",number);
		break;
	default:
		sprintf(strbuffer,"%d",number);
		break;
	}
	strnum=strbuffer;
	result += strnum;
	result+=fileExt;
	
	fileName = result;
}

void fNameIncNum(string& fileName,int num)
{
	string strnum,fileExt,result;
	int i,j,k,length;
	int number;
	int FormatWidth;
	char strbuffer[100];
	length=fileName.length();
	i=length;

	fileExt=GetFileExt(fileName);

	while(fileName[i--]!='.'&&i>=0);
	j=i+1;
	while(isdigit(fileName[i])&&i>=0)
		i--;
	i++;//prefix characters number
	if(i!=0)
		result=fileName.substr(0,i);
	FormatWidth=j-i;
	if(i==j)
		number=num;
	else{

		strnum=fileName.substr(i,j-i);
		number=atoi(strnum.c_str());
		number+=num;
	}

	if(FormatWidth<log10((float)number))
		FormatWidth = log10((float)number);

	switch(FormatWidth){
	case 2:
		sprintf(strbuffer,"%.2d",number);
		break;
	case 3:
		sprintf(strbuffer,"%.3d",number);
		break;
	case 4:
		sprintf(strbuffer,"%.4d",number);
		break;
	case 5:
		sprintf(strbuffer,"%.5d",number);
		break;
	case 6:
		sprintf(strbuffer,"%.6d",number);
		break;
	case 7:
		sprintf(strbuffer,"%.7d",number);
		break;
	case 8:
		sprintf(strbuffer,"%.8d",number);
		break;
	case 9:
		sprintf(strbuffer,"%.9d",number);
		break;
	case 10:
		sprintf(strbuffer,"%.10d",number);
		break;
	default:
		sprintf(strbuffer,"%d",number);
		break;
	}
	strnum=strbuffer;
	result += strnum;
	result+=fileExt;
	
	fileName = result;

}

void addFilePost(string& fileName,const char* postfix)
{
	string  prefix,fileTail,result;
	int length=fileName.length();
	int i;
	i=length-1;
	while(i>=0&&fileName[i--]!='.');	
	if(i==-1)	//no '.'
		i=length-1;
	fileTail=new char(length-i);
	i++;			//prefix string number
	prefix=new char(i+1);
	prefix=fileName.substr(i);
	prefix[i]='\0';
	fileTail=fileName.substr(i);	//fileTail contain '.'
	result+=prefix;
	result+=postfix;
	result+=fileTail;//result=prefix+postfix+fileTail

	fileName = result;
}

void addFilePost2(string& fileName,const char* postfix)
{
	string  prefix,fileTail,result;
	int length=fileName.length();
	int i;
	i=length-1;
	while(i>=0&&fileName[i--]!='.');	
	if(i==-1)	//no '.'
		i=length-1;
	while(isdigit(fileName[i]))
		i--;
	i++;			//prefix string number
	prefix=fileName.substr(i);
	fileTail=fileName.substr(i);	//fileTail contain '.'
	result=prefix;
	result+=postfix;
	result+=fileTail;//result=prefix+postfix+fileTail

	fileName = result;
}
