📄 templateex.cpp
字号:
#include "stdafx.h"
#include "TemplateEx.h"
#include "Template.h"
#include <fstream>
//////////////////////////////////////////////////////////////////////////
//<-
//d : value
//->
//null
static int SetUtf8(lua_State *L)
{
int nValue = (int)lua_tointeger(L,-1);
//CTemplate::Instance()->SetSid(nValue);
return 0;
}
static int SetSid(lua_State *L)
{
int nValue = (int)lua_tointeger(L,-1);
CTemplate::Instance()->SetSid(nValue);
return 0;
}
//<-
//d : value
//->
//null
static int SetThread(lua_State *L)
{
int nValue = (int)lua_tointeger(L,-1);
CTemplate::Instance()->SetThread(nValue);
return 0;
}
//<-
//d : value
//->
//null
static int SetCTimeOut(lua_State *L)
{
int nValue = (int)lua_tointeger(L,-1);
CTemplate::Instance()->SetCTimeOut(nValue);
return 0;
}
//<-
//d : value
//->
//null
static int SetConnect(lua_State *L)
{
int nValue = (int)lua_tointeger(L,-1);
CTemplate::Instance()->SetConnect(nValue);
return 0;
}
//<-
//s : useragent
//->
//null
static int SetUserAgent(lua_State *L)
{
const char* strValue = lua_tostring(L,-1);
CTemplate::Instance()->SetUserAgent(strValue);
return 0;
}
static int SetDiscuzCookie(lua_State *L)
{
const char* strValue = lua_tostring(L,-1);
//CTemplate::Instance()->SetCookie(strValue);
return 0;
}
static int SetDvbbsCookie(lua_State *L)
{
return 0;
}
//<-
//s : seedpage
//->
//null
static int SetSeedPage(lua_State *L)
{
const char* strValue = lua_tostring(L,-1);
CTemplate::Instance()->SetSeedPage(strValue);
return 0;
}
//<-
//s : seedpage
//d : start
//d : end
//->
//null
static int SetSeedPattern(lua_State *L)
{
const char* strValue = lua_tostring(L,-3);
int nValue1 = (int)lua_tointeger(L,-2);
int nValue2 = (int)lua_tointeger(L,-1);
CTemplate::Instance()->SetSeedPattern(strValue,nValue1,nValue2);
return 0;
}
//<-
//s : connenttype
//->
//null
static int SetConnentType(lua_State *L)
{
const char* strValue = lua_tostring(L,-1);
CTemplate::Instance()->SetConnentType(strValue);
return 0;
}
//<-
//s : filter
//->
//null
static int SetFilterSkip(lua_State *L)
{
const char* strValue = lua_tostring(L,-1);
CTemplate::Instance()->SetFilter(strValue,FT_SKIP);
return 0;
}
//<-
//s : filter
//->
//null
static int SetFilterParse(lua_State *L)
{
const char* strValue = lua_tostring(L,-1);
CTemplate::Instance()->SetFilter(strValue,FT_PARSE);
return 0;
}
//<-
//s : filter
//->
//null
static int SetFilterStore(lua_State *L)
{
const char* strValue = lua_tostring(L,-1);
CTemplate::Instance()->SetFilter(strValue,FT_STORE);
return 0;
}
//<-
//s : url
//->
//null
static int AddTask(lua_State *L)
{
const char* strValue = lua_tostring(L,-1);
return 0;
}
//<-
//s : url
//s : refer
//->
//null
static int AddTaskEx(lua_State *L)
{
const char* strValue1 = lua_tostring(L,-2);
const char* strValue2 = lua_tostring(L,-1);
return 0;
}
//<-
//s : pattern
//s : foo
//->
//null
static int RegisterStoreCallback(lua_State *L)
{
const char* strValue1 = lua_tostring(L,-2);
const char* strValue2 = lua_tostring(L,-1);
CTemplateEx::SetStoreCallback(strValue1,strValue2);
return 0;
}
//<-
//s : task pattern
//s : content pattern
//s : foo
//->
//null
static int RegisterStoreCallbackEx2(lua_State *L)
{
const char* strValue1 = lua_tostring(L,-3); //task pattern
const char* strValue2 = lua_tostring(L,-2); //content pattern
const char* strValue3 = lua_tostring(L,-1); //foo name
return 0;
}
//<-
//s : task pattern
//s : content pattern
//s : foo
//->
//null
static int RegisterStoreCallbackEx(lua_State *L)
{
const char* strValue1 = lua_tostring(L,-3); //task pattern
const char* strValue2 = lua_tostring(L,-2); //content pattern
const char* strValue3 = lua_tostring(L,-1); //foo name
return 0;
}
//<-
//s : refer
//s : url
//s : content
//->
//null
static int AddTaskStoreAlbum(lua_State *L)
{
const char* strValue1 = lua_tostring(L,-3);
const char* strValue2 = lua_tostring(L,-2);
const char* strValue3 = lua_tostring(L,-1);
return 0;
}
//<-
//s : refer
//s : url
//s : title
//s : singer
//->
//null
static int AddTaskStoreSong(lua_State *L)
{
const char* strValue1 = lua_tostring(L,-4);
const char* strValue2 = lua_tostring(L,-3);
const char* strValue3 = lua_tostring(L,-2);
const char* strValue4 = lua_tostring(L,-1);
return 0;
}
//<-
//s : refer
//s : url
//s : lyric
//->
//null
static int AddTaskStoreLyric(lua_State *L)
{
return 0;
}
//<-
//s : furl-refer
//s : furl
//s : refer
//->
//null
static int AddTaskStoreLink(lua_State *L)
{
return 0;
}
static int AddTaskStoreInfo(lua_State *L)
{
return 0;
}
static int SetDebugLua(lua_State *L)
{
return 0;
}
static int JsCall(lua_State *L)
{
return 0;
}
static int LetMeSee(lua_State *L)
{
return 0;
}
static int SetConstantVar(lua_State *L)
{
return 0;
}
static int GetConstantVar(lua_State *L)
{
return 1;
}
static int DownloadAndRegexPage(lua_State *L)
{
return 1;
}
static int SetCookie(lua_State *L)
{
return 0;
}
static int AddFingerFilter(lua_State *L)
{
return 0;
}
static int SetFilterReplace(lua_State *L)
{
return 0;
}
static int RegisterReplaceCallback(lua_State *L)
{
return 0;
}
static int SetNULLInBody(lua_State *L)
{
return 0;
}
//////////////////////////////////////////////////////////////////////////
#undef xx
#define xx(a) {a,#a}
static struct {
lua_CFunction pFoo;
char* lpszFoo;
}arrFoo[]={
xx(SetUtf8),
xx(SetSid),
xx(SetThread),
xx(SetCTimeOut),
xx(SetConnect),
xx(SetUserAgent),
xx(SetSeedPage),
xx(SetSeedPattern),
xx(SetConnentType),
xx(SetFilterSkip),
xx(SetFilterParse),
xx(SetFilterStore),
xx(SetFilterReplace),
xx(AddTask),
xx(AddTaskEx),
xx(RegisterStoreCallback),
xx(RegisterStoreCallbackEx),
xx(RegisterReplaceCallback),
xx(AddTaskStoreAlbum),
xx(AddTaskStoreSong),
xx(AddTaskStoreLyric),
xx(AddTaskStoreLink),
xx(AddTaskStoreInfo),
xx(SetDebugLua),
xx(JsCall),
xx(LetMeSee),
xx(SetConstantVar),
xx(GetConstantVar),
xx(DownloadAndRegexPage),
xx(SetDiscuzCookie),
xx(SetDvbbsCookie),
xx(SetCookie),
xx(AddFingerFilter),
xx(SetNULLInBody),
{NULL,NULL}
};
#undef xx
static void RegisterFoo(lua_State *L)
{
for(int i=0;i < sizeof(arrFoo)/sizeof(arrFoo[0]) && arrFoo[i].lpszFoo!=NULL;i++)
{
lua_pushcfunction(L, arrFoo[i].pFoo);
lua_setglobal(L,arrFoo[i].lpszFoo);
}
}
//////////////////////////////////////////////////////////////////////////
CString CTemplateEx::s_strScript;
std::vector<CCallBackTag> CTemplateEx::s_arrStoreCallback;
std::vector<CCallBackTag> CTemplateEx::s_arrStoreCallbackEx;
//////////////////////////////////////////////////////////////////////////
void CTemplateEx::Load(const CString& strPath)
{
LoadScript(strPath);
CallInit();
}
void CTemplateEx::LoadScript(const CString& strPath)
{
CHAR pBuffer[MAX_PATH];
std::ifstream in;
s_strScript="";
in.open(strPath);
while(!in.eof())
{
memset(pBuffer,0,MAX_PATH);
in.getline(pBuffer,MAX_PATH);
s_strScript.Append(pBuffer);
s_strScript.Append("\n");
}
in.close();
}
//////////////////////////////////////////////////////////////////////////
//2007.4.17,sunwang
static int lua_message(const char* msg)
{
DebugString("[PageCrawler]","lua_report:(%s)",msg);
//LOGE("lua_report:(%s)",msg);
return 1;
}
static int lua_report (lua_State *L, int status) {
if (status && !lua_isnil(L, -1)) {
const char *msg = lua_tostring(L, -1);
if (msg == NULL) msg = "(error object is not a string)";
lua_message(msg);
lua_pop(L, 1);
}
return status;
}
void CTemplateEx::CallInit()
{
lua_State *L = luaL_newstate();
if(L==NULL)
{
lua_message("cannot create state: not enough memory");
return;
}
luaL_openlibs(L);
//lua_cpcall(L,luaopen_base,0);
//lua_cpcall(L,luaopen_io,0);
//lua_cpcall(L,luaopen_string,0);
//register
RegisterFoo(L);
//invoke,技巧:把脚本全部写成函数,那么在这里就可以使用dostring了,然后需要初始化的东西
//通过调用特定的函数就可以了,loadstring在这里不行,tmd
//2007.2.8,sunwang
int rc = luaL_dostring(L,s_strScript);
if(rc !=0)
{
DebugString("[CTemplateEx::CallInit]","luaL_loadstring error(rc=%d)",rc);
lua_report(L,rc);
}
else
{
LPCSTR lpszFoo=SCRIPT_INIT_FOO;
lua_getglobal(L,lpszFoo);
rc = lua_pcall(L,0,0,0);
if(rc !=0)
{
DebugString("[CTemplateEx::CallInit]","lua_pcall error(rc=%d)",rc);
lua_report(L,rc);
}
}
lua_close(L);
}
void CTemplateEx::CallStoreCallback(LPCSTR lpszTask)
{
if(s_arrStoreCallback.size()==0)
return;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
//lua_cpcall(L,luaopen_base,0);
//lua_cpcall(L,luaopen_io,0);
//lua_cpcall(L,luaopen_string,0);
//register
RegisterFoo(L);
//invoke
int rc = luaL_dostring(L,s_strScript);
if(rc !=0)
{
DebugString("[CTemplateEx::CallStoreCallback]","luaL_loadstring error(rc=%d)",rc);
lua_report(L,rc);
}
else
{
for (int i=0;i<(int)s_arrStoreCallback.size();i++)
{
LPCSTR lpszFoo=s_arrStoreCallback[i].strFunction;
LPCSTR lpszPattern=s_arrStoreCallback[i].strPattern;
lua_getglobal(L,lpszFoo);
lua_pushstring(L,lpszTask);
rc = lua_pcall(L,1,0,0);
if(rc !=0)
{
DebugString("[CTemplateEx::CallStoreCallback]","lua_pcall error(rc=%d)",rc);
lua_report(L,rc);
}
}
}
lua_close(L);
}
void CTemplateEx::CallStoreCallbackEx(LPCSTR lpszTask,LPCSTR lpszContent)
{
if(s_arrStoreCallbackEx.size()==0)
return;
lua_State *L = luaL_newstate();
luaL_openlibs(L);
//lua_cpcall(L,luaopen_base,0);
//lua_cpcall(L,luaopen_io,0);
//lua_cpcall(L,luaopen_string,0);
//register
RegisterFoo(L);
//invoke
int rc = luaL_dostring(L,s_strScript);
if(rc !=0)
{
DebugString("[CTemplateEx::CallStoreCallback]","luaL_loadstring error(rc=%d)",rc);
lua_report(L,rc);
}
else
{
for (int i=0;i<(int)s_arrStoreCallbackEx.size();i++)
{
LPCSTR lpszFoo=s_arrStoreCallbackEx[i].strFunction;
lua_getglobal(L,lpszFoo);
lua_pushstring(L,lpszTask);
rc = lua_pcall(L,1,0,0);
if(rc !=0)
{
DebugString("[CTemplateEx::CallStoreCallback]","lua_pcall error(rc=%d)",rc);
lua_report(L,rc);
}
}
}
lua_close(L);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -