1)将ini功能独立到HGIni.h文件

2)实现HGTime.h
3)获取文档目录,不带进程名
4)websdk功能完善
This commit is contained in:
luoliangyi 2022-05-30 19:01:23 +08:00
parent 7b04b4bbaf
commit ad3809266d
25 changed files with 1368 additions and 489 deletions

View File

@ -2,6 +2,7 @@
#include "base/HGDef.h" #include "base/HGDef.h"
#include "base/HGInc.h" #include "base/HGInc.h"
#include "base/HGUtility.h" #include "base/HGUtility.h"
#include "base/HGIni.h"
#include "HGUIGlobal.h" #include "HGUIGlobal.h"
#include "HGString.h" #include "HGString.h"

View File

@ -23,7 +23,10 @@ Dialog_AquireInto::Dialog_AquireInto(QWidget* parent) :
HGChar aquireIntoPath[512]; HGChar aquireIntoPath[512];
HGBase_GetDocumentsPath(aquireIntoPath, 512); HGBase_GetDocumentsPath(aquireIntoPath, 512);
strcat(aquireIntoPath, "AquireInto/"); HGChar procName[512];
HGBase_GetProcessName(procName, 512);
strcat(aquireIntoPath, procName);
strcat(aquireIntoPath, "/AquireInto/");
QString filePath = getStdFileName(StdStringToUtf8(aquireIntoPath).c_str()); QString filePath = getStdFileName(StdStringToUtf8(aquireIntoPath).c_str());
ui->lineEdit_directory->setText(getCfgValue("aquire", "aquireIntoPath", filePath)); ui->lineEdit_directory->setText(getCfgValue("aquire", "aquireIntoPath", filePath));

View File

@ -27,7 +27,10 @@ QString Dialog_ClrCache::getCachePath()
{ {
HGChar cachePath[512]; HGChar cachePath[512];
HGBase_GetDocumentsPath(cachePath, 512); HGBase_GetDocumentsPath(cachePath, 512);
strcat(cachePath, "Cache/"); HGChar procName[512];
HGBase_GetProcessName(procName, 512);
strcat(cachePath, procName);
strcat(cachePath, "/Cache/");
QString filePath = getStdFileName(StdStringToUtf8(cachePath).c_str()); QString filePath = getStdFileName(StdStringToUtf8(cachePath).c_str());
return filePath; return filePath;
} }

View File

@ -42,6 +42,8 @@ HGBase_EnableInfo
HGBase_DisableInfo HGBase_DisableInfo
HGBase_WriteInfo HGBase_WriteInfo
HGBase_GetLocalTime
HGBase_GetTmpPath HGBase_GetTmpPath
HGBase_GetCurrentDir HGBase_GetCurrentDir
HGBase_SetCurrentDir HGBase_SetCurrentDir
@ -57,11 +59,12 @@ HGBase_GetDocumentsPath
HGBase_GetProcessName HGBase_GetProcessName
HGBase_GetFileName HGBase_GetFileName
HGBase_GetFilePath HGBase_GetFilePath
HGBase_StandardiseFileName
HGBase_SetProfileInt HGBase_SetProfileInt
HGBase_SetProfileString HGBase_SetProfileString
HGBase_GetProfileInt HGBase_GetProfileInt
HGBase_GetProfileString HGBase_GetProfileString
HGBase_StandardiseFileName
HGBase_CreateBuffer HGBase_CreateBuffer
HGBase_CreateBufferFromData HGBase_CreateBufferFromData

View File

@ -32,12 +32,14 @@
<ClCompile Include="..\..\..\modules\base\HGImage.cpp" /> <ClCompile Include="..\..\..\modules\base\HGImage.cpp" />
<ClCompile Include="..\..\..\modules\base\HGInfo.cpp" /> <ClCompile Include="..\..\..\modules\base\HGInfo.cpp" />
<ClCompile Include="..\..\..\modules\base\HGInfoImpl.cpp" /> <ClCompile Include="..\..\..\modules\base\HGInfoImpl.cpp" />
<ClCompile Include="..\..\..\modules\base\HGIni.cpp" />
<ClCompile Include="..\..\..\modules\base\HGLock.cpp" /> <ClCompile Include="..\..\..\modules\base\HGLock.cpp" />
<ClCompile Include="..\..\..\modules\base\HGLog.cpp" /> <ClCompile Include="..\..\..\modules\base\HGLog.cpp" />
<ClCompile Include="..\..\..\modules\base\HGMd5.cpp" /> <ClCompile Include="..\..\..\modules\base\HGMd5.cpp" />
<ClCompile Include="..\..\..\modules\base\HGMsgPump.cpp" /> <ClCompile Include="..\..\..\modules\base\HGMsgPump.cpp" />
<ClCompile Include="..\..\..\modules\base\HGMsgPumpImpl.cpp" /> <ClCompile Include="..\..\..\modules\base\HGMsgPumpImpl.cpp" />
<ClCompile Include="..\..\..\modules\base\HGThread.cpp" /> <ClCompile Include="..\..\..\modules\base\HGThread.cpp" />
<ClCompile Include="..\..\..\modules\base\HGTime.cpp" />
<ClCompile Include="..\..\..\modules\base\HGUtility.cpp" /> <ClCompile Include="..\..\..\modules\base\HGUtility.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
@ -54,12 +56,14 @@
<ClInclude Include="..\..\..\modules\base\HGImage.h" /> <ClInclude Include="..\..\..\modules\base\HGImage.h" />
<ClInclude Include="..\..\..\modules\base\HGInfo.h" /> <ClInclude Include="..\..\..\modules\base\HGInfo.h" />
<ClInclude Include="..\..\..\modules\base\HGInfoImpl.hpp" /> <ClInclude Include="..\..\..\modules\base\HGInfoImpl.hpp" />
<ClInclude Include="..\..\..\modules\base\HGIni.h" />
<ClInclude Include="..\..\..\modules\base\HGLock.h" /> <ClInclude Include="..\..\..\modules\base\HGLock.h" />
<ClInclude Include="..\..\..\modules\base\HGLog.h" /> <ClInclude Include="..\..\..\modules\base\HGLog.h" />
<ClInclude Include="..\..\..\modules\base\HGMd5.h" /> <ClInclude Include="..\..\..\modules\base\HGMd5.h" />
<ClInclude Include="..\..\..\modules\base\HGMsgPump.h" /> <ClInclude Include="..\..\..\modules\base\HGMsgPump.h" />
<ClInclude Include="..\..\..\modules\base\HGMsgPumpImpl.hpp" /> <ClInclude Include="..\..\..\modules\base\HGMsgPumpImpl.hpp" />
<ClInclude Include="..\..\..\modules\base\HGThread.h" /> <ClInclude Include="..\..\..\modules\base\HGThread.h" />
<ClInclude Include="..\..\..\modules\base\HGTime.h" />
<ClInclude Include="..\..\..\modules\base\HGUtility.h" /> <ClInclude Include="..\..\..\modules\base\HGUtility.h" />
</ItemGroup> </ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">

View File

@ -130,7 +130,7 @@
<ConformanceMode>true</ConformanceMode> <ConformanceMode>true</ConformanceMode>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<AdditionalIncludeDirectories>../../../modules/;../../../third_party/sha1/;../../../third_party/base64/;../../../third_party/json/;../../../utility/;../../../third_party/libzip/windows/include/;../../../third_party/libcurl/windows/include/;../../../../sdk/include/;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <AdditionalIncludeDirectories>../../../modules/;../../../third_party/sha1/;../../../third_party/base64/;../../../third_party/json/;../../../utility/;../../../third_party/libzip/windows/include/;../../../third_party/libcurl/windows/include/;../../../../sdk/include/;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
<DisableSpecificWarnings>28251;%(DisableSpecificWarnings)</DisableSpecificWarnings> <DisableSpecificWarnings>28251;26812;%(DisableSpecificWarnings)</DisableSpecificWarnings>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>

View File

@ -16,5 +16,8 @@
#include "HGThread.h" #include "HGThread.h"
#include "HGUtility.h" #include "HGUtility.h"
#include "HGInfo.h" #include "HGInfo.h"
#include "HGIni.h"
#include "HGMsgPump.h"
#include "HGTime.h"
#endif /* __HGBASE_H__ */ #endif /* __HGBASE_H__ */

View File

@ -2,6 +2,7 @@
#include "HGInfo.h" #include "HGInfo.h"
#include "HGInc.h" #include "HGInc.h"
#include "HGUtility.h" #include "HGUtility.h"
#include "HGIni.h"
#include <string> #include <string>
#include <vector> #include <vector>

327
modules/base/HGIni.cpp Normal file
View File

@ -0,0 +1,327 @@
#include "HGIni.h"
#include <vector>
#include <string>
static int IniWriteValue(const char* section, const char* key, const char* val, const char* file)
{
typedef std::vector<std::pair<std::string, std::string> > KeyList;
typedef std::vector<std::pair<std::string, KeyList> > SectionList;
SectionList sectList;
FILE* fp = fopen(file, "r");
if (fp != NULL)
{
KeyList* pCurKeyList = NULL;
while (feof(fp) == 0)
{
char lineContent[256] = { 0 };
if (NULL == fgets(lineContent, 256, fp))
{
continue;
}
if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n'))
{
continue;
}
for (size_t i = 0; i < strlen(lineContent); ++i)
{
if (lineContent[i] == '\r' || lineContent[i] == '\n')
{
lineContent[i] = '\0';
break;
}
}
if (lineContent[0] == '[')
{
std::pair<std::string, KeyList> pr;
pr.first = lineContent;
sectList.push_back(pr);
pCurKeyList = &sectList[sectList.size() - 1].second;
}
else
{
int pos = -1;
for (int i = 0; i < (int)strlen(lineContent); ++i)
{
if (lineContent[i] == '=')
{
pos = i;
break;
}
}
if (NULL != pCurKeyList)
{
std::pair<std::string, std::string> pr;
if (-1 != pos)
{
pr.first.assign(lineContent, pos);
pr.second.assign(lineContent + pos + 1);
}
else
{
pr.first = lineContent;
}
pCurKeyList->push_back(pr);
}
}
}
fclose(fp);
}
bool bFindSect = false;
for (size_t i = 0; i < sectList.size(); ++i)
{
if (strcmp(sectList[i].first.c_str(), section) == 0)
{
bool bFindKey = false;
for (size_t j = 0; j < sectList[i].second.size(); ++j)
{
if (strcmp(sectList[i].second[j].first.c_str(), key) == 0)
{
sectList[i].second[j].second = val;
bFindKey = true;
break;
}
}
if (!bFindKey)
{
std::pair<std::string, std::string> pr;
pr.first = key;
pr.second = val;
sectList[i].second.push_back(pr);
}
bFindSect = true;
break;
}
}
if (!bFindSect)
{
std::pair<std::string, KeyList> pr;
pr.first = section;
std::pair<std::string, std::string> pr2;
pr2.first = key;
pr2.second = val;
pr.second.push_back(pr2);
sectList.push_back(pr);
}
fp = fopen(file, "w");
if (fp == NULL)
{
return -1;
}
for (size_t i = 0; i < sectList.size(); ++i)
{
fputs(sectList[i].first.c_str(), fp);
fputs("\n", fp);
for (size_t j = 0; j < sectList[i].second.size(); ++j)
{
fputs(sectList[i].second[j].first.c_str(), fp);
fputs("=", fp);
fputs(sectList[i].second[j].second.c_str(), fp);
fputs("\n", fp);
}
}
fclose(fp);
return 0;
}
static int writeStringValue(const char* section, const char* key, const char* val, const char* file)
{
if (section == NULL || key == NULL || val == NULL || file == NULL)
{
return -1;
}
char sect[256];
sprintf(sect, "[%s]", section);
return IniWriteValue(sect, key, val, file);
}
HGResult HGAPI HGBase_SetProfileInt(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, HGInt value)
{
if (NULL == fileName || NULL == appName || NULL == keyName)
{
return HGBASE_ERR_INVALIDARG;
}
char str[8];
sprintf(str, "%d", value);
if (0 != writeStringValue(appName, keyName, str, fileName))
return HGBASE_ERR_FAIL;
return HGBASE_ERR_OK;
}
HGResult HGAPI HGBase_SetProfileString(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, const HGChar* value)
{
if (NULL == fileName || NULL == appName || NULL == keyName)
{
return HGBASE_ERR_INVALIDARG;
}
if (0 != writeStringValue(appName, keyName, value, fileName))
return HGBASE_ERR_FAIL;
return HGBASE_ERR_OK;
}
static int IniReadValue(const char* section, const char* key, char* val, const char* def, const char* file)
{
typedef std::vector<std::pair<std::string, std::string> > KeyList;
typedef std::vector<std::pair<std::string, KeyList> > SectionList;
SectionList sectList;
FILE* fp = fopen(file, "r");
if (fp != NULL)
{
KeyList* pCurKeyList = NULL;
while (feof(fp) == 0)
{
char lineContent[256] = { 0 };
if (NULL == fgets(lineContent, 256, fp))
{
continue;
}
if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n'))
{
continue;
}
for (size_t i = 0; i < strlen(lineContent); ++i)
{
if (lineContent[i] == '\r' || lineContent[i] == '\n')
{
lineContent[i] = '\0';
break;
}
}
if (lineContent[0] == '[')
{
std::pair<std::string, KeyList> pr;
pr.first = lineContent;
sectList.push_back(pr);
pCurKeyList = &sectList[sectList.size() - 1].second;
}
else
{
int pos = -1;
for (int i = 0; i < (int)strlen(lineContent); ++i)
{
if (lineContent[i] == '=')
{
pos = i;
break;
}
}
if (NULL != pCurKeyList)
{
std::pair<std::string, std::string> pr;
if (-1 != pos)
{
pr.first.assign(lineContent, pos);
pr.second.assign(lineContent + pos + 1);
}
else
{
pr.first = lineContent;
}
pCurKeyList->push_back(pr);
}
}
}
fclose(fp);
}
else
{
strcpy(val, def);
return -1;
}
bool bGetVal = false;
for (size_t i = 0; i < sectList.size(); ++i)
{
if (strcmp(sectList[i].first.c_str(), section) == 0)
{
for (size_t j = 0; j < sectList[i].second.size(); ++j)
{
if (strcmp(sectList[i].second[j].first.c_str(), key) == 0)
{
strcpy(val, sectList[i].second[j].second.c_str());
bGetVal = true;
break;
}
}
break;
}
}
if (!bGetVal)
{
strcpy(val, def);
}
return 0;
}
static int readStringValue(const char* section, const char* key, char* val, const char* def, const char* file)
{
if (section == NULL || key == NULL || val == NULL || def == NULL || file == NULL)
{
return -1;
}
char sect[256];
sprintf(sect, "[%s]", section);
return IniReadValue(sect, key, val, def, file);
}
HGResult HGAPI HGBase_GetProfileInt(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, HGInt def, HGInt* value)
{
if (NULL == appName || NULL == keyName || NULL == value)
{
return HGBASE_ERR_INVALIDARG;
}
char strRet[256] = { 0 };
char strDef[32];
sprintf(strDef, "%d", def);
readStringValue(appName, keyName, strRet, strDef, fileName);
*value = atoi(strRet);
return HGBASE_ERR_OK;
}
HGResult HGAPI HGBase_GetProfileString(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, const HGChar* def, HGChar* value, HGUInt maxLen)
{
if (NULL == appName || NULL == keyName || NULL == value)
{
return HGBASE_ERR_INVALIDARG;
}
readStringValue(appName, keyName, value, def, fileName);
return HGBASE_ERR_OK;
}

27
modules/base/HGIni.h Normal file
View File

@ -0,0 +1,27 @@
#ifndef __HGINI_H__
#define __HGINI_H__
#include "HGDef.h"
#include "HGBaseErr.h"
/* 设置ini文件的值
*/
HGEXPORT HGResult HGAPI HGBase_SetProfileInt(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, HGInt value);
/* 设置ini文件的值
*/
HGEXPORT HGResult HGAPI HGBase_SetProfileString(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, const HGChar* value);
/* 获取ini文件的值
*/
HGEXPORT HGResult HGAPI HGBase_GetProfileInt(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, HGInt def, HGInt* value);
/* 获取ini文件的值
*/
HGEXPORT HGResult HGAPI HGBase_GetProfileString(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, const HGChar* def, HGChar* value, HGUInt maxLen);
#endif /* __HGINI_H__ */

37
modules/base/HGTime.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "HGTime.h"
#include "HGInc.h"
HGResult HGAPI HGBase_GetLocalTime(HGTimeInfo* timeInfo)
{
if (NULL == timeInfo)
{
return HGBASE_ERR_INVALIDARG;
}
#if defined(HG_CMP_MSC)
SYSTEMTIME st;
GetLocalTime(&st);
timeInfo->year = st.wYear;
timeInfo->month = st.wMonth;
timeInfo->day = st.wDay;
timeInfo->dayOfWeek = st.wDayOfWeek;
timeInfo->hour = st.wHour;
timeInfo->minute = st.wMinute;
timeInfo->second = st.wSecond;
timeInfo->milliseconds = st.wMilliseconds;
#else
struct timeval time;
gettimeofday(&time, NULL);
struct tm* p = localtime(&time.tv_sec);
assert(NULL != p);
timeInfo->year = p->tm_year + 1900;
timeInfo->month = 1 + p->tm_mon;
timeInfo->day = p->tm_mday;
timeInfo->dayOfWeek = p->tm_wday;
timeInfo->hour = p->tm_hour;
timeInfo->minute = p->tm_min;
timeInfo->second = p->tm_sec;
timeInfo->milliseconds = time.tv_usec / 1000;
#endif
return HGBASE_ERR_OK;
}

21
modules/base/HGTime.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef __HGTIME_H__
#define __HGTIME_H__
#include "HGDef.h"
#include "HGBaseErr.h"
typedef struct
{
HGUShort year;
HGUShort month;
HGUShort day;
HGUShort dayOfWeek; /* 0为星期天, 1-6表示星期一到星期六 */
HGUShort hour;
HGUShort minute;
HGUShort second;
HGUShort milliseconds;
}HGTimeInfo;
HGEXPORT HGResult HGAPI HGBase_GetLocalTime(HGTimeInfo *timeInfo);
#endif /* __HGTIME_H__ */

View File

@ -6,7 +6,6 @@
#else #else
#include "uuid/uuid.h" #include "uuid/uuid.h"
#endif #endif
#include <vector>
#include <string> #include <string>
HGResult HGAPI HGBase_GetTmpPath(HGChar* path, HGUInt maxLen) HGResult HGAPI HGBase_GetTmpPath(HGChar* path, HGUInt maxLen)
@ -224,7 +223,7 @@ HGResult HGAPI HGBase_GetUuid(HGChar* uuid, HGUInt maxLen)
return HGBASE_ERR_OK; return HGBASE_ERR_OK;
} }
HGResult HGAPI HGBase_GetTmpFileName(HGChar* fileName, HGUInt maxLen) HGResult HGAPI HGBase_GetTmpFileName(const HGChar* suffix, HGChar* fileName, HGUInt maxLen)
{ {
if (NULL == fileName || 0 == maxLen) if (NULL == fileName || 0 == maxLen)
{ {
@ -235,6 +234,11 @@ HGResult HGAPI HGBase_GetTmpFileName(HGChar* fileName, HGUInt maxLen)
HGBase_GetTmpPath(path, 256); HGBase_GetTmpPath(path, 256);
HGBase_GetUuid(uuid, 128); HGBase_GetUuid(uuid, 128);
strcat(path, uuid); strcat(path, uuid);
if (NULL != suffix && 0 != *suffix)
{
strcat(path, ".");
strcat(path, suffix);
}
if (maxLen < strlen(path) + 1) if (maxLen < strlen(path) + 1)
return HGBASE_ERR_FAIL; return HGBASE_ERR_FAIL;
@ -332,22 +336,12 @@ HGResult HGAPI HGBase_GetDocumentsPath(HGChar* documentsPath, HGUInt maxLen)
return HGBASE_ERR_FAIL; return HGBASE_ERR_FAIL;
if (docPath[strlen(docPath) - 1] != '\\') if (docPath[strlen(docPath) - 1] != '\\')
strcat(docPath, "\\"); strcat(docPath, "\\");
HGChar procName[64];
HGBase_GetProcessName(procName, 64);
strcat(docPath, procName);
strcat(docPath, "\\");
#else #else
char docPath[512] = { 0 }; char docPath[512] = { 0 };
strcpy(docPath, getenv("HOME")); strcpy(docPath, getenv("HOME"));
if (docPath[strlen(docPath) - 1] != '/') if (docPath[strlen(docPath) - 1] != '/')
strcat(docPath, "/"); strcat(docPath, "/");
strcat(docPath, "Documents/"); strcat(docPath, "Documents/");
HGChar procName[64];
HGBase_GetProcessName(procName, 64);
strcat(docPath, procName);
strcat(docPath, "/");
#endif #endif
if (maxLen < strlen(docPath) + 1) if (maxLen < strlen(docPath) + 1)
@ -446,330 +440,6 @@ HGResult HGAPI HGBase_GetFilePath(const HGChar* filePath, HGChar* path, HGUInt m
return HGBASE_ERR_OK; return HGBASE_ERR_OK;
} }
static int IniWriteValue(const char* section, const char* key, const char* val, const char* file)
{
typedef std::vector<std::pair<std::string, std::string> > KeyList;
typedef std::vector<std::pair<std::string, KeyList> > SectionList;
SectionList sectList;
FILE* fp = fopen(file, "r");
if (fp != NULL)
{
KeyList* pCurKeyList = NULL;
while (feof(fp) == 0)
{
char lineContent[256] = { 0 };
if (NULL == fgets(lineContent, 256, fp))
{
continue;
}
if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n'))
{
continue;
}
for (size_t i = 0; i < strlen(lineContent); ++i)
{
if (lineContent[i] == '\r' || lineContent[i] == '\n')
{
lineContent[i] = '\0';
break;
}
}
if (lineContent[0] == '[')
{
std::pair<std::string, KeyList> pr;
pr.first = lineContent;
sectList.push_back(pr);
pCurKeyList = &sectList[sectList.size() - 1].second;
}
else
{
int pos = -1;
for (int i = 0; i < (int)strlen(lineContent); ++i)
{
if (lineContent[i] == '=')
{
pos = i;
break;
}
}
if (NULL != pCurKeyList)
{
std::pair<std::string, std::string> pr;
if (-1 != pos)
{
pr.first.assign(lineContent, pos);
pr.second.assign(lineContent + pos + 1);
}
else
{
pr.first = lineContent;
}
pCurKeyList->push_back(pr);
}
}
}
fclose(fp);
}
bool bFindSect = false;
for (size_t i = 0; i < sectList.size(); ++i)
{
if (strcmp(sectList[i].first.c_str(), section) == 0)
{
bool bFindKey = false;
for (size_t j = 0; j < sectList[i].second.size(); ++j)
{
if (strcmp(sectList[i].second[j].first.c_str(), key) == 0)
{
sectList[i].second[j].second = val;
bFindKey = true;
break;
}
}
if (!bFindKey)
{
std::pair<std::string, std::string> pr;
pr.first = key;
pr.second = val;
sectList[i].second.push_back(pr);
}
bFindSect = true;
break;
}
}
if (!bFindSect)
{
std::pair<std::string, KeyList> pr;
pr.first = section;
std::pair<std::string, std::string> pr2;
pr2.first = key;
pr2.second = val;
pr.second.push_back(pr2);
sectList.push_back(pr);
}
fp = fopen(file, "w");
if (fp == NULL)
{
return -1;
}
for (size_t i = 0; i < sectList.size(); ++i)
{
fputs(sectList[i].first.c_str(), fp);
fputs("\n", fp);
for (size_t j = 0; j < sectList[i].second.size(); ++j)
{
fputs(sectList[i].second[j].first.c_str(), fp);
fputs("=", fp);
fputs(sectList[i].second[j].second.c_str(), fp);
fputs("\n", fp);
}
}
fclose(fp);
return 0;
}
static int writeStringValue(const char* section, const char* key, const char* val, const char* file)
{
if (section == NULL || key == NULL || val == NULL || file == NULL)
{
return -1;
}
char sect[256];
sprintf(sect, "[%s]", section);
return IniWriteValue(sect, key, val, file);
}
HGResult HGAPI HGBase_SetProfileInt(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, HGInt value)
{
if (NULL == fileName || NULL == appName || NULL == keyName)
{
return HGBASE_ERR_INVALIDARG;
}
char str[8];
sprintf(str, "%d", value);
if (0 != writeStringValue(appName, keyName, str, fileName))
return HGBASE_ERR_FAIL;
return HGBASE_ERR_OK;
}
HGResult HGAPI HGBase_SetProfileString(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, const HGChar* value)
{
if (NULL == fileName || NULL == appName || NULL == keyName)
{
return HGBASE_ERR_INVALIDARG;
}
if (0 != writeStringValue(appName, keyName, value, fileName))
return HGBASE_ERR_FAIL;
return HGBASE_ERR_OK;
}
static int IniReadValue(const char* section, const char* key, char* val, const char* def, const char* file)
{
typedef std::vector<std::pair<std::string, std::string> > KeyList;
typedef std::vector<std::pair<std::string, KeyList> > SectionList;
SectionList sectList;
FILE* fp = fopen(file, "r");
if (fp != NULL)
{
KeyList* pCurKeyList = NULL;
while (feof(fp) == 0)
{
char lineContent[256] = { 0 };
if (NULL == fgets(lineContent, 256, fp))
{
continue;
}
if ((lineContent[0] == ';') || (lineContent[0] == '\0') || (lineContent[0] == '\r') || (lineContent[0] == '\n'))
{
continue;
}
for (size_t i = 0; i < strlen(lineContent); ++i)
{
if (lineContent[i] == '\r' || lineContent[i] == '\n')
{
lineContent[i] = '\0';
break;
}
}
if (lineContent[0] == '[')
{
std::pair<std::string, KeyList> pr;
pr.first = lineContent;
sectList.push_back(pr);
pCurKeyList = &sectList[sectList.size() - 1].second;
}
else
{
int pos = -1;
for (int i = 0; i < (int)strlen(lineContent); ++i)
{
if (lineContent[i] == '=')
{
pos = i;
break;
}
}
if (NULL != pCurKeyList)
{
std::pair<std::string, std::string> pr;
if (-1 != pos)
{
pr.first.assign(lineContent, pos);
pr.second.assign(lineContent + pos + 1);
}
else
{
pr.first = lineContent;
}
pCurKeyList->push_back(pr);
}
}
}
fclose(fp);
}
else
{
strcpy(val, def);
return -1;
}
bool bGetVal = false;
for (size_t i = 0; i < sectList.size(); ++i)
{
if (strcmp(sectList[i].first.c_str(), section) == 0)
{
for (size_t j = 0; j < sectList[i].second.size(); ++j)
{
if (strcmp(sectList[i].second[j].first.c_str(), key) == 0)
{
strcpy(val, sectList[i].second[j].second.c_str());
bGetVal = true;
break;
}
}
break;
}
}
if (!bGetVal)
{
strcpy(val, def);
}
return 0;
}
static int readStringValue(const char* section, const char* key, char* val, const char* def, const char* file)
{
if (section == NULL || key == NULL || val == NULL || def == NULL || file == NULL)
{
return -1;
}
char sect[256];
sprintf(sect, "[%s]", section);
return IniReadValue(sect, key, val, def, file);
}
HGResult HGAPI HGBase_GetProfileInt(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, HGInt def, HGInt* value)
{
if (NULL == appName || NULL == keyName || NULL == value)
{
return HGBASE_ERR_INVALIDARG;
}
char strRet[256] = { 0 };
char strDef[32];
sprintf(strDef, "%d", def);
readStringValue(appName, keyName, strRet, strDef, fileName);
*value = atoi(strRet);
return HGBASE_ERR_OK;
}
HGResult HGAPI HGBase_GetProfileString(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, const HGChar* def, HGChar* value, HGUInt maxLen)
{
if (NULL == appName || NULL == keyName || NULL == value)
{
return HGBASE_ERR_INVALIDARG;
}
readStringValue(appName, keyName, value, def, fileName);
return HGBASE_ERR_OK;
}
HGResult HGAPI HGBase_StandardiseFileName(const HGChar* fileName, HGChar* result, HGUInt maxLen) HGResult HGAPI HGBase_StandardiseFileName(const HGChar* fileName, HGChar* result, HGUInt maxLen)
{ {
if (NULL == fileName || NULL == result || 0 == maxLen) if (NULL == fileName || NULL == result || 0 == maxLen)

View File

@ -40,7 +40,7 @@ HGEXPORT HGResult HGAPI HGBase_GetUuid(HGChar* uuid, HGUInt maxLen);
/* 获取临时文件名 /* 获取临时文件名
*/ */
HGEXPORT HGResult HGAPI HGBase_GetTmpFileName(HGChar* fileName, HGUInt maxLen); HGEXPORT HGResult HGAPI HGBase_GetTmpFileName(const HGChar *suffix, HGChar* fileName, HGUInt maxLen);
/* 获取配置文件路径 /* 获取配置文件路径
*/ */
@ -66,26 +66,6 @@ HGEXPORT HGResult HGAPI HGBase_GetFileName(const HGChar *filePath, HGChar* name,
*/ */
HGEXPORT HGResult HGAPI HGBase_GetFilePath(const HGChar* filePath, HGChar* path, HGUInt maxLen); HGEXPORT HGResult HGAPI HGBase_GetFilePath(const HGChar* filePath, HGChar* path, HGUInt maxLen);
/* 设置ini文件的值
*/
HGEXPORT HGResult HGAPI HGBase_SetProfileInt(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, HGInt value);
/* 设置ini文件的值
*/
HGEXPORT HGResult HGAPI HGBase_SetProfileString(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, const HGChar *value);
/* 获取ini文件的值
*/
HGEXPORT HGResult HGAPI HGBase_GetProfileInt(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, HGInt def, HGInt* value);
/* 获取ini文件的值
*/
HGEXPORT HGResult HGAPI HGBase_GetProfileString(const HGChar* fileName, const HGChar* appName,
const HGChar* keyName, const HGChar* def, HGChar* value, HGUInt maxLen);
/* 将文件名标准化 */ /* 将文件名标准化 */
HGEXPORT HGResult HGAPI HGBase_StandardiseFileName(const HGChar* fileName, HGChar *result, HGUInt maxLen); HGEXPORT HGResult HGAPI HGBase_StandardiseFileName(const HGChar* fileName, HGChar *result, HGUInt maxLen);

View File

@ -653,7 +653,7 @@ bool HGOfdImageWriterImpl::AddPublicResXml()
bool HGOfdImageWriterImpl::AddXmlFile(tinyxml2::XMLDocument& xmlDoc, const HGChar* name) bool HGOfdImageWriterImpl::AddXmlFile(tinyxml2::XMLDocument& xmlDoc, const HGChar* name)
{ {
HGChar tmpName[256]; HGChar tmpName[256];
HGBase_GetTmpFileName(tmpName, 256); HGBase_GetTmpFileName(NULL, tmpName, 256);
if (tinyxml2::XML_SUCCESS != xmlDoc.SaveFile(tmpName)) if (tinyxml2::XML_SUCCESS != xmlDoc.SaveFile(tmpName))
{ {
return false; return false;
@ -681,7 +681,7 @@ bool HGOfdImageWriterImpl::AddXmlFile(tinyxml2::XMLDocument& xmlDoc, const HGCha
bool HGOfdImageWriterImpl::AddJpegImageFile(HGImage image, const HGJpegSaveInfo* info, const HGChar* name) bool HGOfdImageWriterImpl::AddJpegImageFile(HGImage image, const HGJpegSaveInfo* info, const HGChar* name)
{ {
HGChar tmpName[256]; HGChar tmpName[256];
HGBase_GetTmpFileName(tmpName, 256); HGBase_GetTmpFileName(NULL, tmpName, 256);
if (HGBASE_ERR_OK != HGImgFmt_SaveJpegImage(image, info, tmpName)) if (HGBASE_ERR_OK != HGImgFmt_SaveJpegImage(image, info, tmpName))
{ {
return false; return false;

View File

@ -103,8 +103,7 @@ HGResult HGOCRHanvon::ImageOCR(HGImage image, class HGOCRRetImpl **ocrRet)
} }
HGChar tmpFileName[256]; HGChar tmpFileName[256];
HGBase_GetTmpFileName(tmpFileName, 256); HGBase_GetTmpFileName("bmp", tmpFileName, 256);
strcat(tmpFileName, ".bmp");
HGImgFmt_SaveBmpImage(image, NULL, tmpFileName); HGImgFmt_SaveBmpImage(image, NULL, tmpFileName);
char* rst = NULL; char* rst = NULL;
@ -154,8 +153,7 @@ HGResult HGOCRHanvon::ImageOCRToFile(HGImage image, HGUInt outType, const HGChar
} }
HGChar tmpFileName[256]; HGChar tmpFileName[256];
HGBase_GetTmpFileName(tmpFileName, 256); HGBase_GetTmpFileName("bmp", tmpFileName, 256);
strcat(tmpFileName, ".bmp");
HGImgFmt_SaveBmpImage(image, NULL, tmpFileName); HGImgFmt_SaveBmpImage(image, NULL, tmpFileName);
HGChar tmpDir[256]; HGChar tmpDir[256];
@ -179,8 +177,7 @@ HGResult HGOCRHanvon::ImageTextDirectOCR(HGImage image, HGUInt* direct)
} }
HGChar tmpFileName[256]; HGChar tmpFileName[256];
HGBase_GetTmpFileName(tmpFileName, 256); HGBase_GetTmpFileName("bmp", tmpFileName, 256);
strcat(tmpFileName, ".bmp");
HGImgFmt_SaveBmpImage(image, NULL, tmpFileName); HGImgFmt_SaveBmpImage(image, NULL, tmpFileName);
int dire = -1; int dire = -1;

View File

@ -2,6 +2,7 @@
#include "base/HGBuffer.h" #include "base/HGBuffer.h"
#include "base/HGBase64.h" #include "base/HGBase64.h"
#include "base/HGUtility.h" #include "base/HGUtility.h"
#include "base/HGIni.h"
#include "base/HGInfo.h" #include "base/HGInfo.h"
#include "imgfmt/HGJpeg.h" #include "imgfmt/HGJpeg.h"
#include "imgfmt/HGOfd.h" #include "imgfmt/HGOfd.h"
@ -144,8 +145,10 @@ namespace ver_1
HGBase_CreateLock(&m_lock); HGBase_CreateLock(&m_lock);
m_devName.clear(); m_devName.clear();
m_devHandle = NULL; m_devHandle = NULL;
m_scanEvent = NULL; m_saneEvent = NULL;
m_scanParam = NULL; m_saneParam = NULL;
m_saneImageCallback = NULL;
m_saneImageParam = NULL;
m_scanInsertImgName.clear(); m_scanInsertImgName.clear();
m_scanIsInsert = false; m_scanIsInsert = false;
@ -227,21 +230,39 @@ namespace ver_1
m_scanning = false; m_scanning = false;
} }
void ManagerV1::SetScanEvent(ScanEvent event, void* param) void ManagerV1::SetSaneEvent(SaneEvent event, void* param)
{ {
assert(NULL != event && NULL != param); assert(NULL != event && NULL != param);
HGBase_EnterLock(m_lock); HGBase_EnterLock(m_lock);
m_scanEvent = event; m_saneEvent = event;
m_scanParam = param; m_saneParam = param;
HGBase_LeaveLock(m_lock); HGBase_LeaveLock(m_lock);
} }
void ManagerV1::ResetScanEvent() void ManagerV1::SetSaneImageCallback(SaneImageCallback func, void* param)
{
assert(NULL != func && NULL != param);
HGBase_EnterLock(m_lock);
m_saneImageCallback = func;
m_saneImageParam = param;
HGBase_LeaveLock(m_lock);
}
void ManagerV1::ResetSaneEvent()
{ {
HGBase_EnterLock(m_lock); HGBase_EnterLock(m_lock);
m_scanEvent = NULL; m_saneEvent = NULL;
m_scanParam = NULL; m_saneParam = NULL;
HGBase_LeaveLock(m_lock);
}
void ManagerV1::ResetSaneImageCallback()
{
HGBase_EnterLock(m_lock);
m_saneImageCallback = NULL;
m_saneImageParam = NULL;
HGBase_LeaveLock(m_lock); HGBase_LeaveLock(m_lock);
} }
@ -675,7 +696,7 @@ namespace ver_1
} }
HGChar tmpFileName[512]; HGChar tmpFileName[512];
HGBase_GetTmpFileName(tmpFileName, 512); HGBase_GetTmpFileName(NULL, tmpFileName, 512);
ExportOfdFile(devId, isAuto, tmpFileName); ExportOfdFile(devId, isAuto, tmpFileName);
*data = GetBuffer(tmpFileName, size); *data = GetBuffer(tmpFileName, size);
@ -754,7 +775,7 @@ namespace ver_1
} }
HGChar tmpFileName[512]; HGChar tmpFileName[512];
HGBase_GetTmpFileName(tmpFileName, 512); HGBase_GetTmpFileName(NULL, tmpFileName, 512);
ExportPdfFile(devId, tmpFileName); ExportPdfFile(devId, tmpFileName);
*data = GetBuffer(tmpFileName, size); *data = GetBuffer(tmpFileName, size);
@ -833,7 +854,7 @@ namespace ver_1
} }
HGChar tmpFileName[512]; HGChar tmpFileName[512];
HGBase_GetTmpFileName(tmpFileName, 512); HGBase_GetTmpFileName(NULL, tmpFileName, 512);
ExportTiff(devId, tmpFileName); ExportTiff(devId, tmpFileName);
*data = GetBuffer(tmpFileName, size); *data = GetBuffer(tmpFileName, size);
@ -917,7 +938,7 @@ namespace ver_1
} }
HGChar tmpFileName[512]; HGChar tmpFileName[512];
HGBase_GetTmpFileName(tmpFileName, 512); HGBase_GetTmpFileName(NULL, tmpFileName, 512);
ExportZipFile(devId, tmpFileName); ExportZipFile(devId, tmpFileName);
*data = GetBuffer(tmpFileName, size); *data = GetBuffer(tmpFileName, size);
@ -936,7 +957,7 @@ namespace ver_1
GetCurDevId(devId); GetCurDevId(devId);
HGChar tmpFileName[512]; HGChar tmpFileName[512];
HGBase_GetTmpFileName(tmpFileName, 512); HGBase_GetTmpFileName(NULL, tmpFileName, 512);
if (0 == uploadParam.format) if (0 == uploadParam.format)
{ {
strcat(tmpFileName, ".ofd"); strcat(tmpFileName, ".ofd");
@ -1364,8 +1385,11 @@ namespace ver_1
{ {
HGChar docsPath[256]; HGChar docsPath[256];
HGBase_GetDocumentsPath(docsPath, 256); HGBase_GetDocumentsPath(docsPath, 256);
HGChar procName[256];
HGBase_GetProcessName(procName, 256);
HGChar imgPath[512]; HGChar imgPath[512];
sprintf(imgPath, "%s%s/", docsPath, Utf8ToStdString(devId).c_str()); sprintf(imgPath, "%s%s/%s/", docsPath, procName, Utf8ToStdString(devId).c_str());
HGChar stdImgPath[512]; HGChar stdImgPath[512];
HGBase_StandardiseFileName(imgPath, stdImgPath, 512); HGBase_StandardiseFileName(imgPath, stdImgPath, 512);
@ -1893,11 +1917,6 @@ namespace ver_1
SANE_Device* sane_dev = (SANE_Device*)data; SANE_Device* sane_dev = (SANE_Device*)data;
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "device arrive, name=%s", Utf8ToStdString(sane_dev->name).c_str()); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "device arrive, name=%s", Utf8ToStdString(sane_dev->name).c_str());
HGBase_EnterLock(p->m_lock);
if (NULL != p->m_scanEvent)
p->m_scanEvent(SCANEVENT_ARRIVE, (void*)sane_dev->name, (void*)0, p->m_scanParam);
HGBase_LeaveLock(p->m_lock);
OpenDevParam* openDevParam = new OpenDevParam; OpenDevParam* openDevParam = new OpenDevParam;
openDevParam->mgr = p; openDevParam->mgr = p;
openDevParam->devName = sane_dev->name; openDevParam->devName = sane_dev->name;
@ -1909,6 +1928,11 @@ namespace ver_1
{ {
delete openDevParam; delete openDevParam;
} }
HGBase_EnterLock(p->m_lock);
if (NULL != p->m_saneEvent)
p->m_saneEvent(SANEEVENT_ARRIVE, sane_dev->name, false, p->m_saneParam);
HGBase_LeaveLock(p->m_lock);
} }
break; break;
case SANE_EVENT_DEVICE_LEFT: case SANE_EVENT_DEVICE_LEFT:
@ -1916,11 +1940,6 @@ namespace ver_1
SANE_Device* sane_dev = (SANE_Device*)data; SANE_Device* sane_dev = (SANE_Device*)data;
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "device remove, name=%s", Utf8ToStdString(sane_dev->name).c_str()); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "device remove, name=%s", Utf8ToStdString(sane_dev->name).c_str());
HGBase_EnterLock(p->m_lock);
if (NULL != p->m_scanEvent)
p->m_scanEvent(SCANEVENT_REMOVE, (void*)sane_dev->name, (void*)0, p->m_scanParam);
HGBase_LeaveLock(p->m_lock);
CloseDevParam* closeDevParam = new CloseDevParam; CloseDevParam* closeDevParam = new CloseDevParam;
closeDevParam->mgr = p; closeDevParam->mgr = p;
closeDevParam->devName = sane_dev->name; closeDevParam->devName = sane_dev->name;
@ -1932,6 +1951,11 @@ namespace ver_1
{ {
delete closeDevParam; delete closeDevParam;
} }
HGBase_EnterLock(p->m_lock);
if (NULL != p->m_saneEvent)
p->m_saneEvent(SANEEVENT_REMOVE, sane_dev->name, false, p->m_saneParam);
HGBase_LeaveLock(p->m_lock);
} }
break; break;
case SANE_EVENT_STATUS: case SANE_EVENT_STATUS:
@ -1939,8 +1963,8 @@ namespace ver_1
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_STATUS, msg=%s", Utf8ToStdString((char*)data).c_str()); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_STATUS, msg=%s", Utf8ToStdString((char*)data).c_str());
HGBase_EnterLock(p->m_lock); HGBase_EnterLock(p->m_lock);
if (NULL != p->m_scanEvent) if (NULL != p->m_saneEvent)
p->m_scanEvent(SCANEVENT_STATUS, (void*)data, (void*)0, p->m_scanParam); p->m_saneEvent(SANEEVENT_STATUS, (const char*)data, false, p->m_saneParam);
HGBase_LeaveLock(p->m_lock); HGBase_LeaveLock(p->m_lock);
} }
break; break;
@ -1949,8 +1973,8 @@ namespace ver_1
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_ERROR, msg=%s", Utf8ToStdString((char*)data).c_str()); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_ERROR, msg=%s", Utf8ToStdString((char*)data).c_str());
HGBase_EnterLock(p->m_lock); HGBase_EnterLock(p->m_lock);
if (NULL != p->m_scanEvent) if (NULL != p->m_saneEvent)
p->m_scanEvent(SCANEVENT_ERROR, (void*)data, (void*)0, p->m_scanParam); p->m_saneEvent(SANEEVENT_ERROR, (const char*)data, (0 != *len), p->m_saneParam);
HGBase_LeaveLock(p->m_lock); HGBase_LeaveLock(p->m_lock);
} }
break; break;
@ -1959,8 +1983,8 @@ namespace ver_1
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_WORKING, msg=%s", Utf8ToStdString((char*)data).c_str()); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_WORKING, msg=%s", Utf8ToStdString((char*)data).c_str());
HGBase_EnterLock(p->m_lock); HGBase_EnterLock(p->m_lock);
if (NULL != p->m_scanEvent) if (NULL != p->m_saneEvent)
p->m_scanEvent(SCANEVENT_WORKING, (void*)data, (void*)0, p->m_scanParam); p->m_saneEvent(SANEEVENT_WORKING, (const char*)data, false, p->m_saneParam);
HGBase_LeaveLock(p->m_lock); HGBase_LeaveLock(p->m_lock);
} }
break; break;
@ -2046,8 +2070,8 @@ namespace ver_1
imgBase64 += GetBase64(img); imgBase64 += GetBase64(img);
HGBase_EnterLock(p->m_lock); HGBase_EnterLock(p->m_lock);
if (NULL != p->m_scanEvent) if (NULL != p->m_saneImageCallback)
p->m_scanEvent(SCANEVENT_IMAGE, (void*)imgName.c_str(), (void*)imgBase64.c_str(), p->m_scanParam); p->m_saneImageCallback(imgName.c_str(), imgBase64.c_str(), p->m_saneImageParam);
HGBase_LeaveLock(p->m_lock); HGBase_LeaveLock(p->m_lock);
HGBase_DestroyImage(img); HGBase_DestroyImage(img);
@ -2058,11 +2082,6 @@ namespace ver_1
{ {
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_SCAN_FINISHED, msg=%s", Utf8ToStdString((char*)data).c_str()); HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_SCAN_FINISHED, msg=%s", Utf8ToStdString((char*)data).c_str());
HGBase_EnterLock(p->m_lock);
if (NULL != p->m_scanEvent)
p->m_scanEvent(SCANEVENT_FINISH, (void*)data, (void*)0, p->m_scanParam);
HGBase_LeaveLock(p->m_lock);
ScanFinishParam* scanFinishParam = new ScanFinishParam; ScanFinishParam* scanFinishParam = new ScanFinishParam;
scanFinishParam->mgr = p; scanFinishParam->mgr = p;
@ -2073,6 +2092,11 @@ namespace ver_1
{ {
delete scanFinishParam; delete scanFinishParam;
} }
HGBase_EnterLock(p->m_lock);
if (NULL != p->m_saneEvent)
p->m_saneEvent(SANEEVENT_FINISH, (const char*)data, (0 != *len), p->m_saneParam);
HGBase_LeaveLock(p->m_lock);
} }
break; break;
} }

View File

@ -16,13 +16,12 @@ namespace ver_1
{ {
enum enum
{ {
SCANEVENT_ARRIVE = 1L, SANEEVENT_ARRIVE = 1L,
SCANEVENT_REMOVE, SANEEVENT_REMOVE,
SCANEVENT_STATUS, SANEEVENT_STATUS,
SCANEVENT_WORKING, SANEEVENT_WORKING,
SCANEVENT_IMAGE, SANEEVENT_FINISH,
SCANEVENT_FINISH, SANEEVENT_ERROR
SCANEVENT_ERROR
}; };
struct DevParam struct DevParam
@ -87,7 +86,8 @@ namespace ver_1
int format; // 上传格式 0: ofd 1: pdf 2: zip, 默认2 int format; // 上传格式 0: ofd 1: pdf 2: zip, 默认2
}; };
typedef void (*ScanEvent)(HGUInt event, void* value1, void* value2, void* param); typedef void (*SaneEvent)(int code, const char* str, bool err, void* param);
typedef void (*SaneImageCallback)(const char* name, const char *base64, void* param);
class ManagerV1 : public Manager class ManagerV1 : public Manager
{ {
@ -103,9 +103,11 @@ namespace ver_1
void ScanFinish(const ScanFinishParam* param); void ScanFinish(const ScanFinishParam* param);
// 设置回调 // 设置回调
void SetScanEvent(ScanEvent event, void* param); void SetSaneEvent(SaneEvent event, void* param);
void SetSaneImageCallback(SaneImageCallback func, void* param);
// 清理回调 // 清理回调
void ResetScanEvent(); void ResetSaneEvent();
void ResetSaneImageCallback();
// 扫描 // 扫描
bool Scan(const std::string& insertImgName, bool isInsert); bool Scan(const std::string& insertImgName, bool isInsert);
// 停止扫描 // 停止扫描
@ -185,8 +187,10 @@ namespace ver_1
std::string m_devName; std::string m_devName;
SANE_Handle m_devHandle; SANE_Handle m_devHandle;
DevParam m_devParam; DevParam m_devParam;
ScanEvent m_scanEvent; SaneEvent m_saneEvent;
void* m_scanParam; void* m_saneParam;
SaneImageCallback m_saneImageCallback;
void* m_saneImageParam;
std::string m_scanInsertImgName; std::string m_scanInsertImgName;
bool m_scanIsInsert; bool m_scanIsInsert;

View File

@ -2,7 +2,9 @@
#include "base/HGBuffer.h" #include "base/HGBuffer.h"
#include "base/HGBase64.h" #include "base/HGBase64.h"
#include "base/HGUtility.h" #include "base/HGUtility.h"
#include "base/HGIni.h"
#include "base/HGInfo.h" #include "base/HGInfo.h"
#include "base/HGTime.h"
#include "imgfmt/HGJpeg.h" #include "imgfmt/HGJpeg.h"
#include "imgfmt/HGOfd.h" #include "imgfmt/HGOfd.h"
#include "imgfmt/HGPdf.h" #include "imgfmt/HGPdf.h"
@ -15,21 +17,107 @@ namespace ver_2
ManagerV2::ManagerV2(HGMsgPump msgPump) ManagerV2::ManagerV2(HGMsgPump msgPump)
: Manager(msgPump) : Manager(msgPump)
{ {
HGBase_CreateLock(&m_lock);
HGChar docsPath[256];
HGBase_GetDocumentsPath(docsPath, 256);
HGChar procName[256];
HGBase_GetProcessName(procName, 256);
HGChar defSavePath[256];
sprintf(defSavePath, "%s%s/", docsPath, procName);
m_globalCfg.fileSavePath = GetCfgStringValue("global", "fileSavePath", defSavePath);
m_globalCfg.fileNamePrefix = GetCfgStringValue("global", "fileNamePrefix", "Huago");
m_globalCfg.fileNameMode = GetCfgStringValue("global", "fileNameMode", "date_time");
m_globalCfg.imageFormat = GetCfgStringValue("global", "imageFormat", "jpg");
m_globalCfg.imageJpegQuality = GetCfgIntValue("global", "imageJpegQuality", 80);
m_globalCfg.imageTiffCompression = GetCfgStringValue("global", "imageTiffCompression", "lzw");
m_globalCfg.imageTiffJpegQuality = GetCfgIntValue("global", "imageTiffJpegQuality", 80);
m_globalCfg.uploadHttpHost = GetCfgStringValue("global", "uploadHttpHost", "");
m_globalCfg.uploadHttpPort = GetCfgIntValue("global", "uploadHttpPort", 80);
m_globalCfg.uploadHttpPath = GetCfgStringValue("global", "uploadHttpPath", "/upload.cgi");
m_globalCfg.uploadFtpUser = GetCfgStringValue("global", "uploadFtpUser", "");
m_globalCfg.uploadFtpPassword = GetCfgStringValue("global", "uploadFtpPassword", "");
m_globalCfg.uploadFtpHost = GetCfgStringValue("global", "uploadFtpHost", "");
m_globalCfg.uploadFtpPort = GetCfgIntValue("global", "uploadFtpPort", 21);
m_globalCfg.fileSavePath.push_back('/');
HGChar stdSavePath[256];
HGBase_StandardiseFileName(m_globalCfg.fileSavePath.c_str(), stdSavePath, 256);
m_globalCfg.fileSavePath = stdSavePath;
// 读取m_saveFilePathList
m_saveFilePathList.clear();
m_saneEvent = NULL;
m_saneParam = NULL;
m_saneImageCallback = NULL;
m_saneImageParam = NULL;
m_initDevice = false;
m_devNameList.clear();
m_openDevice = false;
m_devName.clear();
m_devHandle = NULL;
m_devParam.Reset();
m_scanning = false;
} }
ManagerV2::~ManagerV2() ManagerV2::~ManagerV2()
{ {
DeinitDevice();
HGBase_DestroyLock(m_lock);
m_lock = NULL;
}
void ManagerV2::SetSaneEvent(SaneEvent event, void* param)
{
assert(NULL != event && NULL != param);
HGBase_EnterLock(m_lock);
m_saneEvent = event;
m_saneParam = param;
HGBase_LeaveLock(m_lock);
}
void ManagerV2::SetSaneImageCallback(SaneImageCallback func, void* param)
{
assert(NULL != func && NULL != param);
HGBase_EnterLock(m_lock);
m_saneImageCallback = func;
m_saneImageParam = param;
HGBase_LeaveLock(m_lock);
}
void ManagerV2::ResetSaneEvent()
{
HGBase_EnterLock(m_lock);
m_saneEvent = NULL;
m_saneParam = NULL;
HGBase_LeaveLock(m_lock);
}
void ManagerV2::ResetSaneImageCallback()
{
HGBase_EnterLock(m_lock);
m_saneImageCallback = NULL;
m_saneImageParam = NULL;
HGBase_LeaveLock(m_lock);
} }
void ManagerV2::ScanFinish(const ScanFinishParam* param) void ManagerV2::ScanFinish(const ScanFinishParam* param)
{ {
assert(NULL != param && this == param->mgr); assert(NULL != param && this == param->mgr);
m_scanning = false;
} }
int ManagerV2::SetGlobalConfig(const GlobalConfig& cfg) int ManagerV2::SetGlobalConfig(const GlobalConfig& cfg)
{ {
if (m_scanning)
return -1;
if ("date_time" != cfg.fileNameMode && "random" != cfg.fileNameMode) if ("date_time" != cfg.fileNameMode && "random" != cfg.fileNameMode)
return -1; return -1;
if ("jpg" != cfg.imageFormat && "bmp" != cfg.imageFormat && "png" != cfg.imageFormat && "tif" != cfg.imageFormat if ("jpg" != cfg.imageFormat && "bmp" != cfg.imageFormat && "png" != cfg.imageFormat && "tif" != cfg.imageFormat
@ -37,46 +125,38 @@ namespace ver_2
return -1; return -1;
if (cfg.imageJpegQuality < 0 || cfg.imageJpegQuality > 100) if (cfg.imageJpegQuality < 0 || cfg.imageJpegQuality > 100)
return -1; return -1;
if ("none" != cfg.imageTiffCompression && "lzw" != cfg.imageTiffCompression && "jpeg" != cfg.imageTiffCompression) if ("none" != cfg.imageTiffCompression && "lzw" != cfg.imageTiffCompression
&& "jpeg" != cfg.imageTiffCompression && "ccitt-g4" != cfg.imageTiffCompression)
return -1; return -1;
if (cfg.imageTiffJpegQuality < 0 || cfg.imageTiffJpegQuality > 100) if (cfg.imageTiffJpegQuality < 0 || cfg.imageTiffJpegQuality > 100)
return -1; return -1;
SetCfgStringValue("global", "fileSavePath", Utf8ToStdString(cfg.fileSavePath)); m_globalCfg = cfg;
SetCfgStringValue("global", "fileNamePrefix", Utf8ToStdString(cfg.fileNamePrefix)); m_globalCfg.fileSavePath.push_back('/');
SetCfgStringValue("global", "fileNameMode", cfg.fileNameMode); HGChar stdSavePath[256];
SetCfgStringValue("global", "imageFormat", cfg.imageFormat); HGBase_StandardiseFileName(m_globalCfg.fileSavePath.c_str(), stdSavePath, 256);
SetCfgIntValue("global", "imageJpegQuality", cfg.imageJpegQuality); m_globalCfg.fileSavePath = stdSavePath;
SetCfgStringValue("global", "imageTiffCompression", cfg.imageTiffCompression);
SetCfgIntValue("global", "imageTiffJpegQuality", cfg.imageTiffJpegQuality); SetCfgStringValue("global", "fileSavePath", m_globalCfg.fileSavePath);
SetCfgStringValue("global", "uploadHttpHost", cfg.uploadHttpHost); SetCfgStringValue("global", "fileNamePrefix", m_globalCfg.fileNamePrefix);
SetCfgIntValue("global", "uploadHttpPort", cfg.uploadHttpPort); SetCfgStringValue("global", "fileNameMode", m_globalCfg.fileNameMode);
SetCfgStringValue("global", "uploadHttpPath", cfg.uploadHttpPath); SetCfgStringValue("global", "imageFormat", m_globalCfg.imageFormat);
SetCfgStringValue("global", "uploadFtpUser", cfg.uploadFtpUser); SetCfgIntValue("global", "imageJpegQuality", m_globalCfg.imageJpegQuality);
SetCfgStringValue("global", "uploadFtpPassword", cfg.uploadFtpPassword); SetCfgStringValue("global", "imageTiffCompression", m_globalCfg.imageTiffCompression);
SetCfgStringValue("global", "uploadFtpHost", cfg.uploadFtpHost); SetCfgIntValue("global", "imageTiffJpegQuality", m_globalCfg.imageTiffJpegQuality);
SetCfgIntValue("global", "uploadFtpPort", cfg.uploadFtpPort); SetCfgStringValue("global", "uploadHttpHost", m_globalCfg.uploadHttpHost);
SetCfgIntValue("global", "uploadHttpPort", m_globalCfg.uploadHttpPort);
SetCfgStringValue("global", "uploadHttpPath", m_globalCfg.uploadHttpPath);
SetCfgStringValue("global", "uploadFtpUser", m_globalCfg.uploadFtpUser);
SetCfgStringValue("global", "uploadFtpPassword", m_globalCfg.uploadFtpPassword);
SetCfgStringValue("global", "uploadFtpHost", m_globalCfg.uploadFtpHost);
SetCfgIntValue("global", "uploadFtpPort", m_globalCfg.uploadFtpPort);
return 0; return 0;
} }
int ManagerV2::GetGlobalConfig(GlobalConfig& cfg) int ManagerV2::GetGlobalConfig(GlobalConfig& cfg)
{ {
HGChar docPath[256]; cfg = m_globalCfg;
HGBase_GetDocumentsPath(docPath, 256);
cfg.fileSavePath = StdStringToUtf8(GetCfgStringValue("global", "fileSavePath", docPath));
cfg.fileNamePrefix = StdStringToUtf8(GetCfgStringValue("global", "fileNamePrefix", "Huago"));
cfg.fileNameMode = GetCfgStringValue("global", "fileNameMode", "date_time");
cfg.imageFormat = GetCfgStringValue("global", "imageFormat", "jpg");
cfg.imageJpegQuality = GetCfgIntValue("global", "imageJpegQuality", 80);
cfg.imageTiffCompression = GetCfgStringValue("global", "imageTiffCompression", "lzw");
cfg.imageTiffJpegQuality = GetCfgIntValue("global", "imageTiffJpegQuality", 80);
cfg.uploadHttpHost = GetCfgStringValue("global", "uploadHttpHost", "");
cfg.uploadHttpPort = GetCfgIntValue("global", "uploadHttpPort", 80);
cfg.uploadHttpPath = GetCfgStringValue("global", "uploadHttpPath", "/upload.cgi");
cfg.uploadFtpUser = GetCfgStringValue("global", "uploadFtpUser", "");
cfg.uploadFtpPassword = GetCfgStringValue("global", "uploadFtpPassword", "");
cfg.uploadFtpHost = GetCfgStringValue("global", "uploadFtpHost", "");
cfg.uploadFtpPort = GetCfgIntValue("global", "uploadFtpPort", 21);
return 0; return 0;
} }
@ -85,11 +165,11 @@ namespace ver_2
imgBase64.clear(); imgBase64.clear();
HGUInt imgType = 0; HGUInt imgType = 0;
HGImgFmt_GetImgFmtType(Utf8ToStdString(imagePath).c_str(), &imgType); HGImgFmt_GetImgFmtType(imagePath.c_str(), &imgType);
if (0 == imgType) if (0 == imgType)
return -1; return -1;
int ret = LoadBase64(Utf8ToStdString(imagePath), imgBase64); int ret = LoadBase64(imagePath, imgBase64);
if (0 != ret) if (0 != ret)
return ret; return ret;
@ -135,17 +215,55 @@ namespace ver_2
if (0 != ret) if (0 != ret)
return ret; return ret;
imagePath = StdStringToUtf8(imagePath2); imagePath = imagePath2;
return 0; return 0;
} }
int ManagerV2::DeleteLocalFile(const std::string& filePath) int ManagerV2::DeleteLocalFile(const std::string& filePath)
{ {
return 0; int ret = -1;
for (int i = 0; i < (int)m_saveFilePathList.size(); ++i)
{
if (filePath == m_saveFilePathList[i])
{
if (HGBASE_ERR_OK == HGBase_DeleteFile(filePath.c_str()))
{
m_saveFilePathList.erase(m_saveFilePathList.begin() + i);
ret = 0;
}
break;
}
}
return ret;
} }
int ManagerV2::ClearGlobalFileSavePath() int ManagerV2::ClearGlobalFileSavePath()
{ {
int i = 0;
while (i < (int)m_saveFilePathList.size())
{
HGChar path[256];
HGBase_GetFilePath(m_saveFilePathList[i].c_str(), path, 256);
if (0 == strcmp(path, m_globalCfg.fileSavePath.c_str()))
{
if (HGBASE_ERR_OK == HGBase_DeleteFile(m_saveFilePathList[i].c_str()))
{
m_saveFilePathList.erase(m_saveFilePathList.begin() + i);
}
else
{
++i;
}
}
else
{
++i;
}
}
HGBase_DeleteDir(m_globalCfg.fileSavePath.c_str());
return 0; return 0;
} }
@ -182,6 +300,132 @@ namespace ver_2
return 0; return 0;
} }
int ManagerV2::InitDevice()
{
if (m_initDevice)
return -1;
SANE_Int version_code = 0;
SANE_Status status = sane_init_ex(&version_code, sane_ex_callback, this);
assert(SANE_STATUS_GOOD == status);
m_initDevice = true;
return 0;
}
int ManagerV2::DeinitDevice()
{
if (m_initDevice)
{
CloseDevice();
sane_exit();
m_devNameList.clear();
m_initDevice = false;
}
return 0;
}
int ManagerV2::GetDeviceNameList(std::vector<std::string>& deviceNameList)
{
deviceNameList.clear();
if (!m_initDevice)
return -1;
HGBase_EnterLock(m_lock);
deviceNameList = m_devNameList;
HGBase_LeaveLock(m_lock);
return 0;
}
int ManagerV2::OpenDevice(const std::string& deviceName)
{
if (!m_initDevice || m_openDevice)
return -1;
std::string devName = deviceName;
HGBase_EnterLock(m_lock);
if (devName.empty() && m_devNameList.size() > 0)
devName = m_devNameList[0];
HGBase_LeaveLock(m_lock);
SANE_Handle dev = NULL;
SANE_Status status = sane_open(devName.c_str(), &dev);
if (SANE_STATUS_GOOD != status)
return -1;
m_devHandle = dev;
m_devName = devName;
// 加载m_devParam并设置到设备
m_openDevice = true;
return 0;
}
int ManagerV2::CloseDevice()
{
if (m_openDevice)
{
StopScan();
sane_close(m_devHandle);
m_devHandle = NULL;
m_devName.clear();
m_devParam.Reset();
m_openDevice = false;
}
return 0;
}
int ManagerV2::SetDeviceParam(const DeviceParam& param)
{
if (!m_openDevice || m_scanning)
return -1;
m_devParam = param;
// 设置到设备
return 0;
}
int ManagerV2::GetDeviceParam(DeviceParam& param)
{
param.Reset();
if (!m_openDevice)
return -1;
param = m_devParam;
return 0;
}
int ManagerV2::GetCurrDeviceName(std::string& deviceName)
{
deviceName = m_devName;
return 0;
}
int ManagerV2::StartScan()
{
if (!m_openDevice || m_scanning)
return -1;
SANE_Status status = sane_start(m_devHandle);
if (SANE_STATUS_GOOD != status)
return -1;
m_scanning = true;
return 0;
}
int ManagerV2::StopScan()
{
if (m_scanning)
{
sane_cancel(m_devHandle);
}
return 0;
}
std::string ManagerV2::GetCfgStringValue(const std::string& app, const std::string& key, const std::string& def) std::string ManagerV2::GetCfgStringValue(const std::string& app, const std::string& key, const std::string& def)
{ {
HGChar cfgPath[256]; HGChar cfgPath[256];
@ -312,26 +556,19 @@ namespace ver_2
std::string ManagerV2::GetFilePath(const std::string& suffix) std::string ManagerV2::GetFilePath(const std::string& suffix)
{ {
HGChar docPath[256];
HGBase_GetDocumentsPath(docPath, 256);
std::string fileSavePath = GetCfgStringValue("global", "fileSavePath", docPath);
std::string fileNamePrefix = GetCfgStringValue("global", "fileNamePrefix", "Huago");
std::string fileNameMode = GetCfgStringValue("global", "fileNameMode", "date_time");
char filePath[256] = { 0 }; char filePath[256] = { 0 };
if ("random" == fileNameMode) if ("random" == m_globalCfg.fileNameMode)
{ {
HGChar uuid[256]; HGChar uuid[256];
HGBase_GetUuid(uuid, 256); HGBase_GetUuid(uuid, 256);
sprintf(filePath, "%s%s%s.%s", fileSavePath.c_str(), fileNamePrefix.c_str(), uuid, suffix.c_str()); sprintf(filePath, "%s%s%s.%s", m_globalCfg.fileSavePath.c_str(), m_globalCfg.fileNamePrefix.c_str(), uuid, suffix.c_str());
} }
else else
{ {
timeb tb; HGTimeInfo timeInfo;
ftime(&tb); HGBase_GetLocalTime(&timeInfo);
struct tm* p = localtime(&tb.time); sprintf(filePath, "%s%s%04d%02d%02d%02d%02d%02d%03d.%s", m_globalCfg.fileSavePath.c_str(), m_globalCfg.fileNamePrefix.c_str(), timeInfo.year,
sprintf(filePath, "%s%s%04d%02d%02d%02d%02d%02d.%s", fileSavePath.c_str(), fileNamePrefix.c_str(), (1900 + p->tm_year), timeInfo.month, timeInfo.day, timeInfo.hour, timeInfo.minute, timeInfo.second, timeInfo.milliseconds, suffix.c_str());
(1 + p->tm_mon), p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec, suffix.c_str());
} }
return filePath; return filePath;
@ -339,7 +576,150 @@ namespace ver_2
std::string ManagerV2::GetImagePath() std::string ManagerV2::GetImagePath()
{ {
std::string imageFormat = GetCfgStringValue("global", "imageFormat", "jpg"); return GetFilePath(m_globalCfg.imageFormat);
return GetFilePath(imageFormat); }
int ManagerV2::sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param)
{
(void)hdev;
(void)len;
ManagerV2* p = (ManagerV2*)param;
switch (code)
{
case SANE_EVENT_DEVICE_ARRIVED:
{
SANE_Device* sane_dev = (SANE_Device*)data;
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_DEVICE_ARRIVED, name=%s", Utf8ToStdString(sane_dev->name).c_str());
HGBase_EnterLock(p->m_lock);
p->m_devNameList.push_back(sane_dev->name);
if (NULL != p->m_saneEvent)
p->m_saneEvent(SANEEVENT_ARRIVE, sane_dev->name, false, p->m_saneParam);
HGBase_LeaveLock(p->m_lock);
}
break;
case SANE_EVENT_DEVICE_LEFT:
{
SANE_Device* sane_dev = (SANE_Device*)data;
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_DEVICE_LEFT, name=%s", Utf8ToStdString(sane_dev->name).c_str());
HGBase_EnterLock(p->m_lock);
for (int i = 0; i < (int)p->m_devNameList.size(); ++i)
{
if (0 == strcmp(sane_dev->name, p->m_devNameList[i].c_str()))
{
p->m_devNameList.erase(p->m_devNameList.begin() + i);
break;
}
}
if (NULL != p->m_saneEvent)
p->m_saneEvent(SANEEVENT_REMOVE, sane_dev->name, false, p->m_saneParam);
HGBase_LeaveLock(p->m_lock);
}
break;
case SANE_EVENT_WORKING:
{
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_WORKING, msg=%s", Utf8ToStdString((char*)data).c_str());
HGBase_EnterLock(p->m_lock);
if (NULL != p->m_saneEvent)
p->m_saneEvent(SANEEVENT_WORKING, (const char*)data, false, p->m_saneParam);
HGBase_LeaveLock(p->m_lock);
}
break;
case SANE_EVENT_SCAN_FINISHED:
{
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_SCAN_FINISHED, msg=%s", Utf8ToStdString((char*)data).c_str());
ScanFinishParam* scanFinishParam = new ScanFinishParam;
scanFinishParam->mgr = p;
HGMsg msg;
msg.id = MSGID_SCAN_FINISH;
msg.data = scanFinishParam;
if (HGBASE_ERR_OK != HGBase_PostPumpMessage(p->m_msgPump, &msg))
{
delete scanFinishParam;
}
HGBase_EnterLock(p->m_lock);
if (NULL != p->m_saneEvent)
p->m_saneEvent(SANEEVENT_FINISH, (const char*)data, (0 != *len), p->m_saneParam);
HGBase_LeaveLock(p->m_lock);
}
break;
case SANE_EVENT_STATUS:
{
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_STATUS, msg=%s", Utf8ToStdString((char*)data).c_str());
HGBase_EnterLock(p->m_lock);
if (NULL != p->m_saneEvent)
p->m_saneEvent(SANEEVENT_STATUS, (const char*)data, false, p->m_saneParam);
HGBase_LeaveLock(p->m_lock);
}
break;
case SANE_EVENT_ERROR:
{
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_ERROR, msg=%s", Utf8ToStdString((char*)data).c_str());
HGBase_EnterLock(p->m_lock);
if (NULL != p->m_saneEvent)
p->m_saneEvent(SANEEVENT_ERROR, (const char*)data, (0 != *len), p->m_saneParam);
HGBase_LeaveLock(p->m_lock);
}
break;
case SANE_EVENT_IMAGE_OK:
{
HGBase_WriteInfo(HGBASE_INFOTYPE_DESC, "SANE_EVENT_IMAGE_OK");
SANE_Image* sane_img = (SANE_Image*)data;
HGUInt imgType = 0;
if (sane_img->header.format == SANE_FRAME_GRAY)
{
if (1 == sane_img->header.depth)
imgType = HGBASE_IMGTYPE_BINARY;
else if (8 == sane_img->header.depth)
imgType = HGBASE_IMGTYPE_GRAY;
}
else if (sane_img->header.format == SANE_FRAME_RGB)
imgType = HGBASE_IMGTYPE_RGB;
HGByte* data = sane_img->data;
HGImageInfo imgInfo = { (HGUInt)sane_img->header.pixels_per_line, (HGUInt)sane_img->header.lines,
imgType, (HGUInt)sane_img->header.bytes_per_line, HGBASE_IMGORIGIN_TOP };
HGImage img = NULL;
HGBase_CreateImageFromData(data, &imgInfo, NULL, 0, HGBASE_IMGORIGIN_TOP, &img);
if (NULL != img)
{
std::string imagePath = p->GetImagePath();
HGImgFmtSaveInfo saveInfo;
saveInfo.jpegQuality = p->m_globalCfg.imageJpegQuality;
saveInfo.tiffJpegQuality = p->m_globalCfg.imageTiffJpegQuality;
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_LZW;
if ("none" == p->m_globalCfg.imageTiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_NONE;
else if ("jpeg" == p->m_globalCfg.imageTiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_JPEG;
else if ("ccitt-g4" == p->m_globalCfg.imageTiffCompression)
saveInfo.tiffCompression = HGIMGFMT_TIFFCOMP_CCITTFAX4;
if (HGBASE_ERR_OK == HGImgFmt_SaveImage(img, 0, &saveInfo, imagePath.c_str()))
{
HGBase_EnterLock(p->m_lock);
if (NULL != p->m_saneImageCallback)
p->m_saneImageCallback(imagePath.c_str(), p->m_saneImageParam);
HGBase_LeaveLock(p->m_lock);
}
HGBase_DestroyImage(img);
}
}
break;
}
return 0;
} }
} }

View File

@ -14,8 +14,26 @@
namespace ver_2 namespace ver_2
{ {
enum
{
SANEEVENT_ARRIVE = 1L,
SANEEVENT_REMOVE,
SANEEVENT_STATUS,
SANEEVENT_WORKING,
SANEEVENT_FINISH,
SANEEVENT_ERROR
};
struct GlobalConfig struct GlobalConfig
{ {
GlobalConfig()
{
imageJpegQuality = 0;
imageTiffJpegQuality = 0;
uploadHttpPort = 0;
uploadFtpPort = 0;
}
// 文件保存 // 文件保存
std::string fileSavePath; std::string fileSavePath;
std::string fileNamePrefix; std::string fileNamePrefix;
@ -35,12 +53,69 @@ namespace ver_2
unsigned short uploadFtpPort; unsigned short uploadFtpPort;
}; };
struct DeviceParam
{
DeviceParam()
{
Reset();
}
void Reset()
{
}
// 颜色模式
std::vector<std::string> colorModeList;
std::string colorMode;
// 扫描页面
std::vector<std::string> pageModeList;
std::string pageMode;
// 分辨率
std::vector<int> resolutionList;
int resolution;
// 亮度
int brightnessMin;
int brightnessMax;
int brightness;
// 对比度
int contrastMin;
int contrastMax;
int contrast;
// 伽马
int gammaMin;
int gammaMax;
int gamma;
// 纸张尺寸
std::vector<std::string> paperSizeList;
std::string pageSize;
int paperSizeWidth;
int paperSizeHeight;
// 手动裁剪
bool paperCutEnabled;
float paperCutLeft;
float paperCutTop;
float paperCutRight;
float paperCutBottom;
// 是否纠偏
bool autoCrop;
};
typedef void (*SaneEvent)(int code, const char *str, bool err, void* param);
typedef void (*SaneImageCallback)(const char* path, void* param);
class ManagerV2 : public Manager class ManagerV2 : public Manager
{ {
public: public:
ManagerV2(HGMsgPump msgPump); ManagerV2(HGMsgPump msgPump);
virtual ~ManagerV2(); virtual ~ManagerV2();
// 设置回调
void SetSaneEvent(SaneEvent event, void* param);
void SetSaneImageCallback(SaneImageCallback func, void* param);
// 清理回调
void ResetSaneEvent();
void ResetSaneImageCallback();
// 扫描完成 // 扫描完成
void ScanFinish(const ScanFinishParam* param); void ScanFinish(const ScanFinishParam* param);
@ -72,6 +147,27 @@ namespace ver_2
// 上传文件 // 上传文件
int UploadLocalFile(const std::string& filePath, const std::string& mode, const std::string& remoteFilePath); int UploadLocalFile(const std::string& filePath, const std::string& mode, const std::string& remoteFilePath);
// 设备初始化
int InitDevice();
// 设备反初始化
int DeinitDevice();
// 获取设备列表
int GetDeviceNameList(std::vector<std::string> &deviceNameList);
// 打开设备
int OpenDevice(const std::string& deviceName);
// 关闭设备
int CloseDevice();
// 设置设备参数
int SetDeviceParam(const DeviceParam& param);
// 获取设备参数
int GetDeviceParam(DeviceParam& param);
// 获取当前设备名
int GetCurrDeviceName(std::string& deviceName);
// 开始扫描
int StartScan();
// 停止扫描
int StopScan();
private: private:
static std::string GetCfgStringValue(const std::string& app, const std::string& key, const std::string& def); static std::string GetCfgStringValue(const std::string& app, const std::string& key, const std::string& def);
static int GetCfgIntValue(const std::string& app, const std::string& key, int def); static int GetCfgIntValue(const std::string& app, const std::string& key, int def);
@ -81,10 +177,25 @@ namespace ver_2
static bool SetCfgBoolValue(const std::string& app, const std::string& key, bool val); static bool SetCfgBoolValue(const std::string& app, const std::string& key, bool val);
static int LoadBase64(const std::string& fileName, std::string& base64); static int LoadBase64(const std::string& fileName, std::string& base64);
static int SaveBase64(const std::string& base64, const std::string& fileName); static int SaveBase64(const std::string& base64, const std::string& fileName);
static std::string GetFilePath(const std::string& suffix); std::string GetFilePath(const std::string& suffix);
static std::string GetImagePath(); std::string GetImagePath();
static int sane_ex_callback(SANE_Handle hdev, int code, void* data, unsigned int* len, void* param);
private: private:
HGLock m_lock;
GlobalConfig m_globalCfg;
std::vector<std::string> m_saveFilePathList;
SaneEvent m_saneEvent;
void* m_saneParam;
SaneImageCallback m_saneImageCallback;
void* m_saneImageParam;
bool m_initDevice;
std::vector<std::string> m_devNameList;
bool m_openDevice;
SANE_Handle m_devHandle;
std::string m_devName;
DeviceParam m_devParam;
bool m_scanning;
}; };
} }

View File

@ -16,12 +16,14 @@ namespace ver_1
#endif #endif
: WebUser(server, id, ip, port, sockConn) : WebUser(server, id, ip, port, sockConn)
{ {
GetManager()->SetScanEvent(ScanCallback, this); GetManager()->SetSaneEvent(SaneEvent2, this);
GetManager()->SetSaneImageCallback(SaneImageCallback2, this);
} }
SockIoUser::~SockIoUser() SockIoUser::~SockIoUser()
{ {
GetManager()->ResetScanEvent(); GetManager()->ResetSaneImageCallback();
GetManager()->ResetSaneEvent();
} }
ManagerV1* SockIoUser::GetManager() ManagerV1* SockIoUser::GetManager()
@ -425,42 +427,53 @@ namespace ver_1
} }
} }
void SockIoUser::ScanCallback(HGUInt event, void* value1, void* value2, void* param) void SockIoUser::SaneEvent2(int code, const char* str, bool err, void* param)
{ {
SockIoUser* p = (SockIoUser*)param; SockIoUser* p = (SockIoUser*)param;
char* resp = NULL; char* resp = NULL;
if (SCANEVENT_ARRIVE == event) if (SANEEVENT_ARRIVE == code)
{ {
resp = new char[256]; resp = new char[256];
sprintf(resp, "42[\"success\", \"%s\"]", (const char*)value1); sprintf(resp, "42[\"success\", \"%s\"]", str);
} }
else if (SCANEVENT_REMOVE == event) else if (SANEEVENT_REMOVE == code)
{ {
} }
else if (SCANEVENT_WORKING == event) else if (SANEEVENT_WORKING == code)
{ {
resp = new char[256]; resp = new char[256];
sprintf(resp, "42[\"event\", \"%s\"]", "......"); sprintf(resp, "42[\"event\", \"%s\"]", "......");
} }
else if (SCANEVENT_FINISH == event) else if (SANEEVENT_FINISH == code)
{ {
resp = new char[256]; resp = new char[256];
sprintf(resp, "42[\"result\", {\"code\":204, \"msg\":\"%s\"}]", (const char*)value1); sprintf(resp, "42[\"result\", {\"code\":204, \"msg\":\"%s\"}]", str);
} }
else if (SCANEVENT_ERROR == event) else if (SANEEVENT_ERROR == code)
{ {
resp = new char[256]; resp = new char[256];
sprintf(resp, "42[\"error\", \"%s\"]", (const char*)value1); sprintf(resp, "42[\"error\", \"%s\"]", str);
} }
else if (SCANEVENT_IMAGE == event)
if (NULL != resp)
{ {
const char* imgName = (const char*)value1; p->PostEventMsg((const HGByte*)resp, (HGUInt)strlen(resp));
const char* imgBase64 = (const char*)value2; delete[] resp;
}
}
void SockIoUser::SaneImageCallback2(const char* name, const char* base64, void* param)
{
SockIoUser* p = (SockIoUser*)param;
char* resp = NULL;
const char* imgName = (const char*)name;
const char* imgBase64 = (const char*)base64;
resp = new char[256 + strlen(imgName) + strlen(imgBase64)]; resp = new char[256 + strlen(imgName) + strlen(imgBase64)];
sprintf(resp, "42[\"image\", {\"code\":201, \"imageName\":\"%s\", \"image\":\"%s\"}]", imgName, imgBase64); sprintf(resp, "42[\"image\", {\"code\":201, \"imageName\":\"%s\", \"image\":\"%s\"}]", imgName, imgBase64);
}
if (NULL != resp) if (NULL != resp)
{ {

View File

@ -25,7 +25,8 @@ namespace ver_1
virtual void ThreadFunc(); virtual void ThreadFunc();
private: private:
static void ScanCallback(HGUInt event, void* value1, void* value2, void* param); static void SaneEvent2(int code, const char* str, bool err, void* param);
static void SaneImageCallback2(const char* name, const char *base64, void* param);
static void GetMsgInfo(const SockIoCmdParam* param, std::string& user, std::string& data); static void GetMsgInfo(const SockIoCmdParam* param, std::string& user, std::string& data);
bool ShakeHand(const std::string& head); bool ShakeHand(const std::string& head);
void Pong(); void Pong();

View File

@ -5,6 +5,7 @@
#include "cJSON.h" #include "cJSON.h"
#include "sha1.h" #include "sha1.h"
#include "base64.h" #include "base64.h"
#include "HGString.h"
namespace ver_2 namespace ver_2
{ {
@ -97,18 +98,244 @@ namespace ver_2
if (NULL != json) if (NULL != json)
{ {
std::string func = GetJsonStringValue(json, "func"); std::string func = GetJsonStringValue(json, "func");
std::string iden = GetJsonStringValue(json, "iden");
if ("set_global_config" == func) if ("set_global_config" == func)
{ {
GlobalConfig cfg;
cfg.fileSavePath = Utf8ToStdString(GetJsonStringValue(json, "file_save_path"));
cfg.fileNamePrefix = Utf8ToStdString(GetJsonStringValue(json, "file_name_prefix"));
cfg.fileNameMode = GetJsonStringValue(json, "file_name_mode");
cfg.imageFormat = GetJsonStringValue(json, "image_format");
cfg.imageJpegQuality = GetJsonIntValue(json, "image_jpeg_quality");
cfg.imageTiffCompression = GetJsonStringValue(json, "image_tiff_compression");
cfg.imageTiffJpegQuality = GetJsonIntValue(json, "image_tiff_jpeg_quality");
cfg.uploadHttpHost = GetJsonStringValue(json, "upload_http_host");
cfg.uploadHttpPort = GetJsonIntValue(json, "upload_http_port");
cfg.uploadHttpPath = GetJsonStringValue(json, "upload_http_path");
cfg.uploadFtpUser = GetJsonStringValue(json, "upload_ftp_user");
cfg.uploadFtpPassword = GetJsonStringValue(json, "upload_ftp_password");
cfg.uploadFtpHost = GetJsonStringValue(json, "upload_ftp_host");
cfg.uploadFtpPort = GetJsonIntValue(json, "upload_ftp_port");
int ret = GetManager()->SetGlobalConfig(cfg);
std::string errInfo;
if (0 != ret)
errInfo = StdStringToUtf8("错误");
std::string fmt;
fmt += "{\"func\":\"%s\", ";
fmt += "\"iden\":\"%s\", ";
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
char resp[1024];
sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str());
SendResponse((const HGByte *)resp, (HGUInt)strlen(resp), HGTRUE);
} }
else if ("get_global_config" == func) else if ("get_global_config" == func)
{ {
GlobalConfig cfg;
int ret = GetManager()->GetGlobalConfig(cfg);
std::string errInfo;
if (0 != ret)
errInfo = StdStringToUtf8("错误");
std::string fmt;
fmt += "{\"func\":\"%s\", ";
fmt += "\"iden\":\"%s\", ";
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\", ";
fmt += "\"file_save_path\":\"%s\", ";
fmt += "\"file_name_prefix\":\"%s\", ";
fmt += "\"file_name_mode\":\"%s\", ";
fmt += "\"image_format\":\"%s\", ";
fmt += "\"image_jpeg_quality\":%d, ";
fmt += "\"image_tiff_compression\":\"%s\", ";
fmt += "\"image_tiff_jpeg_quality\":%d, ";
fmt += "\"upload_http_host\":\"%s\", ";
fmt += "\"upload_http_port\":%d, ";
fmt += "\"upload_http_path\":\"%s\", ";
fmt += "\"upload_ftp_user\":\"%s\", ";
fmt += "\"upload_ftp_password\":\"%s\", ";
fmt += "\"upload_ftp_host\":\"%s\", ";
fmt += "\"upload_ftp_port\":%d}";
char resp[1024];
sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str(), StdStringToUtf8(strToJson(cfg.fileSavePath)).c_str(),
StdStringToUtf8(strToJson(cfg.fileNamePrefix)).c_str(), cfg.fileNameMode.c_str(), cfg.imageFormat.c_str(), cfg.imageJpegQuality,
cfg.imageTiffCompression.c_str(), cfg.imageTiffJpegQuality, cfg.uploadHttpHost.c_str(), cfg.uploadHttpPort,
strToJson(cfg.uploadHttpPath).c_str(), cfg.uploadFtpUser.c_str(), cfg.uploadFtpPassword.c_str(), cfg.uploadFtpHost.c_str(),
cfg.uploadFtpPort);
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
} }
else if ("load_local_image" == func) else if ("load_local_image" == func)
{ {
} }
else if ("init_device" == func)
{
int ret = GetManager()->InitDevice();
std::string errInfo;
if (0 != ret)
errInfo = StdStringToUtf8("错误");
std::string fmt;
fmt += "{\"func\":\"%s\", ";
fmt += "\"iden\":\"%s\", ";
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
char resp[1024];
sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str());
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
else if ("deinit_device" == func)
{
int ret = GetManager()->DeinitDevice();
std::string errInfo;
if (0 != ret)
errInfo = StdStringToUtf8("错误");
std::string fmt;
fmt += "{\"func\":\"%s\", ";
fmt += "\"iden\":\"%s\", ";
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
char resp[1024];
sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str());
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
else if ("get_device_name_list" == func)
{
std::vector<std::string> devNameList;
int ret = GetManager()->GetDeviceNameList(devNameList);
std::string errInfo;
if (0 != ret)
errInfo = StdStringToUtf8("错误");
std::string fmt;
fmt += "{\"func\":\"%s\", ";
fmt += "\"iden\":\"%s\", ";
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\", ";
fmt += "\"device_name_list\":%s}";
std::string nameList = "[";
for (int i = 0; i < (int)devNameList.size(); ++i)
{
nameList += "\"";
nameList += devNameList[i];
nameList += "\"";
if (i != (int)devNameList.size() - 1)
{
nameList += ", ";
}
}
nameList += "]";
char resp[1024];
sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str(), nameList.c_str());
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
else if ("open_device" == func)
{
std::string devName = GetJsonStringValue(json, "device_name");
int ret = GetManager()->OpenDevice(devName);
std::string errInfo;
if (0 != ret)
errInfo = StdStringToUtf8("错误");
std::string fmt;
fmt += "{\"func\":\"%s\", ";
fmt += "\"iden\":\"%s\", ";
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
char resp[1024];
sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str());
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
else if ("close_device" == func)
{
int ret = GetManager()->CloseDevice();
std::string errInfo;
if (0 != ret)
errInfo = StdStringToUtf8("错误");
std::string fmt;
fmt += "{\"func\":\"%s\", ";
fmt += "\"iden\":\"%s\", ";
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
char resp[1024];
sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str());
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
else if ("get_curr_device_name" == func)
{
std::string devName;
int ret = GetManager()->GetCurrDeviceName(devName);
std::string errInfo;
if (0 != ret)
errInfo = StdStringToUtf8("错误");
std::string fmt;
fmt += "{\"func\":\"%s\", ";
fmt += "\"iden\":\"%s\", ";
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\", ";
fmt += "\"device_name\":\"%s\"}";
char resp[1024];
sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str(), devName.c_str());
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
else if ("start_scan" == func)
{
int ret = GetManager()->StartScan();
std::string errInfo;
if (0 != ret)
errInfo = StdStringToUtf8("错误");
std::string fmt;
fmt += "{\"func\":\"%s\", ";
fmt += "\"iden\":\"%s\", ";
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
char resp[1024];
sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str());
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
else if ("stop_scan" == func)
{
int ret = GetManager()->StopScan();
std::string errInfo;
if (0 != ret)
errInfo = StdStringToUtf8("错误");
std::string fmt;
fmt += "{\"func\":\"%s\", ";
fmt += "\"iden\":\"%s\", ";
fmt += "\"ret\":%d, ";
fmt += "\"err_info\":\"%s\"}";
char resp[1024];
sprintf(resp, fmt.c_str(), func.c_str(), iden.c_str(), ret, errInfo.c_str());
SendResponse((const HGByte*)resp, (HGUInt)strlen(resp), HGTRUE);
}
cJSON_Delete(json); cJSON_Delete(json);
} }
@ -452,6 +679,46 @@ namespace ver_2
} }
} }
std::string WSUser::strToJson(const std::string str)
{
std::string ret;
for (size_t i = 0; i < str.size(); i++)
{
char c = str[i];
switch (c)
{
case '\"':
ret.append("\\\"");
break;
case '\\':
ret.append("\\\\");
break;
case '/':
ret.append("\\/");
break;
case '\b':
ret.append("\\b");
break;
case '\f':
ret.append("\\f");
break;
case '\n':
ret.append("\\n");
break;
case '\r':
ret.append("\\r");
break;
case '\t':
ret.append("\\t");
break;
default:
ret.push_back(c);
}
}
return ret;
}
bool WSUser::ShakeHand(const std::string& head) bool WSUser::ShakeHand(const std::string& head)
{ {
std::string requestMethod; std::string requestMethod;

View File

@ -25,6 +25,7 @@ namespace ver_2
virtual void ThreadFunc(); virtual void ThreadFunc();
private: private:
static std::string strToJson(const std::string str);
bool ShakeHand(const std::string& head); bool ShakeHand(const std::string& head);
void Pong(); void Pong();
bool SendResponse(const HGByte* data, HGUInt size, HGBool text); bool SendResponse(const HGByte* data, HGUInt size, HGBool text);

View File

@ -2,6 +2,7 @@
#include "base/HGInc.h" #include "base/HGInc.h"
#include "base/HGThread.h" #include "base/HGThread.h"
#include "base/HGUtility.h" #include "base/HGUtility.h"
#include "base/HGIni.h"
#include "base/HGMsgPump.h" #include "base/HGMsgPump.h"
#include "ManagerV1.h" #include "ManagerV1.h"
#include "ManagerV2.h" #include "ManagerV2.h"