/***************************************************************************
                   Utility function  ( UtilFn.cpp )
                -----------------------------------------

	begin      : 2013/03/15
	copyright  : (C) 2013 Solbox Inc.
	author     : Development 1 Team
					- 2013/03/15 - 1st dadamin
	email      : dev1@solbox.com
	version    : 3.2.0

	CopyRight(C) 2005 Solbox Inc. All Rights reserved.
	Redistribution and use in source and binary forms, with or with out
	modification, are not permitted in outside of Solbox Inc.
***************************************************************************/
#include "rc_mngd.h"
#include "UtilFn.h"
#include "Logger.h"

uint64_t GetTimeStamp() 
{
	struct timeval tv;
	gettimeofday(&tv,NULL);
	return tv.tv_sec*(uint64_t)1000000+tv.tv_usec;
}

void StringSplit(string str, string delim, vector<string> &results)
{
	const string strEmpty("");
	string::size_type cutAt;

	while( (cutAt = str.find_first_of(delim)) != str.npos )
	{
		if(cutAt > 0)
		{
			results.push_back(str.substr(0,cutAt));
		}
		else
		{
			if(cutAt == 0)
				results.push_back(strEmpty);
		}
		str = str.substr(cutAt+1);
	}
	if(str.length() > 0)
	{
		results.push_back(str);
	}
}

void StringSplitEx(string str, char delim, vector<string> &results)
{
	const string strEmpty("");
	string::size_type cutAt;

	while( (cutAt = str.find_first_of(delim)) != str.npos )
	{
		if(cutAt > 0)
		{
			results.push_back(str.substr(0,cutAt));
		}
		else
		{
			if(cutAt == 0)
				results.push_back(strEmpty);
		}
		str = str.substr(cutAt+1);
	}
	if(str.length() > 0)
	{
		results.push_back(str);
	}

}

string p_name(string const & path)
{
	return path.substr(0, path.find_last_of("/\\"));
}

void solusleep(unsigned long usec)
{
	// usleep 사용시 multi thread 환경에서 block 발생 가능성이 존재
	// 이에 nanosleep 함수를 사용토록 수정 처리함.
	struct timespec sleep;

	if (usec >= 1000000)
	{
		sleep.tv_sec = (int)(usec / 1000000);
		sleep.tv_nsec = (usec - ((long)sleep.tv_sec * 1000000)) * 1000;
	}
	else
	{
		sleep.tv_sec = 0;
		sleep.tv_nsec = usec * 1000;
	}

	nanosleep(&sleep, NULL);
}