• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
FM_自由人
博客园    首页    新随笔    联系   管理    订阅  订阅

NiceCode 代码整理软件 源码

代码整理软件。

在百度空间这样不支持代码显示插件的博客里也能够漂亮的显示代码。

下载地址(附源码):http://u.115.com/file/f3df1b2b31

//done 2011.5.2
#include <iostream>
#include
<string>
#include
<assert.h>
#include
<fstream>
#include
<stdlib.h>
using namespace std;

string _gSelfPos;

const string _MODEL[2]= { "<span style=\"_D_\">" , "</span>" };
const string _CHANGE_FLAG = "_D_";
string _gKeyword[300]; //from ini
string _gChange[5][2] = { {"&","&amp;"},{"<","&lt;"},{">","&gt;"},{"\"","&quot;"},{" ","&nbsp;"} }; //转义字符

string _gGlobal[2]; //from ini
string _gKwColor[2];
string _gCmtColor[2];
string _gNumColor[2];
string _gStrColor[2];

string _gSplit[1000];
int _gCntSplit,_gCntKw;

int _fsplit( string str ) //将字符串分解 保存到_gSplit中 字母数字连续 空格,符号断开
{
_gCntSplit
= 0;
int la = 0;
for( int i = 0;i < str.length();++i )
{
if( ( str[i] >= 'a' && str[i] <= 'z') || ( str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= '0' && str[i] <= '9') )
;
//不断
else
{
if( la != i ) //清理前一个
_gSplit[_gCntSplit++] = str.substr( la,i-la );
_gSplit[_gCntSplit
++] = str.substr( i,1 ); //本身
la = i+1;
}
}
if( la < str.length() )
_gSplit[_gCntSplit
++] = str.substr( la );
return _gCntSplit;
}

/*
ini:
1.Global begin
2.Global end
3.KeyWord color
4.Comment color
5.Number color
6.String color
7.Key word
*/
void _finit( string fn ) //初始化各种信息 加载ini
{
fn
= _gSelfPos + fn;
for( int i = 0;i < 2;++i )
_gKwColor[i]
= _gCmtColor[i] = _gNumColor[i] = _gStrColor[i] = _MODEL[i];
ifstream fin(fn.c_str());
getline( fin,_gGlobal[
0] );
getline( fin,_gGlobal[
1] );

string color;
getline( fin,color );
//keyword's
_gKwColor[0].replace( _gKwColor[0].find(_CHANGE_FLAG),_CHANGE_FLAG.length(),color );
getline( fin,color );
_gCmtColor[
0].replace( _gCmtColor[0].find(_CHANGE_FLAG),_CHANGE_FLAG.length(),color );
getline( fin,color );
_gNumColor[
0].replace( _gNumColor[0].find(_CHANGE_FLAG),_CHANGE_FLAG.length(),color );
getline( fin,color );
_gStrColor[
0].replace( _gStrColor[0].find(_CHANGE_FLAG),_CHANGE_FLAG.length(),color );

string tkw;
getline( fin,tkw );
_fsplit( tkw );
_gCntKw
= _gCntSplit;

_gCntKw
= 0;
for( int i = 0;i < _gCntSplit;++i )
if( _gSplit[i] != " " ) _gKeyword[_gCntKw++] = _gSplit[i];
fin.close();

////------------------------------------------------
// for( int i = 0;i < 2;++i )
// {
// cout << _gGlobal[i] << endl;
// cout << _gKwColor[i] << endl;
// cout << _gCmtColor[i] << endl;
// cout << _gStrColor[i] << endl;
// cout << _gNumColor[i] << endl;
// cout << "----------------------------------" << endl;
// }
// for( int i = 0;i < _gCntKw;++i )
// cout << _gKeyword[i] << ' ';
// cout << endl;
// system("pause");
}

string _fchange( string str ) //转义
{
for( int i = 0;i < 5;++i )
if( str.find(_gChange[i][0]) < str.length() )
str.replace( str.find(_gChange[i][
0]),_gChange[i][0].length(),_gChange[i][1] );
return str;
}

bool _fisKeyWord( string str )
{
for( int i = 0;i < _gCntKw;++i )
if( _gKeyword[i] == str )
return true;
return false;
}

bool _fisNumber( string str )
{
for( int i = 0;i < str.length();++i )
if( str[i] > '9' || str[i] < '0' )
return false;
return true;
}

void _fwork( string srFn )
{
string iniFn = srFn.substr(srFn.rfind('.')+1) + ".ini";
//cout << iniFn << endl;
//system("pause");
_finit(iniFn);

ifstream sfin( srFn.c_str() );
ofstream sfout( (srFn
+".html").c_str() );

sfout
<< _gGlobal[0] << endl;

string line;
bool cmt,isstr,ign,lineIgn;
cmt
= isstr = ign = lineIgn = false;
while( getline( sfin,line ) )
{
_fsplit(line);
for( int i = 0;i < _gCntSplit;++i )
{
string nw = _gSplit[i]; //保证nw是单个字符或者诺干个字母数字 保证转义顺利进行
string prt = _fchange(nw);
//nw = _fchange(nw);
if( ign || lineIgn )
{
ign
= false;
}

else if( nw == "*" && i+1 < _gCntSplit && _gSplit[i+1] == "/" ) //end
{
cmt
= false;
prt
+= _gCmtColor[1];
}

else if( cmt ) // in comment
{
;
}

else if( nw == "/" && i+1 < _gCntSplit && _gSplit[i+1] == "*" ) //*
{
cmt
= true;
prt
= _gCmtColor[0] + prt;
}

else if( nw == "/" && i+1 < _gCntSplit && _gSplit[i+1] == "/" ) //'//'
{
lineIgn
= true;
prt
= _gCmtColor[0] + prt;
}

else if( nw == "\\" && i+1 < _gCntSplit )
{
ign
= true;
}

else if( isstr || nw == "\"" || nw == "\'" )
{
if( nw == "\"" || nw == "\'" )
{
isstr
= !isstr;
if( isstr )
prt
= _gStrColor[0] + prt;
else
prt
= prt + _gStrColor[1];
}
}

else
{
//keyword number
if( _fisKeyWord(nw) ) prt = _gKwColor[0] + prt + _gKwColor[1];
if( _fisNumber(nw) ) prt = _gNumColor[0] + prt + _gNumColor[1];
}

sfout
<< prt;
}
if( lineIgn )
{
lineIgn
= 0;
sfout
<< _gCmtColor[1];
}
sfout
<< endl;
}
sfout
<< _gGlobal[1] << endl;

sfin.close();
sfout.close();
}

int main( int argc,char **argv )
{
if( argc == 1 )
{
cout
<< "可以把源文件拖到程序图标来运行程序" << endl;
system(
"pause");
return 0;
}
_gSelfPos
= argv[0];
_gSelfPos
= _gSelfPos.substr( 0,_gSelfPos.rfind("\\")+1 );
//_fwork( "D:\\niceCode.cpp");
_fwork( argv[1] );
return 0;
}

posted @ 2011-05-02 17:40  FM_自由人  阅读(297)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3