程序的总体说明:
这个程序源于自己喜欢诺机里的ActiveNote程序,所以用C++写了个程序,用于转换电脑里的文本文件为HTML格式的文件,然后导到手机里阅读。该版本是一个精简版本,做成了DLL更通用。
DLL接口提供了两个函数,第一个函数,Txt2Html,是转换的主函数,第一个参数是完整路径的文件名,第二个参数是设置参数;
第二个函数,GetInfoLog,用于返回处理信息,如果成功转换,返回一些转换的信息,如转换了多少文件之类的,如果失败,则由此获得错误代码。
关于转换参数的设计:
(一)特殊参数设计:#@1 该参数为配色方案1 ,具体是网页背景黄色,字体为褐色。
(二)默认参数:background-color:#1E4641; 设置背景色蓝色
color:#F6F2E9; 设置字体色灰白色
font-size:12pt; 设置字体大小
line-height:135%; 设置行间距
margin-left:10px; 设置页边距
以上为预置的默认的五个参数。参数可以自己灵活设定,只要符合CSS语法即可,相同关键字的则覆盖默认值。注意设置的参数之间以分号“;”隔开,最好在分号后添个空格。
注意,以上参数设置在body{}之中,因此作用于整个页面。
特殊语法的设计:
文本转换是逐行进行的,如果读到一行起始两个字符是{{,则将其后的内容原样输出至文件中,不做转换处理,这类似于C++中的解释标记。不同的是{{必须是顶行,作用域直到遇到下一个换行符。防止潜在的误判,加了一个条件,即该行中存在HTML标签符“<”。
这样设计的好处在于,可以自己手动对文本格式进行设定。如标题设置:
{{<h1>标题文字</h1>
再如字体设置
{{<font size="-1">文本</font>
还有最有趣之处在于,添加链接,插入图片等
{{<img src="1.gif">
这样,在手机上用HTML格式的文档阅读,表现的会更丰富一些。
其他设计:
1.代码中处理的文件名用vector向量管理,好处在于以后升级可处理一批文件,现在的版本只是处理一个文件。
2.文本最终会转成网页通用的编码UTF-8。
不足之处:
文本文件只能是ANSI编码,不支持Unicode,如果是UTF-8编码的文本文件,转网页处理没有问题,转UTF-8编码的时候需要加判断条件,跳过编码处理。
以下附带实现源代码,仅供学习参考!
这个程序源于自己喜欢诺机里的ActiveNote程序,所以用C++写了个程序,用于转换电脑里的文本文件为HTML格式的文件,然后导到手机里阅读。该版本是一个精简版本,做成了DLL更通用。
DLL接口提供了两个函数,第一个函数,Txt2Html,是转换的主函数,第一个参数是完整路径的文件名,第二个参数是设置参数;
第二个函数,GetInfoLog,用于返回处理信息,如果成功转换,返回一些转换的信息,如转换了多少文件之类的,如果失败,则由此获得错误代码。
关于转换参数的设计:
(一)特殊参数设计:#@1 该参数为配色方案1 ,具体是网页背景黄色,字体为褐色。
(二)默认参数:background-color:#1E4641; 设置背景色蓝色
color:#F6F2E9; 设置字体色灰白色
font-size:12pt; 设置字体大小
line-height:135%; 设置行间距
margin-left:10px; 设置页边距
以上为预置的默认的五个参数。参数可以自己灵活设定,只要符合CSS语法即可,相同关键字的则覆盖默认值。注意设置的参数之间以分号“;”隔开,最好在分号后添个空格。
注意,以上参数设置在body{}之中,因此作用于整个页面。
特殊语法的设计:
文本转换是逐行进行的,如果读到一行起始两个字符是{{,则将其后的内容原样输出至文件中,不做转换处理,这类似于C++中的解释标记。不同的是{{必须是顶行,作用域直到遇到下一个换行符。防止潜在的误判,加了一个条件,即该行中存在HTML标签符“<”。
这样设计的好处在于,可以自己手动对文本格式进行设定。如标题设置:
{{<h1>标题文字</h1>
再如字体设置
{{<font size="-1">文本</font>
还有最有趣之处在于,添加链接,插入图片等
{{<img src="1.gif">
这样,在手机上用HTML格式的文档阅读,表现的会更丰富一些。
其他设计:
1.代码中处理的文件名用vector向量管理,好处在于以后升级可处理一批文件,现在的版本只是处理一个文件。
2.文本最终会转成网页通用的编码UTF-8。
不足之处:
文本文件只能是ANSI编码,不支持Unicode,如果是UTF-8编码的文本文件,转网页处理没有问题,转UTF-8编码的时候需要加判断条件,跳过编码处理。
以下附带实现源代码,仅供学习参考!
头文件1:
#ifndef TXT2HTML_H
#define TXT2HTML_H
extern "C" int __declspec(dllexport)Txt2Html(char *file, char * para);
extern "C" unsigned long __declspec(dllexport)GetInfoLog();
#endif
头文件2:
// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//
#if !defined(AFX_STDAFX_H__B8EF9455_1C3B_4FBD_A3C8_D8D11154AD3E__INCLUDED_)
#define AFX_STDAFX_H__B8EF9455_1C3B_4FBD_A3C8_D8D11154AD3E__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Insert your headers here
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <windows.h>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
// TODO: reference additional headers your program requires here
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_STDAFX_H__B8EF9455_1C3B_4FBD_A3C8_D8D11154AD3E__INCLUDED_)
实现文件:
// txt2html.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include "txt2html.h"
#include <vector>
using namespace std;
string& replaceAll(string& context,const string& from,const string& to);
unsigned long infoLog;
//const var
const int numOfRsvdFormatStr=5;
string rsvdFormatKeyStrList[numOfRsvdFormatStr] //系统预定的格式设定参数关键字
= {
string("background-color"),
string("color"),
string("font-size"),
string("line-height"),
string("margin-left")
};
//string("word-spacing"),string("letter-spacing"), 去除两个属性
string rsvdFormatStrList[numOfRsvdFormatStr] // 预设定参数以及默认值
= {
string(" background-color:#1E4641; "),
string(" color:#F6F2E9; "),
string(" font-size:12pt; "),
string(" line-height:135%; "),
string(" margin-left:10px; ")
};
// string(" word-spacing:2px; "), string(" letter-spacing:1px; "),
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
return TRUE;
}
unsigned long GetInfoLog()
{
return infoLog;
}
int Txt2Html(char *file, char *para)
{
// file Title List
// 定义文件变量
vector<string> fileTitleList;
string inFilePath,outFilePath,fileExtName,fileTitle;
string filePathName;
string formatString;
// 解析文件参数
inFilePath=string(file);
size_t lastPoint=inFilePath.rfind('.');
size_t lastDivd=inFilePath.rfind('\\');
fileExtName=inFilePath.substr(lastPoint+1,3);
fileTitle=inFilePath.substr(lastDivd+1,lastPoint-lastDivd-1);
filePathName=inFilePath.substr(0,lastDivd+1);
if(fileExtName.compare("txt")&&fileExtName.compare("TXT"))
{
infoLog=ERROR_INVALID_PARAMETER;
return 1;
}
// 解析处理命令参数
string tempFormatString=string(para);
size_t foundHere;
foundHere=tempFormatString.find("#@1");
if(foundHere!=string::npos)
{
rsvdFormatStrList[0]=string("background-color:#F0DCB4; ");
rsvdFormatStrList[1]=string("color:#64190A; ");
tempFormatString.erase(foundHere,3);
}
size_t endHere;
size_t startHere;
for(int i=0;i<numOfRsvdFormatStr;i++)
{
if((startHere=tempFormatString.find(rsvdFormatKeyStrList[i]))!=string::npos)
{
endHere=tempFormatString.find(";",startHere);
if(endHere==string::npos) endHere=tempFormatString.size();
formatString+=tempFormatString.substr(startHere,endHere-startHere+1);
formatString+=string(" ");
tempFormatString.erase(startHere,endHere-startHere+1);
}else
{
formatString+=rsvdFormatStrList[i];
}
}
formatString+=tempFormatString;
// 测试文件是否可打开,将可打开的添加至文件列表
ifstream inFile(inFilePath.c_str());
if(!inFile)
{
infoLog=GetLastError();
return 1;
}
inFile.close();
fileTitleList.push_back(fileTitle);
// 循环处理列表中的文件
string strLine;
for(i=0;i<fileTitleList.size();i++)
{
inFilePath=filePathName+fileTitleList.at(i)+string(".txt");
outFilePath=filePathName+fileTitleList.at(i); // 生成临时文件
ifstream inFile(inFilePath.c_str());
if(!inFile)
{
infoLog=GetLastError();
return 1;
}
ofstream outFile(outFilePath.c_str());
if(!outFile)
{
infoLog=GetLastError();
return 1;
}
// generate file head
outFile<<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
<<"<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
<<"<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n"
<<"<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\" />\n"
<<"<title>"<<fileTitleList.at(i)<<"</title>\n"
<<"<style type=\"text/css\">\nbody {"
<<formatString
<<" }\nimg {border: none;}\n</style>\n</head>\n<body><p>\n";
for(;;)
{
if(inFile.eof())break;
getline(inFile,strLine,'\n');
// special characters parse
if(strLine.size() > 2
&& strLine.at(0)=='{'
&& strLine.at(1)=='{'
&& strLine.find("<")!=string::npos)
{
strLine=strLine.substr(2,strLine.size()-2);
}
else if(!strLine.empty())
{
replaceAll(strLine,"&","&"); // It should be the first
replaceAll(strLine,"<","<");
replaceAll(strLine,">",">");
replaceAll(strLine," "," ");
replaceAll(strLine,"\"",""");
replaceAll(strLine,"\t"," ");
}
outFile<<strLine<<"<br>";
}
outFile<<"</p></body></html>\n";
inFile.close();
outFile.close();
}
// 处理UTF-8转码
for(i=0;i<fileTitleList.size();i++)
{
inFilePath=filePathName+fileTitleList.at(i);
outFilePath=filePathName+fileTitleList.at(i)+string(".html");
ifstream inFile(inFilePath.c_str(),ios::binary);
if(!inFile)
{
infoLog=GetLastError();
return 1;
}
ofstream outFile(outFilePath.c_str(),ios::binary);
if(!outFile)
{
infoLog=GetLastError();
return 1;
}
// 转码核心部分
ostringstream ansiTextStringStream;
ansiTextStringStream<<inFile.rdbuf();
string ansiTextString=string(ansiTextStringStream.str());
int uniTextLength=MultiByteToWideChar(CP_ACP,NULL,ansiTextString.c_str(),ansiTextString.length(),NULL,0); // 获得转换缓冲区大小
if(!uniTextLength)
{
infoLog=GetLastError();
return 1;
}
LPWSTR uniTextBuf = new WCHAR[uniTextLength];
if(!MultiByteToWideChar(CP_ACP, NULL, ansiTextString.c_str(),ansiTextString.length(), uniTextBuf, uniTextLength))
{
infoLog=GetLastError();
return 1;
}
int utf8TextLength=WideCharToMultiByte(CP_UTF8, NULL, uniTextBuf, uniTextLength, NULL,0,NULL,NULL);
if(!utf8TextLength)
{
infoLog=GetLastError();
return 1;
}
LPSTR utf8TextBuf=new CHAR[utf8TextLength];
if(!WideCharToMultiByte(CP_UTF8, NULL, uniTextBuf, uniTextLength, utf8TextBuf, utf8TextLength,NULL,NULL))
{
infoLog=GetLastError();
return 1;
}
outFile.write(utf8TextBuf,utf8TextLength);
if(uniTextBuf)
{
delete []uniTextBuf;
}
if(utf8TextBuf)
{
delete []utf8TextBuf;
}
inFile.close();
outFile.close();
DeleteFile(inFilePath.c_str());
}
infoLog=fileTitleList.size();
return 0;
}
string& replaceAll(string& context,const string& from,const string& to)
{
size_t lookHere = 0;
size_t foundHere;
while((foundHere=context.find(from,lookHere))!=string::npos)
{
context.replace(foundHere,from.size(),to);
lookHere=foundHere+to.size();
}
return context;
}
附,生成的dll下载地址: