C++技巧:基于wxWidgets的JSMin工具

C++技巧:基于wxWidgets的JSMin工具,第1张

JSMin是一个很好的JS压缩工具,但是使用起来不方便。所以,我给它增加了一个GUI,增加了多线程支持。这样可以尽可能的提高JSMin的可操作性。
//核心代码
void jsmin dialog::dook(const wx string & src path,const wx string & dest path)
{
if(src path = = dest path)
{
wxlog::onlog(wxlog _ error,wxt("源文件目录下的输出目录不能相同"),time(NULL)
return;
}
wxDir srcDir(src path);
wxDir dest dir(dest path);
wxArrayString fiels;
size_t srcs = srcDir。GetAllFiles(srcPath,&fiels,wxT("*。js”);
wx string src filename;
wx string dest filename;
wx string destFilePath;
for(int I = 0;i {
srcFileName = fiels。项目(一);
dest filename = src filename;
destFilePath =::wxpath only(destFileName);
destFilePath。Replace(srcPath,dest path);
如果(!* wxDirExists(destFilePath))
{
if(!* wxMkdir(destFilePath))
{
continue;
}
}
dest filename。Replace(srcPath,dest path);
cjs min * js = new cjs min(src filename,destFileName,wxT(" ");
CJSMinThread * pThread = new CJSMinThread(js);
if(pThread->Create()!= wx thread _ NO _ ERROR)
{
删除js;
wxlog:: onlog (wxlog _ error,wxT(" generate ")+dest filename+wxT(" error "),wx datetime::getti menow());
}
else
{
wxlog::onlog(wxlog _ message,wxt(" processing ")+src filename+wxt(" > "+dest filename,wx datetime::gettimenow(
pThread-> Run();
}
}
}
# ifndef WXJSMIN _ H _ INCLUDED
# define WXJSMIN _ H _ INCLUDED
# include
# define INLINE INLINE
class CJSMin
{
private:
static const int EOF = EOF;
private:
wx file INF;
wx file outf;
int theA;
int the b;
int展望未来;
private:
INLINE void action(int d);
INLINE int isAlphanum(int c);
INLINE int get();
INLINE int next();
INLINE int peek();
public:
INLINE void jsmin();
public:
CJSMin(wx string src,wxString dest,wxString copyRiht):inf(src),outf(dest,wxFile::write),the look ahead(EOF){ }
virtual ~ CJSMin();
};
INLINE int CJSMin::isAlphanum(int c)
{
return((c > = ' A ' & & c = ' 0 ' & & c(c > = ' A ' & & c 126);
}
/* get -从stdin返回下一个字符。小心前方。如果
字符是控制字符,将其转换为空格或
换行符。
*/
INLINE int cjs min::get()
{
int c = the lookahead;
the look ahead = eof;
if(c = = eof){
char t = 0;
//c = getc(stdin);
如果(!inf。Eof())
{
inf。Read(&t,1);
c = t;
}
else
{
c = eof;
}
}
if(c > = ' ' | | c = = ' \ n ' | | c = = eof){
return c;
}
if(c = = ' \ r '){
return ' \ n ';
}
return“”;
}
/* peek -获取下一个字符而不获取它。
*/
INLINE int cjs min::peek()
{
theLookahead = get();
返回theLookahead
}
/* next -获取下一个字符,不包括注释。peek()用于查看
一个“/”后面是否跟有一个“/”或“*”。
*/
INLINE int CJSMin::next()
{
int c = get();
if(c = = '/'){
switch(peek()){
case '/':
for(;;){
c = get();
if (c返回c;
}
}
case ' * ':
get();
for(;;){
switch(get()){
case ' * ':
if(peek()= = '/'){
get();
return“”;
}
break;
case eof:
fprintf(stderr,"错误:JSMIN未终止的注释。\ n ");
退出(1);
}
}
默认:
返回c;
}
}
return c;
}
/* action -做点什么!你做什么由自变量决定:
1输出A .将B复制到A .获取下一个B.
2将B复制到A .获取下一个B .(删除A)。
3获取下一个B .(删除B)。
action将字符串视为单个字符。哇!
如果正则表达式前面有(或,或=,操作将识别该表达式。
*/INLINE void CJSMin::action(int d)
{
switch(d){
case 1:
//putc(theA,stdout);
outf。写(&theA,1);
案例二:
theA = the b;
if(theA = = ' \ ' ' | | theA = = ' " '){
for(;;){
//putc(theA,stdout);
outf。写(&theA,1);
theA = get();
if(theA = = the b){
break;
}
if(theA = = ' \ \ '){
//putc(theA,stdout);
outf。写(&theA,1);
theA = get();
}
if(theA = = eof){
fprintf(stderr," Error: JSMIN未终止的字符串文字。);
退出(1);
}
}
}
情况三:
theB = next();
if(the b = = '/' & &(theA = = '(' | | theA = = ',' | | theA = = ' = ' |
theA = = ':' | | theA = = '[' | | theA = = '!'| |
theA = = ' & ' | | theA = = ' | ' | | theA = = '?'| |
theA = = ' { ' | | theA = = ' } ' | | theA = = ';'| |
theA = = ' \ n '){
//putc(theA,stdout);
//putc(theB,stdout);
outf。写(&theA,1);
outf。写(&theB,1);
for(;;){
theA = get();
if(theA = = '/'){
break;
}
if(theA = = ' \ \ '){
//putc(theA,stdout);
outf。写(&theA,1);
theA = get();
}
if(theA = = eof){
fprintf(stderr,
"错误:JSMIN未终止的正则表达式文字。\ n ");
退出(1);
}
//putc(theA,stdout);
outf。写(&theA,1);
}
theB = next();
}
}
}
/* jsmin——将输入复制到输出,删除对JavaScript不重要的字符
。评论将被删除。制表符将
替换为空格。回车将被换行符替换。
大多数空格和换行符将被删除。
*/
INLINE void cjs min::jsmin()
{
theA = ' \ n ';
动作(3);
while(西娅!= eof){
switch(theA){
case“”:
if(isAlphanum(the b)){
action(1);
} else {
action(2);
}
break;
case ' \ n ':
switch(the b){
case ' { ':
case '[':
case '(':
case '+':
case '-':
action(1);
break;
案例“”:
操作(3);
break;
默认:
if(isAlphanum(the b)){
action(1);
} else {
action(2);
}
}
break;
默认值:
switch(the b){
case“”:
if(isAlphanum(theA)){
action(1);
break;
}
动作(3);
break;
case ' \ n ':
switch(theA){
case ' } ':
case ']':
case '+':
case '-':
case ' " ':
case ' \ ':
action(1);
break;
默认:
if(isAlphanum(theA)){
action(1);
} else {
action(3);
}
}
break;
默认:
动作(1);
break;
}
}
}
}
# endif//WXJSMIN _ H _ INCLUDED
# ifndef THREAD _ H _ INCLUDED
# define THREAD _ H _ INCLUDED
# include " wx/THREAD . H "
类CJSMin
class cjs minthread:public wx thread
{
cjs min * m _ js min;
public:
cjs minthread(cjs min * jsmin):m _ jsmin(jsmin){ }
public:
virtual void * Entry();
};
# endif//THREAD _ H _ INCLUDED
# include "../thread/thread . h "
# include "../jsmin/wxjsmin . h "
void * CJSMinThread::Entry()
{
m _ jsmin-> jsmin();
删除m _ jsmin
返回NULL
}

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » C++技巧:基于wxWidgets的JSMin工具

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情