#include "CivInpImpoter.h"
// #include "CivSysLog.h"
#include <stdio.h>

CivInpImpoter::CivInpImpoter()
{

}

CivInpImpoter::~CivInpImpoter()
{

}

int CivInpImpoter::handleline(int linenum, char* text) 
{
    printf("line[%d]=%s\n", linenum, text);
    return 0;
}

bool CivInpImpoter::load(const std::string& inppath)
{
    FILE* fp;
    int linenum;
    char* p, buf[1024];
   // CivSysLog::getInstance()->info("开始解析："+inppath, "CivInpImpoter", __FUNCTION__);
    // 打开文件
    fp = fopen(inppath.c_str(), "r");
    if (fp == NULL) 
    {
       /* std::string errInfo = "打开文件: " + inppath + "失败！";
        CivSysLog::getInstance()->error(errInfo, "CivInpImpoter", __FUNCTION__);*/
        return false;
    }

    // 按行解析数据
    for (linenum = 1; fgets(buf, sizeof(buf), fp) != NULL; ++linenum) {
        if ((p = strchr(buf, '\n')) == NULL) {
            p = buf + strlen(buf);
        }
        if (p > buf && p[-1] == '\r') {
            --p;
        }
        *p = '\0';

        //空格跳过
        for (p = buf; *p != '\0' && isspace((int)*p); ++p) 
        {
            ;
        }

        if (*p == '\0' || *p == ';') {
            continue;
        }

        // 处理解析的行数据
        if (handleline(linenum, p) != 0) {
          /*  std::string errInfo = "不能解析第：" + std::to_string(linenum) + "行,跳过";
            CivSysLog::getInstance()->warning(errInfo, "CivInpImpoter", __FUNCTION__);*/
            continue;
        }
    }
    fclose(fp);
	return true;
}

bool CivInpImpoter::import()
{
	return true;
}
