/*
 * EH_Util.cpp
 *
 * Copyright by AirDream Co., Ltd. 2010-2011 All right reserved
 *
 * Description:
 * Created on:2013-5-31
 * Modified on:####-##-##
 *
 */

#include "KK_Util.h"
#include <sys/time.h>

int StringToHex(const char* input, unsigned char* output,const int bufLen) {
    int i, len = strlen(input);
    int j = 0; //length
    for (i = 0; i < len; i += 3) {
        if(j > bufLen) {
            return -1;
        }
        int c;
        sscanf(input + i, "%02X", &c);
        *(output + j) = c;
        j++;
    }
    return j;
}

int StringToHexNoSpace(const char* input, unsigned char* output,const int bufLen) {
    int i, len = strlen(input);
    int j = 0; //length
    for (i = 0; i < len; i += 2) {
        if(j > bufLen) {
            return -1;
        }
        int c;
        sscanf(input + i, "%02X", &c);
        *(output + j) = c;
        j++;
    }
    return j;
}

int HexToString(const char* input, char* output, int len,const char* sep) {
    char * p = output;
    int total = 0, ret;
    for (int i = 0; i < len; i++) {
        ret = sprintf(p, "%02X", (unsigned char) (*(input + i)));
        p += ret;
        total += ret;
        if (i < len - 1) {
            *p = *sep;
        }
        ++p;
        ++total;
    }
    return total;
}

void Split(const string& src, const string& separator, vector<string>& dest) {
    if (src == "") {
          return;
      }

      string str = src;
      string substring;
      string::size_type start = 0, index;
      do {
          index = str.find_first_of(separator, start);
          if (index != string::npos) {
              substring = str.substr(start, index - start);
              dest.push_back(substring);
              start = str.find_first_not_of(separator, index);
              if (start == string::npos) {
                dest.push_back("");
                return;
              }
              if (start != (index + 1)) {
            	  int separatorCount = start - index - 1;
            	  for (int i = 0; i < separatorCount; i++) {
                      dest.push_back("");
            	  }
              }
          }
      } while (index != string::npos);
      substring = str.substr(start);
      dest.push_back(substring);
}

 string Replace(const string& str, const string& src, const string& dest)
{
    string ret;

    string::size_type pos_begin = 0;
    string::size_type pos   = str.find(src);
    while (pos != string::npos)
    {
        ret.append(str.data() + pos_begin, pos - pos_begin);
        ret += dest;
        pos_begin = pos + 1;
        pos   = str.find(src, pos_begin);
    }
    if (pos_begin < str.length())
    {
        ret.append(str.begin() + pos_begin, str.end());
    }
    return ret;
}


string IntToString(const int input) {
    stringstream oss;
    oss << input;
    string output;
    oss >> output;
    return output;
}

string UIntToString(const uint input) {
    stringstream oss;
    oss << input;
    string output;
    oss >> output;
    return output;
}

int StringToInt(const string input,int& error) {
    stringstream oss;
    oss << input;
    int output = 0;
    oss >> output;
    if (oss.fail() || !oss.eof()) {
    	error = -1;
    } else {
    	error = 0;
    }
    return output;
}

uint StringToUInt(const string input,int& error) {
    stringstream oss;
    oss << input;
    uint output = 0;
    oss >> output;
    if (oss.fail() || !oss.eof()) {
    	error = -1;
    } else {
    	error = 0;
    }

    return output;
}

double StringToDouble(const string input,int& error) {
    stringstream oss;
    oss << input;
    double output = 0;
    oss >> output;
    if (oss.fail() || !oss.eof()) {
    	error = -1;
    } else {
    	error = 0;
    }

    return output;
}

string DoubleToString(const double input,int& error) {
    stringstream oss;
    oss << input;
    if (oss.fail() || !oss.eof()) {
    	error = -1;
    } else {
    	error = 0;
    }

    return oss.str();
}

unsigned long getSystemTimeInMs() {
	struct timeval now;
	gettimeofday(&now, 0);

	unsigned long  nowInMs = (unsigned long )now.tv_sec * 1000;
	nowInMs += now.tv_usec / 1000;
	return nowInMs;
}

