2009年2月12日星期四

trick in scanf

libjingle has a very tricky use of scanf and printf in httpcommon.cc

"%.*s" used in printf and "%*s%n %n%*s%n" used in scanf.

no offical explainations found so far.But you can figure out what they mean if you run the following exmaple code.

#include

int main()
{
  char path[] = "/test.html";
  printf("%s %.*s HTTP/%s\n", "Get", sizeof(path),path, "1.1");
 
  char line[256];
  sprintf(line,"%s %.*s HTTP/%s", "Get", sizeof(path),path, "1.1");
 
  int vmajor, vminor;
  int vend, dstart, dend;
  int ret = sscanf(line, "%*s%n %n%*s%n HTTP/%lu.%lu", &vend, &dstart, &dend, &vmajor, &vminor);
  
  printf("result: %d ",ret);
  printf("vend = %d ", vend);
  printf("dstart = %d ", dstart);
  printf("dend = %d ", dend);
  printf("vmajor = %d ", vmajor);
  printf("vminor = %d ", vminor);
  
 system("pause");
}


Results :

Get /test.html HTTP/1.1
result: 2 vend = 3 dstart = 4 dend = 14 vmajor = 1 vminor = 1 请按任意键继续. .

2009年2月1日星期日

LONG UNC (UNCW) used by libjingle

referring to wikipedia,

"The types of paths in Windows are local file system (LFS), such as C:\File, uniform naming convention (UNC), such as \\Server\Volume\File, and Long UNC or UNCW, such as \\?\C:\File or \\?\UNC\Server\Volume\File.
In earlier versions of Windows only the APIs that accept "Long UNC" were able to accept more than 260 characters."

We often use LFS in Windows, and sometimes,we use UNC to reach the share folder of LAN, Long UNC and seldomly used because the only use maybe in the earlier versions API, such as in _wfopen, to eliminate the limitation of 260 characters of filepath.


ref:
Naming a File or Directory