网络资源的拷贝粘贴 备份参考之用


Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

21 October 2008

告诉我什么是stub程序

1) From CSDN forum:

j2ee里面的stub是这样说的..为屏蔽客户调用远程主机上的对象,必须提供某种方式来模拟本地对象,这种本地对象称为存根(stub),存根负责接收本地方法调用,并将它们委派给各自的具体实现对象

2) From someone's blog:

stub:RPC(Remote Procedure Call protocol)的一个重要思想就是使远程调用看起来象当地的调用一样,也就是说调用进程无需知道被调进程具体在哪台机器上执行。Stub就是用来保证此特性的很重要的部分。具体的讲,比如在客户端,一个进程在执行过程中调用到了某个函数fn(),此函数的具体实现是在远程的某台机器上,那么此进程实际上是调用了位于当地机器上的另外一个版本的fn()(起名为c_fn()),此c_fn()就是客户端的一个stub. 对应的,当客户端的消息发送到服务器端时,服务器端也不是把消息直接就交给真正的fn(),而是同样先交给一个不同版本的fn()(起名为s_fn()),此s_fn()就是服务器端的一个stub.

注:远程过程调用(RPC)是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议。RPC 协议假定某些传输协议的存在,如 TCP 或 UDP,为通信程序之间携带信息数据。在 OSI 网络通信模型中,RPC 跨越了传输层和应用层。RPC 使得开发包括网络分布式多程序在内的应用程序更加容易。

RPC 采用客户机/服务器模式。请求程序就是一个客户机,而服务提供程序就是一个服务器。首先,调用进程发送一个有进程参数的调用信息到服务进程,然后等待应答信息。在服务器端,进程保持睡眠状态直到调用信息的到达为止。当一个调用信息到达,服务器获得进程参数,计算结果,发送答复信息,然后等待下一个调用信息,最后,客户端调用过程接收答复信息,获得进程结果,然后调用执行继续进行。

17 December 2007

Why Not C? What are O'Caml (caml.inria.fr) and Haskell (haskell.org)?

Why Not C?
Hal Daume III
-------------


I've been asked on many occasions a question like: "Why don't you use
x", where x is "C", "C++" or "Java." There are, of course, many
reasons, but here is list the four that I find most compelling. These
all essentially boil down to the fact that I think a programming
language should be a friend who helps you accomplish your goals, not
an enemy you should have to fight with.

1. I refuse to allocate my own memory.

I find the process of dealing with low-level bugaboos like allocating
memory obnoxious. It serves only to clutter otherwise clear code. It
also makes debugging a major hassle and causes programs to core dump
like crazy. I'm aware of various libraries that allow some languages
that don't have build in memory management to do naive things like
reference counting, but this is -- if nothing else -- simply an
admonishon that your language has failed you. Memory management is
not something that should be tacked on to a language in the form of a
library. I believe this for many reasons: aesthetics, efficiency,
etc.

2. I refuse to write 20 lines where one will suffice.

This is mostly aimed at Java, since I don't really know of any other
language that got this so wrong. The whole "everything must be a
class" thing with Java is just such a pain to deal with. Comparing,
for instance, the Hello World program in C to Java is just laughable.
More than that, I also just don't find the whole OO paradigm all that
expressive. Everything I want to express is typically easily
accomplishable with a simple data type declaration and a few simple
functions on that data type. This refusal also includes refusing to
write 20 characters when one will do (e.g., x.equals(y) rather than
x=y).

3. I refuse to live without higher order functions.

This might seem a bit elitist, but once you get use to writing code
with higher order functions, it's really hard to go back. Sure, C has
function pointers, C++ has something nasty like that and Java has
anonymous classes, but this is all just really too much hassle for
dealing with something so basic (see point 2).

4. I refuse to live without algebraic data types.

They are simply too expressive. Try writing code for reading in parse
trees in C or C++ or Java (for a real challenge, do it in Perl). This
is just a painful experience. Algebraic data types are too useful to
live without, especially when dealing with any sort of structured
data. Having to introduce heavy-weight classes to handle the same
thing is overkill (not to mention terribly inefficient).

So that sums it up, more or less. My languages of choice, of course,
are O'Caml (caml.inria.fr) and Haskell (haskell.org). I wind up using
O'Caml for everything, primarily because the code it produces tends to
be more efficient than Haskell and because it has better support for
arrays. I would write Haskell exclusively if it optimized array
operations better and had built-in syntax supporting them.

22 November 2007

利用VS调试JAVASCRIPT

zz 利用VS调试JAVASCRIPT

(1) 打开IE --> Internet Options -- > Advanced ; 去掉"Disable Script Debugging" 上的选项


(2) 打开需要调试的页面



(3) 启动VS.Net IDE, 选择 TOOLS-Debug Process (Ctrl + Alt + P). 选择需要调试的IE进程。


(4) 点击Attach按钮,选择Script选项; 再OK关掉对话框



(5) 选择IDE菜单 DEBUGàWindowsàRunning Documents (Ctrl + Alt + N)


(6) 选择需要调试的Javascript并设置断点

(7) 回到IE页面,运行与JavaScript有关的功能,就会中断到断点上。这时可以观察相关的参数,就和其他VS.Net程序一样。

JS很有意思,可以动态增加成员变量,而且还可以以HASH表的形式访问..

SUBSTRING函数还是可以颠倒START和END 的..


13 April 2007

C++ std::string, std::wstring转换方法 [转载]

VS2005:C++ std::string, std::wstring转换方法

by Byte

随着VS2003升级到VS2005,很多以前熟悉的输入输出方式以及参数传递方式都不再有效
(参看
vs2003 vs2005代码升级要点)。

其中与字符串相关的内容是,

wcout
不再有效,默认参数传递方式由char*改成了wchar_t*等几个方面。
为了解决上面的这些问题,这篇文章里,我将给出几种
C++ std::stringstd::wstring相互转换的转换方法。
第一种方法:调用WideCharToMultiByte()和MultiByteToWideChar(),代码如下(关于详细的解释,可以参考《windows核心编程》):
#include
#include
using namespace std;
//Converting a WChar string to a Ansi string
std::string WChar2Ansi(LPCWSTR pwszSrc)
{
int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
if (nLen<= 0) return std::string("");
char* pszDst = new char[nLen];
if (NULL == pszDst) return std::string("");
WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
pszDst[nLen -1] = 0;
std::string strTemp(pszDst);
delete [] pszDst;
return strTemp;
}
string ws2s(wstring& inputws){ return WChar2Ansi(inputws.c_str()); }
//Converting a Ansi string to WChar string
std::wstring Ansi2WChar(LPCSTR pszSrc, int nLen)
{
int nSize = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)pszSrc, nLen, 0, 0);
if(nSize <= 0) return NULL;
WCHAR *pwszDst = new WCHAR[nSize+1];
if( NULL == pwszDst) return NULL;
MultiByteToWideChar(CP_ACP, 0,(LPCSTR)pszSrc, nLen, pwszDst, nSize);
pwszDst[nSize] = 0;
if( pwszDst[0] == 0xFEFF) // skip Oxfeff
for(int i = 0; i <>
pwszDst[i] = pwszDst[i+1];
wstring wcharString(pwszDst);
delete pwszDst;
return wcharString;
}
std::wstring s2ws(const string& s){ return Ansi2WChar(s.c_str(),s.size());}
第二种方法:采用ATL封装_bstr_t的过渡:(注,_bstr_是Microsoft Specific的,所以下面代码可以在VS2005通过,无移植性);
#include
#include
using namespace std;
#pragma comment(lib, "comsuppw.lib")
string ws2s(const wstring& ws);
wstring s2ws(const string& s);
string ws2s(const wstring& ws)
{
_bstr_t t = ws.c_str();
char* pchar = (char*)t;
string result = pchar;
return result;
}
wstring s2ws(const string& s)
{
_bstr_t t = s.c_str();
wchar_t* pwchar = (wchar_t*)t;
wstring result = pwchar;
return result;
}
第三种方法:使用CRT库的mbstowcs()函数和wcstombs()函数,平台无关,需设定locale。
#include
#include
using namespace std;
string ws2s(const wstring& ws)
{
string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
setlocale(LC_ALL, "chs");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 2 * ws.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest,0,_Dsize);
wcstombs(_Dest,_Source,_Dsize);
string result = _Dest;
delete []_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
wstring s2ws(const string& s)
{
setlocale(LC_ALL, "chs");
const char* _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest,_Source,_Dsize);
wstring result = _Dest;
delete []_Dest;
setlocale(LC_ALL, "C");
return result;
}
//第四种方法,标准C++转换方法:(待续)
//第五种方法,在C++中使用C#类库:(待续)

其中第四种,我的实现始终存在一些问题。 第五种,我只是知道有这么一种方案,没有时间去详细了解,算是给一些提示吧。

(转载自bianyongtao的msn space)

CString详细讲解 [转载]

CString详细讲解(zz)


前 言:串操作是编程中最常用也最基本的操作之一。 做为VC程序员,无论是菜鸟或高手都曾用过Cstring。而且好像实际编程中很难离得开它(虽然它不是标准C++中的库)。因为MFC中提供的这个类对 我们操作字串实在太方便了,CString不仅提供各种丰富的操作函数、操作符重载,使我们使用起串起来更象basic中那样直观;而且它还提供了动态内 存分配,使我们减少了多少字符串数组越界的隐患。但是,我们在使用过程中也体会到CString简直太容易出错了,而且有的不可捉摸。所以有许多高人站过 来,建议抛弃它。

在此,我个人认为:CString封装得确实很完美,它有许多优点,如“容易使用 ,功能强,动态分配内存,大量进行拷贝时它很能节省内存资源并且执行效率高,与标准C完全兼容,同时支持多字节与宽字节,由于有异常机制所以使用它安全方 便” 其实,使用过程中之所以容易出错,那是因为我们对它了解得还不够,特别是它的实现机制。因为我们中的大多数人,在工作中并不那么爱深入地去看关于它的文 档,何况它还是英文的。

由于前几天我在工作中遇到了一个本不是问题但却特别棘手、特别难解决而且莫名惊诧 的问题。好来最后发现是由于CString引发的。所以没办法,我把整个CString的实现全部看了一遍,才慌然大悟,并彻底弄清了问题的原因(这个问 题,我已在csdn上开贴)。在此,我想把我的一些关于CString的知识总结一番,以供他(她)人借鉴,也许其中有我理解上的错误,望发现者能通知 我,不胜感谢。

1. CString实现的机制.

CString是通过“引用”来管理串的,“引用”这个词我相信大家并不陌生,象 Window内核对象、COM对象等都是通过引用来实现的。而CString也是通过这样的机制来管理分配的内存块。实际上CString对象只有一个指 针成员变量,所以任何CString实例的长度只有4字节.

即: int len = sizeof(CString);//len等于4

这个指针指向一个相关的引用内存块,如图: CString str("abcd");

‘A’

‘B’

‘C’

‘D’

0

0x04040404 head部,为引用内存块相关信息

str 0x40404040

正因为如此,一个这样的内存块可被多个CString所引用,例如下列代码:

CString str("abcd");

CString a = str;

CString b(str);

CString c;

c = b;

上面代码的结果是:上面四个对象(str,a,b,c)中的成员变量指针有相同的值,都为0x40404040.而这块内存块怎么知道有多少个CString引用它呢?同样,它也会记录一些信息。如被引用数,串长度,分配内存长度。

这块引用内存块的结构定义如下:

struct CStringData

{

long nRefs; //表示有多少个CString 引用它. 4

int nDataLength; //串实际长度. 4

int nAllocLength; //总共分配的内存长度(不计这头部的12字节). 4

};

由于有了这些信息,CString就能正确地分配、管理、释放引用内存块。

如果你想在调试程序的时候获得这些信息。可以在Watch窗口键入下列表达式:

(CStringData*)((CStringData*)(this->m_pchData)-1)或

(CStringData*)((CStringData*)(str.m_pchData)-1)//str为指CString实例

正因为采用了这样的好机制,使得CString在大量拷贝时,不仅效率高,而且分配内存少。

2.LPCTSTR 与 GetBuffer(int nMinBufLength)

这两个函数提供了与标准C的兼容转换。在实际中使用频率很高,但却是最容易出错的地方。这两个函数实际上返回的都是指针,但它们有何区别呢?以及调用它们后,幕后是做了怎样的处理过程呢?

(1) LPCTSTR 它的执行过程其实很简单,只是返回引用内存块的串地址。 它是作为操作符重载提供的,所以在代码中有时可以隐式转换,而有时却需强制转制。如:

CString str;

const char* p = (LPCTSTR)str;

//假设有这样的一个函数,Test(const char* p); 你就可以这样调用

Test(str);//这里会隐式转换为LPCTSTR

(2) GetBuffer(int nMinBufLength) 它类似,也会返回一个指针,不过它有点差别,返回的是LPTSTR

(3) 这两者到底有何不同呢?我想告诉大家,其本质上完全不一样,一般说LPCTSTR转换后只应该当常量使用,或者做函数的入参;而GetBuffer(...)取出指针后,可以通过这个指针来修改里面的内容,或者做函数的出参。为什么呢?也许经常有这样的代码:

CString str("abcd");

char* p = (char*)(const char*)str;

p[2] = 'z';

其实,也许有这样的代码后,你的程序并没有错,而且程序也运行得挺好。但它却是非常危险的。再看

CString str("abcd");

CString test = str;

....

char* p = (char*)(const char*)str;

p[2] = 'z';

strcpy(p, "akfjaksjfakfakfakj");//这下完蛋了

你知道此时,test中的值是多少吗?答案是"abzd"。它也跟着改变了,这不是你 所期望发生的。但为什么会这样呢?你稍微想想就会明白,前面说过,因为CString是指向引用块的,str与test指向同一块地方,当你p[2]= 'z'后,当然test也会随着改变。所以用它做LPCTSTR做转换后,你只能去读这块数据,千万别去改变它的内容。

假如我想直接通过指针去修改数据的话,那怎样办呢?就是用GetBuffer(...).看下述代码:

CString str("abcd");

CString test = str;

....

char* p = str.GetBuffer(20);

p[2] = 'z'; // 执行到此,现在test中值却仍是"abcd"

strcpy(p, "akfjaksjfakfakfakj"); // 执行到此,现在test中值还是"abcd"

为什么会这样?其实GetBuffer(20)调用时,它实际上另外建立了一块新内块存,并分配20字节长度的buffer,而原来的内存块引用计数也相应减1. 所以执行代码后str与test是指向了两块不同的地方,所以相安无事。

(4) 不过这里还有一点注意事项:就是str.GetBuffer(20)后,str的分配长度为20,即指针p它所指向的buffer只有20字节长,给它赋 值时,切不可超过,否则灾难离你不远了;如果指定长度小于原来串长度,如GetBuffer(1),实际上它会分配4个字节长度(即原来串长度);另外, 当调用GetBuffer(...)后并改变其内容,一定要记得调用ReleaseBuffer(),这个函数会根据串内容来更新引用内存块的头部信息。

(5) 最后还有一注意事项,看下述代码:

char* p = NULL;

const char* q = NULL;

{

CString str = "abcd";

q = (LPCTSTR)str;

p = str.GetBuffer(20);

AfxMessageBox(q);// 合法的

strcpy(p, "this is test");//合法的,

}

AfxMessageBox(q);// 非法的,可能完蛋

strcpy(p, "this is test");//非法的,可能完蛋

这里要说的就是,当返回这些指针后, 如果CString对象生命结束,这些指针也相应无效。

3.拷贝 & 赋值 & "引用内存块" 什么时候释放?

下面演示一段代码执行过程

void Test()

{

CString str("abcd");

//str指向一引用内存块(引用内存块的引用计数为1,长度为4,分配长度为4)

CString a;

//a指向一初始数据状态,

a = str;

//a与str指向同一引用内存块(引用内存块的引用计数为2,长度为4,分配长度为4)

CString b(a);

//a、b与str指向同一引用内存块(引用内存块的引用计数为3,长度为4,分配长度为4)

{

LPCTSTR temp = (LPCTSTR)a;

//temp指向引用内存块的串首地址。(引用内存块的引用计数为3,长度为4,分配长度为4)

CString d = a;

//a、b、d与str指向同一引用内存块(引用内存块的引用计数为4, 长度为4,分配长度为4)

b = "testa";

//这条语句实际是调用CString::operator=(CString&)函数。 b指向一新分配的引用内存块。(新分配的引用内存块的 引用计数为1, 长度为5, 分配长度为5)

//同时原引用内存块引用计数减1. a、d与str仍指向原 引用内存块(引用内存块的引用计数为3,长度为4,分配长度为4)

}

//由于d生命结束,调用析构函数,导至引用计数减1(引用内存块的引用计数为2,长度为4,分配长度为4)

LPTSTR temp = a.GetBuffer(10);

//此语句也会导致重新分配新内存块。temp指向新分配引用内存块的串首地址(新 分配的引用内存块的引用计数为1,长度为0,分配长度为10)

//同时原引用内存块引用计数减1. 只有str仍 指向原引用内存块 (引用内存块的引用计数为1, 长度为4, 分配长度为4)

strcpy(temp, "temp");

//a指向的引用内存块的引用计数为1,长度为0,分配长度为10 a.ReleaseBuffer();//注意:a指向的引用内存块的引用计数为1,长度为4,分配长度为10

}

//执行到此,所有的局部变量生命周期都已结束。对象str a b 各自调用自己的析构构

//函数,所指向的引用内存块也相应减1

//注意,str a b 所分别指向的引用内存块的计数均为0,这导致所分配的内存块释放

通过观察上面执行过程,我们会发现CString虽然可以多个对象指向同一引用内块 存,但是它们在进行各种拷贝、赋值及改变串内容时,它的处理是很智能并且非常安全的,完全做到了互不干涉、互不影响。当然必须要求你的代码使用正确恰当, 特别是实际使用中会有更复杂的情况,如做函数参数、引用、及有时需保存到CStringList当中,如果哪怕有一小块地方使用不当,其结果也会导致发生 不可预知的错误

5 FreeExtra()的作用

看这段代码

(1) CString str("test");

(2) LPTSTR temp = str.GetBuffer(50);

(3) strcpy(temp, "there are 22 character");

(4) str.ReleaseBuffer();

(5) str.FreeExtra();

上面代码执行到第(4)行时,大家都知道str指向的引用内存块计数为1,长度为22,分配长度为50. 那么执行str.FreeExtra()时,它会释放所分配的多余的内存。(引用内存块计数为1,长度为22,分配长度为22)

6 Format(...) 与 FormatV(...)

这条语句在使用中是最容易出错的。因为它最富有技巧性,也相当灵活。在这里,我没打算 对它细细分析,实际上sprintf(...)怎么用,它就怎么用。我只提醒使用时需注意一点:就是它的参数的特殊性,由于编译器在编译时并不能去校验格 式串参数与对应的变元的类型及长度。所以你必须要注意,两者一定要对应上,

否则就会出错。如:

CString str;

int a = 12;

str.Format("first:%l, second: %s", a, "error");//result?试试

7 LockBuffer() 与 UnlockBuffer()

顾名思议,这两个函数的作用就是对引用内存块进行加锁及解锁。但使用它有什么作用及执行过它后对CString串有什么实质上的影响。其实挺简单,看下面代码:

(1) CString str("test");

(2) str.LockBuffer();

(3) CString temp = str;

(4) str.UnlockBuffer();

(5) str.LockBuffer();

(6) str = "error";

(7) str.ReleaseBuffer();

执行完(3)后,与通常情况下不同,temp与str并不指向同一引用内存块。你可以在watch窗口用这个表达式(CStringData*)((CStringData*)(str.m_pchData)-1)看看。

其实在msdn中有说明:

While in a locked state, the string is protected in two ways:

No other string can get a reference to the data in the locked string, even if that string is assigned to the locked string.

The locked string will never reference another string, even if that other string is copied to the locked string.

8 CString 只是处理串吗?

不对,CString不只是能操作串,而且还能处理内存块数据。功能完善吧!看这段代码

char p[20];

for(int loop=0; loop

{

p[loop] = 10-loop;

}

CString str((LPCTSTR)p, 20);

char temp[20];

memcpy(temp, str, str.GetLength());

str完全能够转载内存块p到内存块temp中。所以能用CString来处理二进制数据

8 AllocSysString()与SetSysString(BSTR*)

这两个函数提供了串与BSTR的转换。使用时须注意一点:当调用AllocSysString()后,须调用它SysFreeString(...)

9 参数的安全检验

在MFC中提供了多个宏来进行参数的安全检查,如:ASSERT. 其中在CString中也不例外,有许多这样的参数检验,其实这也说明了代码的安全性高,可有时我们会发现这很烦,也导致Debug与Release版本 不一样,如有时程序Debug通正常,而Release则程序崩溃;而有时恰相反,Debug不行,Release行。其实我个人认为,我们对 CString的使用过程中,应力求代码质量高,不能在Debug版本中出现任何断言框,哪怕release运行似乎看起来一切正常。但很不安全。如下代 码:

(1) CString str("test");

(2) str.LockBuffer();

(3) LPTSTR temp = str.GetBuffer(10);

(4) strcpy(temp, "error");

(5) str.ReleaseBuffer();

(6) str.ReleaseBuffer();//执行到此时,Debug版本会弹出错框

10 CString的异常处理

我只想强调一点:只有分配内存时,才有可能导致抛出CMemoryException.

同样,在msdn中的函数声明中,注有throw( CMemoryException)的函数都有重新分配或调整内存的可能。

11 跨模块时的Cstring。即一个DLL的接口函数中的参数为CString&时,它会发生怎样的现象。解答我遇到的问题。我的问题原来已经发贴,地址为:

http://www.csdn.net/expert/topic/741/741921.xml?temp=.2283136

构造一个这样CString对象时,如CString str,你可知道此时的str所指向的引用内存块吗?也许你会认为它指向NULL。其实不对,如果这样的话,CString所采用的引用机制管理内存块就 会有麻烦了,所以CString在构造一个空串的对象时,它会指向一个固定的初始化地址,这块数据的声明如下:

AFX_STATIC_DATA int _afxInitData[] = {-1,0,0,0};

简要描述概括一下:当某个CString对象串置空的话,如Empty(), CString a等,它的成员变量m_pchData就会指向_afxInitData这个变量的地址。当这个CString对象生命周期结束时,正常情况下它会去对所 指向的引用内存块计数减1,如果引用计数为0(即没有任何CString引用它时),则释放这块引用内存。而现在的情况是如果CString所指向的引用 内存块是初始化内存块时,则不会释放任何内存。

说了这么多,这与我遇到的问题有什么关系呢?其实关系大着呢?其真正原因就是如果 exe模块与dll模块有一个是static编译连接的话。那么这个CString初始化数据在exe模块与dll模块中有不同的地址,因为static 连接则会在本模块中有一份源代码的拷贝。另外一种情况,如果两个模块都是share连接的,CString的实现代码则在另一个单独的dll中实现,而 AFX_STATIC_DATA指定变量只装一次,所以两个模块中_afxInitData有相同的地址。

现在问题完全明白了吧!你可以自己去演示一下。

__declspec (dllexport) void test(CString& str)

{

str = "abdefakdfj";//如果是static连接,并且传入的str为空串的话,这里出错。

}

最后一点想法:写得这里,其实CString中还有许多技巧性的好东东,我并没去解释。如很多重载的操作符、查找等。我认为还是详细看看msdn,这样也许会比我讲的好多了。我只侧重那些可能会出错的情况。当然,如我上面叙述中有错误,敬请高手指点,不胜感谢!


msdn:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_mfc_cstring_class_members.asp

CString/string/char *比较详解 [转载]

CString/string/char *比较详解

(一) 概述
string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中;
CString(typedef CStringT> CString)为Visual C++中最常用的字符串类,继承自CSimpleStringT类,主要应用在MFC和ATL编程中,主要数据类型有char(应用于ANSI), wchar_t(unicode),TCHAR(ANSI与unicode均可);
char*为C编程中最常用的字符串指针,一般以'\0'为结束标志;

(二) 构造
string是方便的,可以从几乎所有的字符串构造而来,包括CString和char*;
CString次之,可以从基本的一些字符串变量构造而来,包括char*等;
char*没有构造函数,仅可以赋值;
举例:
char* psz = “joise”;
CString cstr( psz );
string str( cstr );

(三) 运算符重载

a) operator=
string是最方便的,几乎可以直接用所有的字符串赋值,包括CString和char*;
CString次之,可以直接用些基本的字符串赋值,包括char*等;
char*只能由指针赋值,并且是极危险的操作,建议使用strcpy或者memcpy,而且char*在声明的时候如未赋初值建议先设为NULL,以避免野指针;
举例:
char *psz = NULL;
psz = new char[10]; //当然,以上的直接写成char *psz = new char[10];也是一样
memset( psz, 0, 10 );
strcpy( psz, “joise” );
CString cstr;
cstr = psz;
string str;
str = psz;
str = cstr;
delete []psz;

b) operator+
string与CString差不多,可以直接与char*进行加法,但不可以相互使用+运算符,即string str = str + cstr是非法的,须转换成char*;
char*没有+运算,只能使用strcat把两个指针连在一起;
举例:
char* psz = “joise”;
CString cstr = psz;
cstr = cstr + psz;
string str = psz;
str = str + str + psz;
strcat( psz, psz );
strcat( psz, cstr );//合法
strcat( psz, str );//非法,由此可见,CString可自动转换为const char*,而string不行

c) operator +=
string是最强大的,几乎可以与所有的字符串变量+=,包括CString和char*;
CString次之,可以与基本的一些字符串变量进行+=而来,包括char*等;
char*没有+=运算符,只能使用strcat把两个指针连在一起;

d) operator[]
CString最好,当越界时会抛出断言异常;
string与char*下标越界结果未定义;
举例:
char* psz = “joise”;
CString cstr = psz;
cout << cstr[8];
string str = psz;
cout << str[8];
cout << psz[8];

e) operator== 、operator!=、operator> 、operator< 、operator>= 、perator<=
CString与string之间不可以进行比较,但均可以与char*进行比较,并且比较的是值,而不是地址;
cout << ( psz == cstr );
cout << ( psz == str );
cout << ( str == psz );
cout << ( cstr == psz );//以上代码返回均为1


(四) 常用算法

a) 查找
作用 char* string CString
查找指定值 strchr
strstr
strrstr
strspn find Find
第一个匹配的值 fild_first_of FindOneOf
从后面开始查找 ReserveFind
指定匹配方式 find_if


注:find_if中是把范围内的值挨个代入匹配函数直至返回true
b) 比较
作用 char* string CString
查找指定值(区分大小写) strcmp
strncmp
strcoll
_strncoll operator<
operator>
operator<=
operator>=
operator==
operator!= Collate
Compare
查找指定值(不区分大小写) _stricmp
_strnicmp
_stricoll
_strnicoll CollateNoCase
CompareNoCase


注:返回值如果<0则前面的值小于后面的值,反之亦然
c) 替换
作用 char* string CString
查找指定值 _strset
_strnset replace
replace_copy
replace_copy_if
replace_if
Replace


d) 插入
作用 char* string CString
查找指定值 insert Insert






e) 增加
作用 char* string CString
动态增加值 strcat push
append Append
AppendChar
AppendFormat


f) 截取
作用 char* string CString
得到部分值 用下标操作 substr Left
Mid
Right
Truncate


g) 移除
作用 char* string CString
移除部份值 remove Remove
移除空白值 RemoveBlanks
注:此为ATL提供,非C函数 remove_if Trim
TrimLeft
TrimRigth


h) 转换大小写
作用 char* string CString
转换大小写 _strlwr
_strupr MakeLower
MakeUpper


i) 与其他类型转换
作用 char* string CString
转化为数字 atoi
atod
atof Format
转化为char* c_str GetBuffer
GetBufferSetLength


j) 格式化
作用 char* string CString
格式化 sprintf Format


k) 得到长度
作用 char* string CString
得到长度 strlen length GetLength
得到大小 size GetAllocLength


l) 判断为空
作用 char* string CString
判断是否为空 判断是否==NULL或者第一个字符是否是’\0’ empty IsEmpty


m) 重定义大小
作用 char* string CString
重定义大小 realloc
new resize GetBufferSetLength


n) 释放资源
作用 char* string CString
释放 free
delete (delete[]) ReleaseBuffer
ReleaseBufferSetLength


(五) 安全性
CString > string > char*
(六) 灵活性
CString > string >char*
(七) 可移植性
char* = string > CString


总结
综上所述,在MFC、ATL中使用字符串尽量使用CString,毕竟都是微软的孩子,各方面都比其它更有优势,而在非微软平台上或对移植性要求较高的场合推荐使用string,标准模板库提供了
那么强大的泛型算法,没必要再自己去造车轮。


Tags:cstring, string, char*

12 March 2007

HTML Tidy

HTML Tidy

HTML Tidy is a computer program and a library whose purpose is to fix invalid HTML and give the source code a reasonable layout (aka indent style).

HTML Tidy 是用来修复无效的HTML,并且给源代码一个合理的布局格式(比如说缩紧风格)
It was developed by Dave Raggett of W3C, then passed on to become a Sourceforge project. Its source code is written in ANSI C for maximum portability and precompiled binaries are available for variety of platforms. It is available under the W3C license (a permissive, BSD-style license).

W3C的Dave Raggett开发了一个HTML Tidy, 在Sourceforge网站可以下载,程序由 ANSI C写成

Examples of bad code it's able to fix:

* Missing or mismatched end tags, mixed up tags
* Adding missing items (some tags, quotes, ...)
* Reporting proprietary HTML extensions
* Change layout owing to predefined style
* Transform characters from some encodings into HTML entities
* Cleaning up presentational markup

修复的例子:

1. 结束tag的缺失,或者匹配错误
2. 填补上缺失的东西,比如一些 tag, quotes 等
3. 报告proprietary HTML extention
4. 按照预先定义好的风格改变layout
5. 解决不同字体编码的问题
6. 清空 presentational markup


下面是具体的介绍:

icon Clean up your Web pageswith HTML TIDY

The maintenance of Tidy has now been taken over by a group of enthusiastic volunteers at Source Forge, see http://tidy.sourceforge.net. There you can find the source code and binaries for a very wide range of platforms. Tidy is also available as a library TidyLib, which has been designed for easy integration with other software. This page is now somewhat dated, so for the latest information please visit the Source Forge site.

Introduction to TIDY

When editing HTML it's easy to make mistakes. Wouldn't it be nice if there was a simple way to fix these mistakes automatically and tidy up sloppy editing into nicely layed out markup? Well now there is! Dave Raggett's HTML TIDY is a free utility for doing just that. It also works great on the atrociously hard to read markup generated by specialized HTML editors and conversion tools, and can help you identify where you need to pay further attention on making your pages more accessible to people with disabilities.

Tidy is able to fix up a wide range of problems and to bring to your attention things that you need to work on yourself. Each item found is listed with the line number and column so that you can see where the problem lies in your markup. Tidy won't generate a cleaned up version when there are problems that it can't be sure of how to handle. These are logged as "errors" rather than "warnings".

Dave Raggett has now passed the baton for maintaining Tidy to a group of volunteers working together as part of the open source community at Source Forge. The source code continues to be available under an open source license, and you are encouraged to pass on bug reports and enhancement requests at http://tidy.sourceforge.net.

If you find HTML Tidy useful and you would like to say thanks, then please send me a (paper) postcard or other souvenir from the area in which you live along with a few words on what you are using Tidy for. It will be fun to map out where Tidy users are to be found! My postal address is given at the end of this file.

The W3C public email list devoted to HTML Tidy is: . To subscribe send an email to html-tidy-request@w3.org with the word subscribe in the subject line (include the word unsubscribe if you want to unsubscribe). The archive for this list is accessible online. If you would like to contact the developers, or you just want to submit an enhancement request or a bug report, please visit http://tidy.sourceforge.net.

Tidy can now perform wonders on HTML saved from Microsoft Word 2000! Word bulks out HTML files with stuff for round-tripping presentation between HTML and Word. If you are more concerned about using HTML on the Web, check out Tidy's "Word-2000" config option! Of course Tidy does a good job on Word'97 files as well!

Tidy features in an article by Scott Nesbitt on webreview.com, and more recently on Dave Central's Best of Linux, and as tool of the month on Unix Review by Joe Brockmeier, who writes:

"One thing I love about the UNIX philosophy is the idea that each program should do one job and do it really well. There are zillions of small tools for UNIX-type OSes that make life much easier and are hugely useful, but they don't necessarily get written about. They certainly don't receive the same kind of coverage that Apache and Sendmail receive. One of my favorites, HTML Tidy, is a tool for HTML/Web development that I think will interest a lot of folks. HTML Tidy cleans up HTML produced by WYSIWYG editors and such."

Tidy is available as a downloadable binary, as source code (ANSI C), or as an online service at W3C, Info Network, HTML Help's site Valet and other sites.
Tutorials for HTML and CSS

If you are just starting off and would like to know more about how to author Web pages, you may find my guide to HTML and CSS helpful. Please send me feedback on this, and I will do my best to further improve it.
HTML Slidy - a Web based alternative to PowerPoint

A new relative to Tidy, HTML Slidy is my open source presentation tool for slides written in XHTML. It provides an accessible alternative to traditional presentation tools like Microsoft PowerPoint. Best of all, there is no software to install, it just works from the Web!
Examples of TIDY at work

Tidy corrects the markup in a way that matches where possible the observed rendering in popular browsers from Netscape and Microsoft. Here are just a few examples of how TIDY perfects your HTML for you:

* Missing or mismatched end tags are detected and corrected


heading
subheading

is mapped to


heading


subheading

* End tags in the wrong order are corrected:



here is a para bold bold italic bold? normal?

is mapped to



here is a para bold bold italic bold? normal?

* Fixes problems with heading emphasis


italic heading



new paragraph

In Netscape and Internet Explorer this causes everything following the heading to be in the heading font size, not the desired effect at all!

Tidy maps the example to


italic heading



new paragraph

* Recovers from mixed up tags


heading



new paragraph bold text

some more bold text

Tidy maps this to


heading



new paragraph bold text

some more bold text

* Getting the in the right place:


heading


subheading

Tidy maps this to



heading


sub



heading

* Adding the missing "/" in end tags for anchors:

References

Tidy maps this to

References

* Perfecting lists by putting in tags missed out:


1st list item
*
* 2nd list item

is mapped to




o 1st list item

o 2nd list item


* Missing quotes around attribute values are added

Tidy inserts quote marks around all attribute values for you. It can also detect when you have forgotten the closing quote mark, although this is something you will have to fix yourself.
* Unknown/Proprietary attributes are reported

Tidy has a comprehensive knowledge of the attributes defined in the HTML 4.0 recommendation from W3C. This often allows you to spot where you have mistyped an attribute or value.
* Proprietary elements are recognized and reported as such.

Tidy will even work out which version of HTML you are using and insert the appropriate DOCTYPE element, as per the W3C recommendations.
* Tags lacking a terminating '>' are spotted

This is something you then have to fix yourself as Tidy is unsure of where the > should be inserted.

Layout style

You can choose which style you want Tidy to use when it generates the cleaned up markup: for instance whether you like elements to indent their contents or not. Several people have asked if Tidy could preserve the original layout. I am sorry to say that this would be very hard to support due to the way Tidy is implemented. Tidy starts by building a clean parse tree from the source file. The parse tree doesn't contain any information about the original layout. Tidy then pretty prints the parse tree using the current layout options. Trying to preserve the original layout would interact badly with the repair operations needed to build a clean parse tree and considerably complicate the code.

Some browsers can screw up the right alignment of text depending on how you layout headings. As an example, consider:


Heading


Heading

Both of these should be rendered the same. Sadly a common browser bug fails to trim trailing whitespace and misaligns the first heading. HTML Tidy will protect you from this bug, except when you set the indent option to "yes".

Setting the indent option to yes can also cause problems with table layout for some browsers:



will look slightly different from:







You can avoid such quirks by using indent: no or indent: auto in the config file.
Internationalization issues

Tidy offers you a choice of character encodings: US ASCII, ISO Latin-1, UTF-8 and the ISO 2022 family of 7 bit encodings. The full set of HTML 4.0 entities are defined. Cleaned up output uses HTML entity names for characters when appropriate. Otherwise characters outside the normal range are output as numeric character entities. Tidy defaults to assuming you want the output to be in US ASCII. Tidy doesn't yet recognize the use of the HTML meta element for specifying the character encoding.
Accessibility

Tidy offers advice on accessibility problems for people using non-graphical browsers. The most common thing you will see is the suggestion you add a summary attribute to table elements. The idea is to provide a summary of the table's role and structure suitable for use with aural browsers.
Cleaning up presentational markup

Many tools generate HTML with an excess of FONT, NOBR and CENTER tags. Tidy's -clean option will replace them by style properties and rules using CSS. This makes the markup easier to read and maintain as well as reducing the file size! Tidy is expected to get smarter at this in the future.

Some pages rely on the presentation effects of isolated

or
tags.Tidy deletes empty paragraph and heading elements etc. The use of empty paragraph elements is not recommended for adding vertical whitespace. Instead use style sheets, or the
element. Tidy won't discard paragraphs only containing a nonbreaking space

Teaching Tidy about new tags!

You can teach Tidy about new tags by declaring them in the configuration file, the syntax is:

new-inline-tags: tag1, tag2, tag3
new-empty-tags: tag1, tag2, tag3
new-blocklevel-tags: tag1, tag2, tag3
new-pre-tags: tag1, tag2, tag3

The same tag can be defined as empty and as inline or as empty and as block.

These declarations can be combined to define an a new empty inline or empty block element, but you are not advised to declare tags as being both inline and block!

Note that the new tags can only appear where Tidy expects inline or block-level tags respectively. This means you can't (yet) place new tags within the document head or other contexts with restricted content models. So far the most popular use of this feature is to allow Tidy to be applied to Cold Fusion files.
Limited support for ASP, JSTE and PHP

Tidy is somewhat aware of the preprocessing language called ASP which uses a pseudo element syntax <% ... %> to include preprocessor directives. ASP is normally interpreted by the web server before delivery to the browser. JSTE shares the same syntax, but sometimes also uses <# ... #>. Tidy can also cope with another such language called PHP, which uses the syntax

Tidy will cope with ASP, JSTE and PHP pseudo elements within element content and as replacements for attributes, for example:

<% if rsSchool.Fields("ID").Value = session("sessSchoolID") then Response.Write("selected") %>
value='<%=rsSchool.Fields("ID").Value%>'>
<%=rsSchool.Fields("Name").Value%>
(<%=rsSchool.Fields("ID").Value%>)


Note that Tidy doesn't understand the scripting language used within pseudo elements and attributes, and can easily get confused. Tidy may report missing attributes when these are hidden within preprocessor code. Tidy can also get things wrong if the code includes quote marks, e.g. if the example above is changed to:

value="<%=rsSchool.Fields("ID").Value%>"

Tidy will now see the quote mark preceding ID as ending the attribute value, and proceed to complain about what follows. Note you can choose whether to allow line wrapping on spaces within pseudo elements or not using the wrap-asp option. If you used ASP, JSTE or PHP to create a start tag, but placed the end tag explicitly in the markup, Tidy won't be able to match them up, and will delete the end tag for you. So in this case you are advise to make the start tag explicit and to use ASP, JSTE or PHP for just the attributes, e.g.

">do you feel lucky?

Tidy allows you to control whether line wrapping is enabled for ASP, JSTE and PHP instructions, see the wrap-asp, wrap-jste and wrap-php config options, respectively.

I regret that Tidy does not support Tango preprocessing instructions which look like:

<@if variable_1='a'>
do something
<@else>
do nothing


<@include <@cgi><@appfilepath>includes/message.html>

Tidy supports another preprocessing syntax called "Tango", but only for attribute values. Adding support for pseudo elements written in Tango looks as if it would be quite tough, so I would like to gauge the level of interest before committing to this work.
Limited support for XML

XML processors compliant with W3C's XML 1.0 recommendation are very picky about which files they will accept. Tidy can help you to fix errors that cause your XML files to be rejected. Tidy doesn't yet recognize all XML features though, e.g. it doesn't understand CDATA sections or DTD subsets.
Indenting text for a better layout

Indenting the content of elements makes the markup easier to read. Tidy can do this for all elements or just for those where it's needed. The auto-indent mode has been used below to avoid indenting the content of title, p and li elements:









para which has enough text to cause a line break,
and so test the wrapping mechanism for long lines.


This is
genuine
preformatted
text






* 1st list item


* 2nd list item





Indenting the content does increase the size of the file, so you may prefer Tidy's default style:






para which has enough text to cause a line break,
and so test the wrapping mechanism for long lines.


This is
genuine
preformatted
text




* 1st list item


* 2nd list item







How to run tidy

tidy [[options] filename]*

HTML tidy is not (yet) a Windows program. If you run tidy without any arguments, it will just sit there waiting to read markup on the stdin stream. Tidy's input and output default to stdin and stdout respectively. Errors are written to stderr but can be redirected to a file with the -f filename option.

I generally use the -m option to get tidy to update the original file, and if the file is particularly bad I also use the -f option to write the errors to a file to make it easier to review them. Tidy supports a small set of character encoding options. The default is ASCII, which makes it easy to edit markup in regular text editors.

For instance:

tidy -f errs.txt -m index.html

which runs tidy on the file "index.html" updating it in place and writing the error messages to the file "errs.txt". Its a good idea to save your work before tidying it, as with all complex software, tidy may have bugs. If you find any please let me know!

Thanks to Jacek Niedziela, The Win32 executable for tidy is now able to use wild cards in filenames. This utilizes the setargv library supplied with VC++.

Tidy writes errors to stderr, and won't be paused by the more command. A work around is to redirect stderr to stdout as follows. This works on Unix and Windows NT, but not on other platforms. My thanks to Markus Wolf for this tip!

tidy file.html 2>&1 | more

Tidy's Options

To get a list of available options use:

tidy -help

You may want to run it through more to view the help a page at a time.

tidy -help | more

Input and Output default to stdin/stdout respectively. Single letter options apart from -f may be combined as in: tidy -f errs.txt -imu foo.html
Using a Configuration File

Tidy now supports a configuration file, and this is now much the most convenient way to configure Tidy. Assuming you have created a config file named "config.txt" (the name doesn't matter), you can instruct Tidy to use it via the command line option -config config.txt, e.g.

tidy -config config.txt file1.html file2.html

Alternatively, you can name the default config file via the environment variable named "HTML_TIDY". Note this should be the absolute path since you are likely to want to run Tidy in different directories. You can also set a config file at compile time by defining CONFIG_FILE as the path string, see platform.h.

You can now set config options on the command line by preceding the name of the option immediately (no intervening space) by "--", for example:

tidy --break-before-br true --show-warnings false

The full set of options available on recent versions of HTML Tidy can be found in this Quick Reference maintained as part of the Source Forge HTML Tidy project.
Sample Config File

This is just an example to get you started.

// sample config file for HTML tidy
indent: auto
indent-spaces: 2
wrap: 72
markup: yes
output-xml: no
input-xml: no
show-warnings: yes
numeric-entities: yes
quote-marks: yes
quote-nbsp: yes
quote-ampersand: no
break-before-br: no
uppercase-tags: no
uppercase-attributes: no
char-encoding: latin1
new-inline-tags: cfif, cfelse, math, mroot,
mrow, mi, mn, mo, msqrt, mfrac, msubsup, munderover,
munder, mover, mmultiscripts, msup, msub, mtext,
mprescripts, mtable, mtr, mtd, mth
new-blocklevel-tags: cfoutput, cfquery
new-empty-tags: cfelse

Using Tidy from scripts

If you want to run Tidy from a Perl or other scripting language you may find it of value to inspect the result returned by Tidy when it exits: 0 if everything is fine, 1 if there were warnings and 2 if there were errors. This is an example using Perl:

if (close(TIDY) == 0) {
my $exitcode = $? >> 8;
if ($exitcode == 1) {
printf STDERR "tidy issued warning messages\n";
} elsif ($exitcode == 2) {
printf STDERR "tidy issued error messages\n";
} else {
die "tidy exited with code: $exitcode\n";
}
} else {
printf STDERR "tidy detected no errors\n";
}

Source Code

The latest versions of the source code can be found at the Source Forge developer's site for Tidy, see http://tidy.sourceforge.net.
Acknowledgements

I would like to thank the many people who have written to me with suggestions for improvements or reporting bugs. Your help has been invaluable.

Jonathan Adair, Drew Adams, Osma Ahvenlampi, Carsten Allefeld, Richard Allsebrook, Jacob Sparre Andersen, Joe D'Andrea, Jerry Andrews, Bruce Aron, Takuya Asada, Edward Avis, Carlos Piqueres Ayela, Nick B, Chang Hyun Baek, Nick B, Denis Barbier, Chuck Baslock, Christer Bernerus, David J. Biesack, John Bigby, Yu Jian Bin, Alexander Biron, Keith Blakemore-Noble, Eric Blossom, Berend de Boer, Ochen M. Braun, Dave Bryan, David Brooke, Andy Brown, Keith B. Brown, Andreas Buchholz, Maurice Buxton, Jelks Cabaniss, John Cappelletti, Trevor Carden, Terry Cassidy, Mathew Cepl, Kendall Clark, Rob Clark, Jeremy Clulow, Dan Connolly, Larry Cousin, Ken Cox, Luis M. Cruz, John Cumming, Ian Davey, Keith Davies, Ciaran Deignan, David Duffy, Emma Duke-Williams, Tamminen Eero, Bodo Eing, Peter Enzerink, Baruch Even, David Fallon, Claus André Färber, Stephanie Foott, Darren Forcier, Martin Fouts, Frederik Fouvry, Rene Fritz, Stephen Fuqua, Martin Gallwey, Pete Gelbman, Francisco Guardiola, David Getchell, Michael Giroux, Davor Golek, Guus Goos, Léa Gris, Rainer Gutsche, Kai Hackemesser, Juha Häikiö, David Halliday, Johann-Christian Hanke, Vlad Harchev, Shane Harrelson, Andre Hinrichs, Bjoern Hoehrmann, G. Ken Holman, Bill Homer, Olaf Hopp, Craig Horman, Jack Horsfield, Nigel Horspool, Pao-Hsi Huang, Stuart Hungerford, Marc Jauvin, Rick Jelliffe, Peter Jeremy, Craig Johnson, Charles LaFountain, Steven Lobo, Zdenek Kabelac, Michael Kay, Jeffery Kendall, Axel Kielhorn, Konstantinos Kleisouris, Johannes Koch, Daniel Kohn, Rudy Kohut, Allan Kuchinsky, Volker Kuhlmann, Michael LaStella, Johnny Lee, Steve Lee, Tony Leneis, Nick Leverton, Todd Lewis, Dietmar Lippold, Gert-Jan C. Lokhorst, Murray Longmore, John Love-Jensen, Satwinder Mangat, Carole Mah, Anton Marsden, Bede McCall, Shane McCarron, Thomas McGuigan, Ian McKellar, Al Medeiros, Chris Nappin, Ann Navarro, Jacek Niedziela, Morten Blinksbjerg Nielsen, Kenichi Numata, Allan Odgaard, Matt Oshry, Gerald Oskoboiny, Paul Ossenbruggen, Ernst Paalvast, Christian Pantel, Dimitri Papadopoulos, Rick Parsons, Steven Pemberton, Daniel Persson, Lee Anne Phillips, Xavier Plantefeve, Karl Prinz, Andy Quick, Jany Quintard, Julian Reschke, Stephen Reynolds, Thomas Ribbrock, Ross L. Richardson, Philip Riebold, Erik Rossen, Dan Rudman, Peter Ruevski, Christian Ruetgers, Klaus Johannes Rusch, John Russell, Eric Schindler, J. Schlauch, Christian Schüler, Klaus Alexander Seistrup, Jim Seymour, Kazuyoshi Shimizu, Geoff Sinclair, Jo Smith, Paul Smith, Steve Spilker, Rafi Stern, Jacques Steyn, Michael J. Suzio, Zac Thompson, Eric Thorbjornsen, Oren Tirosh, John Tobler, Omri Traub, Loïc Trégan, Jason Tribbeck, Simon Trimmer, Steffen Ullrich, Stuart Updegrave, Charles A. Upsdell, Jussi Vestman, Larry W. Virden, Daniel Vogelheim, Nigel Wadsworth, Jez Wain, Randy Waki, Paul Ward, Neil Weber, Bertilo Wennergren, Yudong Yang, Jeff Young, Edward Zalta, Johannes Zellner, Christian Zuckschwerdt

Dave's Address

35 Frome Road
Bradford on Avon
Wiltshire
BA15 2EA
United Kingdom

Dave Raggett is a consultant with Canon, and previously worked with Openwave, and before that with Hewlett Packard's UK Laboratories. Dave works on assignment to the World Wide Web Consortium, where he is the W3C lead for Voice and Multimodal.

Copyright © 1994-2003 W3C® (MIT, ERCIM, Keio), All Rights Reserved. W3C liability, trademark, document use and software licensing rules apply. Your interactions with this site are in accordance with our public and Member privacy statements.


16 February 2007

关于 UTF8 编码

在网络中有很多地方都有采用UTF8编码,由于要编写与邮件服务端有关的程序,而邮件服务端有些地方用到了UTF8编码,所以对它有了初步的认识!

它其实和Unicode是同类,就是在编码方式上不同!
首先UTF8编码后的大小是不一定,不像Unicode编码后的大小是一样的!

我们先来看Unicode的编码:一个英文字母 “a” 和 一个汉字 “好”,编码后都是占用的空间大小是一样的,都是两个字节!

而UTF8编码:一个英文字母“a” 和 一个汉字 “好”,编码后占用的空间大小就不样了,前者是一个字节,后者是三个字节!

现在就让我们来看看UTF8编码的原理吧:

   因为一个字母还有一些键盘上的符号加起来只用二进制七位就可以表示出来,而一个字节就是八位,所以UTF8就用一个字节来表式字母和一些键盘上的符号。 然而当我们拿到被编码后的一个字节后怎么知道它的组成?它有可能是英文字母的一个字节,也有可能是汉字的三个字节中的一个字节!所以,UTF8是有标志位 的!

  当要表示的内容是 7位 的时候就用一个字节:0*******  第一个0为标志位,剩下的空间正好可以表示ASCII 0-127 的内容。

  当要表示的内容在 8 到 11 位的时候就用两个字节:110***** 10******  第一个字节的110和第二个字节的10为标志位。

  当要表示的内容在 12 到 16 位的时候就用三个字节:1110***** 10****** 10******   和上面一样,第一个字节的1110和第二、三个字节的10都是标志位,剩下的空间正好可以表示汉字。

  以此类推:

四个字节:11110**** 10****** 10****** 10******
  五个字节:111110*** 10****** 10****** 10****** 10******
  六个字节:1111110** 10****** 10****** 10****** 10****** 10******


明白了没有?
编码的方法是从低位到高位

现在就让我们来看看实例吧!

黄色为标志位
其它着色为了显示其,编码后的位置

Unicode十六进制

Unicode二进制

UTF8二进制

UTF8十六进制

UTF8字节数

B

00001011

00001010

B

1

9D

00010011101

11000010 10011101

C2 9D

2

A89E

10101000 10011110

11101010 10100010 10011110

EA A2 9E

3



另外:utf-8 的表示的unicode是有范围的,不会无限延伸。

作者Blog:http://blog.csdn.net/zhjian6/

14 February 2007

VC字符串转换

一、BSTR、LPSTR和LPWSTR

   在Visual C++.NET的所有编程方式中,我们常常要用到这样的一些基本字符串类型,如BSTR、LPSTR和LPWSTR等。之所以出现类似上述的这些数据类 型,是因为不同编程语言之间的数据交换以及对ANSI、Unicode和多字节字符集(MBCS)的支持。

  那么什么是BSTR、LPSTR以及LPWSTR呢?

  BSTR(Basic STRing,Basic字符串)是一个OLECHAR*类型的Unicode字符串。它被描述成一个与自动化相兼容的类型。由于操作系统提供相应的 API函数(如SysAllocString)来管理它以及一些默认的调度代码,因此BSTR实际上就是一个COM字符串,但它却在自动化技术以外的多种 场合下得到广泛使用。图1描述了BSTR的结构,其中DWORD值是字符串中实际所占用的字节数,且它的值是字符串中Unicode字符的两倍。

  LPSTR和LPWSTR是Win32和VC++所使用的一种字符串数据类型。LPSTR被定义成是一个指向以NULL(‘\0’)结尾的8位 ANSI字符数组指针,而LPWSTR是一个指向以NULL结尾的16位双字节字符数组指针。在VC++中,还有类似的字符串类型,如LPTSTR、 LPCTSTR等,它们的含义如图2所示。

  例如,LPCTSTR是指“long pointer to a constant generic string”,表示“一个指向一般字符串常量的长指针类型”,与C/C++的const char*相映射,而LPTSTR映射为 char*。

  一般地,还有下列类型定义:

#ifdef UNICODE
  typedef LPWSTR LPTSTR;
  typedef LPCWSTR LPCTSTR;
#else
  typedef LPSTR LPTSTR;
  typedef LPCSTR LPCTSTR;
#endif

二、CString、CStringA 和 CStringW

  Visual C++.NET中将CStringT作为ATL和MFC的共享的“一般”字符串类,它有CString、CStringA和CStringW三种形式,分 别操作不同字符类型的字符串。这些字符类型是TCHAR、char和wchar_t。TCHAR在Unicode平台中等同于WCHAR(16位 Unicode字符),在ANSI中等价于char。wchar_t通常定义为unsigned short。由于CString在MFC应用程序中经常用到,这里不再重复。

三、VARIANT、COleVariant 和_variant_t

  在OLE、ActiveX和COM中,VARIANT数据类型提供了一种非常有效的机制,由于它既包含了数据本身,也包含了数据的类型,因而它可以实现各种不同的自动化数据的传输。下面让我们来看看OAIDL.H文件中VARIANT定义的一个简化版:

struct tagVARIANT {
  VARTYPE vt;
  union {
   short iVal; // VT_I2.
   long lVal; // VT_I4.
   float fltVal; // VT_R4.
   double dblVal; // VT_R8.
   DATE date; // VT_DATE.
   BSTR bstrVal; // VT_BSTR.
   …
   short * piVal; // VT_BYREF|VT_I2.
   long * plVal; // VT_BYREF|VT_I4.
   float * pfltVal; // VT_BYREF|VT_R4.
   double * pdblVal; // VT_BYREF|VT_R8.
   DATE * pdate; // VT_BYREF|VT_DATE.
   BSTR * pbstrVal; // VT_BYREF|VT_BSTR.
  };
};

  显然,VARIANT类型是一个C结构,它包含了一个类型成员vt、一些保留字节以及一个大的union类型。例如,如果vt为VT_I2,那么我们可以从iVal中读出VARIANT的值。同样,当给一个VARIANT变量赋值时,也要先指明其类型。例如:

VARIANT va;
:: VariantInit(&va); // 初始化
int a = 2002;
va.vt = VT_I4; // 指明long数据类型
va.lVal = a; // 赋值

  为了方便处理VARIANT类型的变量,Windows还提供了这样一些非常有用的函数:

  VariantInit —— 将变量初始化为VT_EMPTY;

  VariantClear —— 消除并初始化VARIANT;

  VariantChangeType —— 改变VARIANT的类型;

  VariantCopy —— 释放与目标VARIANT相连的内存并复制源VARIANT。

  COleVariant类是对VARIANT结构的封装。它的构造函数具有极为强大大的功能,当对象构造时首先调用VariantInit进行 初始化,然后根据参数中的标准类型调用相应的构造函数,并使用VariantCopy进行转换赋值操作,当VARIANT对象不在有效范围时,它的析构函 数就会被自动调用,由于析构函数调用了VariantClear,因而相应的内存就会被自动清除。除此之外,COleVariant的赋值操作符在与 VARIANT类型转换中为我们提供极大的方便。例如下面的代码:

COleVariant v1("This is a test"); // 直接构造
COleVariant v2 = "This is a test";
// 结果是VT_BSTR类型,值为"This is a test"
COleVariant v3((long)2002);
COleVariant v4 = (long)2002;
// 结果是VT_I4类型,值为2002

  _variant_t是一个用于COM的VARIANT类,它的功能与COleVariant相似。不过在Visual C++.NET的MFC应用程序中使用时需要在代码文件前面添加下列两句:

  #include "comutil.h"

  #pragma comment( lib, "comsupp.lib" )

四、CComBSTR和_bstr_t

  CComBSTR是对BSTR数据类型封装的一个ATL类,它的操作比较方便。例如:

CComBSTR bstr1;
bstr1 = "Bye"; // 直接赋值
OLECHAR* str = OLESTR("ta ta"); // 长度为5的宽字符
CComBSTR bstr2(wcslen(str)); // 定义长度为5
wcscpy(bstr2.m_str, str); // 将宽字符串复制到BSTR中
CComBSTR bstr3(5, OLESTR("Hello World"));
CComBSTR bstr4(5, "Hello World");
CComBSTR bstr5(OLESTR("Hey there"));
CComBSTR bstr6("Hey there");
CComBSTR bstr7(bstr6);
// 构造时复制,内容为"Hey there"

  _bstr_t是是C++对BSTR的封装,它的构造和析构函数分别调用SysAllocString和SysFreeString函数,其他操作是借用BSTR API函数。与_variant_t相似,使用时也要添加comutil.h和comsupp.lib。

五、BSTR、char*和CString转换

  (1) char*转换成CString

  若将char*转换成CString,除了直接赋值外,还可使用CString::Format进行。例如:

char chArray[] = "This is a test";
char * p = "This is a test";

  或

LPSTR p = "This is a test";

  或在已定义Unicode应的用程序中

TCHAR * p = _T("This is a test");

  或

LPTSTR p = _T("This is a test");
CString theString = chArray;
theString.Format(_T("%s"), chArray);
theString = p;

  (2) CString转换成char*

  若将CString类转换成char*(LPSTR)类型,常常使用下列三种方法:

  方法一,使用强制转换。例如:

CString theString( "This is a test" );
LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;

  方法二,使用strcpy。例如:

CString theString( "This is a test" );
LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
_tcscpy(lpsz, theString);

  需要说明的是,strcpy(或可移值Unicode/MBCS的_tcscpy)的第二个参数是 const wchar_t* (Unicode)或const char* (ANSI),系统编译器将会自动对其进行转换。

  方法三,使用CString::GetBuffer。例如:

CString s(_T("This is a test "));
LPTSTR p = s.GetBuffer();
// 在这里添加使用p的代码
if(p != NULL) *p = _T('\0');
s.ReleaseBuffer();
// 使用完后及时释放,以便能使用其它的CString成员函数

  (3) BSTR转换成char*

  方法一,使用ConvertBSTRToString。例如:

#include
#pragma comment(lib, "comsupp.lib")
int _tmain(int argc, _TCHAR* argv[]){
BSTR bstrText = ::SysAllocString(L"Test");
char* lpszText2 = _com_util::ConvertBSTRToString(bstrText);
SysFreeString(bstrText); // 用完释放
delete[] lpszText2;
return 0;
}

  方法二,使用_bstr_t的赋值运算符重载。例如:

_bstr_t b = bstrText;
char* lpszText2 = b;

  (4) char*转换成BSTR

  方法一,使用SysAllocString等API函数。例如:

BSTR bstrText = ::SysAllocString(L"Test");
BSTR bstrText = ::SysAllocStringLen(L"Test",4);
BSTR bstrText = ::SysAllocStringByteLen("Test",4);

  方法二,使用COleVariant或_variant_t。例如:

//COleVariant strVar("This is a test");
_variant_t strVar("This is a test");
BSTR bstrText = strVar.bstrVal;

  方法三,使用_bstr_t,这是一种最简单的方法。例如:

BSTR bstrText = _bstr_t("This is a test");

  方法四,使用CComBSTR。例如:

BSTR bstrText = CComBSTR("This is a test");

  或

CComBSTR bstr("This is a test");
BSTR bstrText = bstr.m_str;

  方法五,使用ConvertStringToBSTR。例如:

char* lpszText = "Test";
BSTR bstrText = _com_util::ConvertStringToBSTR(lpszText);

  (5) CString转换成BSTR

  通常是通过使用CStringT::AllocSysString来实现。例如:

CString str("This is a test");
BSTR bstrText = str.AllocSysString();

SysFreeString(bstrText); // 用完释放

  (6) BSTR转换成CString

  一般可按下列方法进行:

BSTR bstrText = ::SysAllocString(L"Test");
CStringA str;
str.Empty();
str = bstrText;

  或

CStringA str(bstrText);

  (7) ANSI、Unicode和宽字符之间的转换

  方法一,使用MultiByteToWideChar将ANSI字符转换成Unicode字符,使用WideCharToMultiByte将Unicode字符转换成ANSI字符。

  方法二,使用“_T”将ANSI转换成“一般”类型字符串,使用“L”将ANSI转换成Unicode,而在托管C++环境中还可使用S将ANSI字符串转换成String*对象。例如:

TCHAR tstr[] = _T("this is a test");
wchar_t wszStr[] = L"This is a test";
String* str = S”This is a test”;

  方法三,使用ATL 7.0的转换宏和类。ATL7.0在原有3.0基础上完善和增加了许多字符串转换宏以及提供相应的类,它具有如图3所示的统一形式:

  其中,第一个C表示“类”,以便于ATL 3.0宏相区别,第二个C表示常量,2表示“to”,EX表示要开辟一定大小的缓冲。SourceType和DestinationType可以是A、 T、W和OLE,其含义分别是ANSI、Unicode、“一般”类型和OLE字符串。例如,CA2CT就是将ANSI转换成一般类型的字符串常量。下面 是一些示例代码:

LPTSTR tstr= CA2TEX<16>("this is a test");
LPCTSTR tcstr= CA2CT("this is a test");
wchar_t wszStr[] = L"This is a test";
char* chstr = CW2A(wszStr);

Google