#include <stdio.h>
#include <stdlib.h>
#include "gterror.h"

std::map<int, std::string> GtError::_errorString;
bool GtError::_errorIsInit = false;

const int GtError::ERROR_TIMEOUT;
const int GtError::ERROR_SELECT;
const int GtError::ERROR_READ_FAILED;
const int GtError::ERROR_WRITE_FAILED;
const int GtError::ERROR_CLOSED;
const int GtError::ERROR_CREATE_SOCKET;
const int GtError::ERROR_SET_SOCKET;
const int GtError::ERROR_BIND_SOCKET;
const int GtError::ERROR_LISTEN_SOCKET;
const int GtError::ERROR_CONNECT_SOCKET;
const int GtError::ERROR_ACCEPT_SOCKET;
const int GtError::ERROR_DIR_NOT_EXIST;
const int GtError::ERROR_DIR_GET_CURRENT;
const int GtError::ERROR_DIR_CREATE;
const int GtError::ERROR_FILE_NOT_EXIST;
const int GtError::ERROR_FILE_OPEN;
const int GtError::ERROR_FILE_WRITE;
const int GtError::ERROR_FILE_READ;
const int GtError::ERROR_DL_OPEN;
const int GtError::ERROR_DL_SYM;
const int GtError::ERROR_DL_INVOKE;
const int GtError::ERROR_INVALID_FUNC_PARAM;
const int GtError::ERROR_INVALID_VALUE;
const int GtError::ERROR_PARSE_FAILED;
const int GtError::ERROR_OVERFLOW;
const int GtError::ERROR_FORMAT;
const int GtError::ERROR_SEM_OP;

const int GtError::ERROR_OTHER;
const int GtError::ERROR_MEM_LOCK_FAILED;
const int GtError::ERROR_MEM_UNLOCK_FAILED;
const int GtError::ERROR_FILE_SERVER_NOT_CONNECT;
const int GtError::ERROR_INVALID_FILE_MD5;
const int GtError::ERROR_GENERATE_ENCRYPT_KEY;
const int GtError::ERROR_WRITE_LAST_TICKET;
const int GtError::ERROR_READ_LAST_TICKET;
const int GtError::ERROR_CLEAR_LAST_TICKET;


void GtError::init()
{
    _errorString.clear();
    _errorString[ERROR_TIMEOUT] = "系统响应超时";
    _errorString[ERROR_SELECT] = "网络select错误";
    _errorString[ERROR_READ_FAILED] = "数据读取错误";
    _errorString[ERROR_WRITE_FAILED] = "数据写入错误";
    _errorString[ERROR_CLOSED] = "网络已断开";
    _errorString[ERROR_CREATE_SOCKET] = "网络创建错误";
    _errorString[ERROR_SET_SOCKET] = "网络设置错误";
    _errorString[ERROR_BIND_SOCKET] = "网络绑定错误";
    _errorString[ERROR_LISTEN_SOCKET] = "网络监听错误";
    _errorString[ERROR_CONNECT_SOCKET] = "网络连接错误";
    _errorString[ERROR_ACCEPT_SOCKET] = "网络accept错误";
    _errorString[ERROR_DIR_NOT_EXIST] = "文件目录不存在";
    _errorString[ERROR_DIR_GET_CURRENT] = "文件目录路径获取错误";
    _errorString[ERROR_DIR_CREATE] = "文件目录创建错误";
    _errorString[ERROR_FILE_NOT_EXIST] = "文件不存在";
    _errorString[ERROR_FILE_OPEN] = "文件打开错误";
    _errorString[ERROR_FILE_WRITE] = "文件写入错误";
    _errorString[ERROR_FILE_READ] = "文件读取错误";
    _errorString[ERROR_DL_OPEN] = "库文件打开错误";
    _errorString[ERROR_DL_SYM] = "库文件接口获取错误";
    _errorString[ERROR_DL_INVOKE] = "库文件接口调用错误";
    _errorString[ERROR_INVALID_FUNC_PARAM] = "无效的接口参数";
    _errorString[ERROR_INVALID_VALUE] = "无效的数据";
    _errorString[ERROR_PARSE_FAILED] = "数据解析错误";
    _errorString[ERROR_OVERFLOW] = "数据溢出错误";
    _errorString[ERROR_FORMAT] = "数据格式化错误";
    _errorString[ERROR_SEM_OP] = "信号灯操作错误";
    _errorString[ERROR_OTHER] = "系统错误";
    _errorString[ERROR_MEM_LOCK_FAILED] = "内存加锁错误";
    _errorString[ERROR_MEM_UNLOCK_FAILED] = "内存解锁错误";
    _errorString[ERROR_FILE_SERVER_NOT_CONNECT] = "连接文件服务器错误";
    _errorString[ERROR_INVALID_FILE_MD5] = "MD5码校验错误";
    _errorString[ERROR_GENERATE_ENCRYPT_KEY] = "生成通讯密匙错误";
    _errorString[ERROR_WRITE_LAST_TICKET] = "保存最近一张票错误";
    _errorString[ERROR_READ_LAST_TICKET] = "读取最近一张票错误";
    _errorString[ERROR_CLEAR_LAST_TICKET] = "清除最近一张票错误";
}

void GtError::localErrorString(int errCode, char *errStr)
{
    if(errStr == NULL)
    {
        return;
    }
    if(!_errorIsInit)
    {
        init();
        _errorIsInit = true;
    }
    sprintf(errStr, "Server disconnect<%d>", errCode);
    #if 0
    std::map<int, std::string>::iterator mapIterator = _errorString.find(errCode);
    if(mapIterator == _errorString.end())
    {
        sprintf(errStr, "Server disconnect<%d>", errCode);
    }
    else
    {
        const char *str = mapIterator->second.data();
        sprintf(errStr, "%s<%d>", str, errCode);
    }
    #endif
}
