jsmin:基于wxWidgets的JSMin工具



JSMin是个很好JS压缩工具只是使用时比较不方便于是我给它加了个GUI增加了多线程支持这样可以尽量提高JSMin操作性
//核心代码
void JSMinDialog::DoOK(const wxString& srcPath,const wxString& destPath)
{
(srcPath destPath)
{
wxLog::OnLog(wxLOG_Error,wxT("源文件目录下输出目录不能是同个"),time(NULL));
;
}
wxDir srcDir(srcPath);
wxDir destDir(destPath);
wxArrayString fiels;
size_t srcs = srcDir.GetAllFiles(srcPath, &fiels, wxT("*.js"));
wxString srcFileName;
wxString destFileName;
wxString destFilePath;
for( i=0; i<fiels.Count; i)
{
srcFileName = fiels.Item(i);
destFileName = srcFileName;
destFilePath = ::wxPathOnly(destFileName);
destFilePath.Replace(srcPath,destPath);
(!::wxDirExists(destFilePath))
{
(!::wxMkdir(destFilePath))
{
continue;
}
}
destFileName.Replace(srcPath,destPath);
CJSMin* js = CJSMin(srcFileName,destFileName,wxT(""));
CJSMinThread* pThread = CJSMinThread(js);
(pThread->Create != wxTHREAD_NO_ERROR)
{
delete js;
wxLog::OnLog(wxLOG_Error,wxT("生成") + destFileName + wxT(""),wxDateTime::GetTimeNow);
}

{
wxLog::OnLog(wxLOG_Message,wxT("处理")+srcFileName+wxT(" > ") + destFileName,wxDateTime::GetTimeNow);
pThread->Run;
}
}
}
#ndef WXJSMIN_H_INCLUDED
# WXJSMIN_H_INCLUDED

# <wx/file.h>

# INLINE inline
CJSMin
{
private:
const eof = EOF;
private:
wxFile inf;
wxFile outf;

theA;
theB;
theLookahead;

private:
INLINE void action( d);
INLINE isAlphanum( c);
INLINE get;
INLINE next;
INLINE peek;
public:
INLINE void jsmin;

public:
CJSMin(wxString src,wxString dest,wxString copyRiht):inf(src),outf(dest,wxFile::write),theLookahead(EOF){}
virtual ~CJSMin;
};


INLINE CJSMin::isAlphanum( c)
{
((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') ||
(c >= 'A' && c <= 'Z') || c '_' || c '$' || c '\\' ||
c > 126);
}


/* get -- the next character from stdin. Watch out for lookahead. If
the character is a control character, translate it to a space or
linefeed.
*/

INLINE CJSMin::get
{
c = theLookahead;
theLookahead = eof;
(c eof) {
char t = 0;
//c = getc(stdin);
(!inf.Eof)
{
inf.Read(&t,1);
c = t;
}

{
c = eof;
}
}
(c >= ' ' || c '\n' || c eof) {
c;
}
(c '\r') {
'\n';
}
' ';
}


/* peek -- get the next character without getting it.
*/

INLINE CJSMin::peek
{
theLookahead = get;
theLookahead;
}


/* next -- get the next character, excluding comments. peek is used to see
a '/' is followed by a '/' or '*'.
*/

INLINE CJSMin::next
{
c = get;
(c '/') {
switch (peek) {
'/':
for (;;) {
c = get;
(c <= '\n') {
c;
}
}
'*':
get;
for (;;) {
switch (get) {
'*':
(peek '/') {
get;
' ';
}
;
eof:
fprf(stderr, "Error: JSMIN Unterminated comment.\n");
exit(1);
}
}
default:
c;
}
}
c;
}


/* action -- do something! What you do is determined by the argument:
1 Output A. Copy B to A. Get the next B.
2 Copy B to A. Get the next B. (Delete A).
3 Get the next B. (Delete B).
action treats a as a single character. Wow!
action recognizes a regular expression it is preceded by ( or , or =.
*/

INLINE void CJSMin::action( d)
{
switch (d) {
1:
//putc(theA, stdout);
outf.Write(&theA,1);
2:
theA = theB;
(theA '\'' || theA '"') {
for (;;) {
//putc(theA, stdout);
outf.Write(&theA,1);
theA = get;
(theA theB) {
;
}
(theA '\\') {
//putc(theA, stdout);
outf.Write(&theA,1);
theA = get;
}
(theA eof) {
fprf(stderr, "Error: JSMIN unterminated literal.");
exit(1);
}
}
}
3:
theB = next;
(theB '/' && (theA '(' || theA ',' || theA '=' ||


theA ':' || theA '[' || theA '!' ||
theA '&' || theA '|' || theA '?' ||
theA '{' || theA '}' || theA ';' ||
theA '\n')) {
//putc(theA, stdout);
//putc(theB, stdout);
outf.Write(&theA,1);
outf.Write(&theB,1);
for (;;) {
theA = get;
(theA '/') {
;
}
(theA '\\') {
//putc(theA, stdout);
outf.Write(&theA,1);
theA = get;
}
(theA eof) {
fprf(stderr,
"Error: JSMIN unterminated Regular Expression literal.\n");
exit(1);
}
//putc(theA, stdout);
outf.Write(&theA,1);
}
theB = next;
}
}
}


/* jsmin -- Copy the input to the output, deleting the characters which are
insignicant to JavaScript. Comments will be removed. Tabs will be
replaced with spaces. Carriage s will be replaced with linefeeds.
Most spaces and linefeeds will be removed.
*/

INLINE void CJSMin::jsmin
{
theA = '\n';
action(3);
while (theA != eof) {
switch (theA) {
' ':
(isAlphanum(theB)) {
action(1);
} {
action(2);
}
;
'\n':
switch (theB) {
'{':
'[':
'(':
'+':
'-':
action(1);
;
' ':
action(3);
;
default:
(isAlphanum(theB)) {
action(1);
} {
action(2);
}
}
;
default:
switch (theB) {
' ':
(isAlphanum(theA)) {
action(1);
;
}
action(3);
;
'\n':
switch (theA) {
'}':
']':
')':
'+':
'-':
'"':
'\'':
action(1);
;
default:
(isAlphanum(theA)) {
action(1);
} {
action(3);
}
}
;
default:
action(1);
;
}
}
}
}

#end // WXJSMIN_H_INCLUDED
#ndef THREAD_H_INCLUDED
# THREAD_H_INCLUDED
# "wx/thread.h"
CJSMin;
CJSMinThread : public wxThread
{
CJSMin* m_jsmin;
public:
CJSMinThread(CJSMin* jsmin):m_jsmin(jsmin){}
public:
virtual void* Entry;
};

#end // THREAD_H_INCLUDED
# "../thread/thread.h"
# "../jsmin/wxjsmin.h"
void* CJSMinThread::Entry
{
m_jsmin->jsmin;
delete m_jsmin;
NULL;
}
Tags:  开源wxwidgets wxwidgets中文教程 wxwidgets jsmin

延伸阅读

最新评论

发表评论