#include "util.h"
#include "check_neocast.h"

int devide(char **val, char *str, char delim)
{
	char *p;
	int i, j;

	val[0] = (char *)malloc(512);
	memset(val[0], 0, 512);

	for (p=str, i=0, j=0; *p != '\0'; p++)
	{
		if (*p == delim)
		{
			*(val[i] + j) = '\0';
			j = 0;
			i++;
			val[i] = (char *)malloc(512);
			memset(val[i], 0, 512);
			continue;
		}
		else if (*p == '\n')
		{
			continue;
		}
		else
		{
			*(val[i] + j++) = *p;
		}
	}
	*(val[i] + j) = '\0';
	return (i+1);
}

char* timeToString(struct tm *t) {
	static char s[20];
	sprintf(s, "%04d-%02d-%02d %02d:%02d:%02d",
              t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
              t->tm_hour, t->tm_min, t->tm_sec
          );
	return s;
}

char *makeLogString()
{
	struct tm *t;
	time_t timer;
	static char log_path_file[128];

	timer = time(NULL);
	t = localtime(&timer);

	snprintf(log_path_file, 128, "%sneo_statusd_%04d%02d%02d.log", DEFAULT_LOGFILE_PATH,
			t->tm_year + 1900, t->tm_mon + 1, t->tm_mday);
	return log_path_file;
}

void WriteSHLog(char *msg, FILE *ifp)
{
	struct tm *t;
    time_t timer;

	timer = time(NULL);
	t = localtime(&timer);
	
	fprintf(ifp, "[%s] %s\n", timeToString(t), msg);	
	fflush(ifp);
}
