bianbian coding life

便便代码人生: 关注技术, 翻译文档, 偶尔动动手

[原] ftp4j的解析list的bug及解决

Posted by bianbian on 2008-06-19 08:08


本文Tags: , , ,

上次推荐的ftp4j在解析部分FTP站点的目录list的时候遇到了FTPListParseException(也怪FTP协议没有对LIST格式作出标准)。查看源码发现,主要是两个问题:
1)文件权限不只rwx这三个,附加了s、t(详见http://en.wikipedia.org/wiki/File_system_permissions
2)部分ftpd似乎直接调用的“ls -l”输出目录,第一行是“total xxx”
给作者写信了,说不定下个版本就有Sepcial Thanks to bianbian 了。嘿嘿嘿嘿。。。
修正后的代码(省略后面没有变化的部分):

  1. public class UnixListParser implements FTPListParser {
  2.     // bianbian.org: Pattern有问题,修正
  3.     private static final Pattern PATTERN = Pattern
  4.             .compile("^([dlcbsp\\-])[r\\-][w\\-][xsS\\-][r\\-][w\\-][xsS\\-][r\\-][w\\-][xtT\\-]\\s+"
  5.                     + "(?:\\d+\\s+)?\\S+\\s*\\S+\\s+(\\d+)\\s+(?:(\\w{3})\\s+(\\d{1,2}))\\s+"
  6.                     + "(?:(\\d{4})|(?:(\\d{1,2}):(\\d{1,2})))\\s+"
  7.                     + "([^\\\\/*?\"<>|]+)(?: -> ([^\\\\*?\"<>|]+))?$");
  8.     private static final DateFormat DATE_FORMAT = new SimpleDateFormat(
  9.             "MMM dd yyyy HH:mm", Locale.US);
  10.     public FTPFile[] parse(String[] lines) throws FTPListParseException {
  11.         int currentYear = new GregorianCalendar().get(Calendar.YEAR);
  12.         int i, jump = 0, size = lines.length;
  13.         //bianbian.org: glftpd 的第一行和 ls -l 一样,会丢个"total N"过来,先去掉
  14.         if (size > 0 && lines[0].startsWith("total")) {
  15.             size--;
  16.             jump = 1;
  17.         }
  18.         FTPFile[] ret = new FTPFile[size];
  19.         for (i = 0; i < size; i++) {
  20.             Matcher m = PATTERN.matcher(lines[i + jump]);
  21.             if (m.matches()) {
  22.                 ret[i] = new FTPFile();
  23.                 // Retrieve the data.
  24.                 char typeChar = m.group(1).charAt(0);
  25.                 String sizeString = m.group(2);
  26.                 String monthString = m.group(3);
  27.                 String dayString = m.group(4);
  28.                 String yearString = m.group(5);
  29.                 String hourString = m.group(6);
  30.                 String minuteString = m.group(7);
  31.                 String nameString = m.group(8);
  32.                 String linkedString = m.group(9);
  33.                 // Parse the data.
  34.                 if ("-cbsp".indexOf(typeChar) > -1) {
  35.                     ret[i].setType(FTPFile.TYPE_FILE);
  36.                 } else if (typeChar == 'd') {
  37.                     ret[i].setType(FTPFile.TYPE_DIRECTORY);
  38.                 } else if (typeChar == 'l') {
  39.                     ret[i].setType(FTPFile.TYPE_LINK);
  40.                     ret[i].setLink(linkedString);
  41.                 } else {
  42.                     throw new FTPListParseException();
  43.                 }
  44.     ...
标签: , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in Java, Technology | 3 Comments »

[原] php读取二进制流(C语言结构体struct数据文件)

Posted by bianbian on 2008-04-20 01:08


本文Tags: , , , , , , , ,

尽管php是用C语言开发的,不过令我不解的是php没有提供对结构体struct的直接支持。
不过php提供了pack和unpack函数,用来进行二进制数据(binary data)和php内部数据的互转:

  1. string pack ( string $format [, mixed $args [, mixed $...]] )
  2. //Pack given arguments into binary string according to format.
  3.  
  4. array unpack ( string $format, string $data )
  5. //Unpacks from a binary string into an array according to the given format.

其中,$format跟perl里的pack格式类似,有如下一些(中文是我加的,有不准确的欢迎提出):
a NUL-padded string,即“\0”作为“空字符”的表示形式
A SPACE-padded string,空格作为“空字符”的表示形式
h Hex string, low nibble first,升序位顺序
H Hex string, high nibble first,降序位顺序
c signed char,有符号单字节
C unsigned char,无符号单字节
s signed short (always 16 bit, machine byte order)
S unsigned short (always 16 bit, machine byte order)
n unsigned short (always 16 bit, big endian byte order)
v unsigned short (always 16 bit, little endian byte order)
i signed integer (machine dependent size and byte order)
I unsigned integer (machine dependent size and byte order)
l signed long (always 32 bit, machine byte order)
L unsigned long (always 32 bit, machine byte order)
N unsigned long (always 32 bit, big endian byte order)
V unsigned long (always 32 bit, little endian byte order)
f float (machine dependent size and representation)
d double (machine dependent size and representation)
x NUL byte,实际使用的时候作为跳过多少字节用,很有用
X Back up one byte,后退1字节
@ NUL-fill to absolute position,实际使用的时候作为从开头跳到某字节用,很有用

实际使用发现:C里的“\0”(即字符串终止符)在php里并不是终止符,而是作为了字符串的一部分。因此,必须对“\0”进行特殊处理,才能进行struct和php内部数据的完美互转。比如 char name[10]; 如果实际数据是“62 69 61 6E 00 62 69 61 6E 00”,在C语言里第5个位置有终止符,name应该是“bian”;而用了unpack转换以后在php里的name却是“bian\0bian\0”。
一开始我用了strpos函数找到“\0”的位置,然后进行substr截取:

  1. $name = substr($name, 0, strpos($name, "\0"));

不过很Faint的事情发生了,不知道是strpos的bug还是substr的bug(其实测试一下就知道,懒得试),有些字符串没问题,有些字符串却只能得到空值(即$name == ”)。很是郁闷,后来找了个strtok函数,这下没有问题了:

  1. $name = strtok($name, "\0");

难为大家看了那么多,下面写个完整的php读取二进制数据流(C语言结构体struct数据)文件的示例代码:
首先是C的struct定义示例,为了演示,我就写个简单点的,实际对照上面那个$format格式表应该没有问题:

  1. struct BIANBIAN {
  2.     char name[10];
  3.     char pass[33];
  4.     int  age;
  5.     unsigned char flag;
  6. };

比如有个“bianbian.org”文件,内容就是上面的N个BIANBIAN结构体构成的。读取的php代码:

  1. //下面根据struct确定$format,注意int类型跟机器环境有关,我的32位Linux是4个长度
  2. $format = 'a10name/a33pass/iage/Cflag';
  3. //确定一个struct占用多少长度字节,如果只是读取单个结构体这是不需要的
  4. $length = 10 + 33 + 4 + 1;
  5. //也可以用fopen + fread + fclose,不过file_get_contents因为可以mmap,效率更高
  6. $data = file_get_contents('bianbian.org', 'r');
  7. for ($i = 0, $c = strlen($data); $i < $c; $i += $length) {
  8.     $bianbian = unpack("@$i/$format", $data);
  9.     //reference传递是php 5才支持的,如果用php4,得用其他办法
  10.     foreach ($bianbian as &$value) {
  11.         if (is_string($value)) {
  12.             $value = strtok($value, "\0");
  13.         }
  14.     }
  15.     print_r($bianbian);
  16. }
  17. //输出为array,即类似:
  18. Array
  19. (
  20.     [name] => 'bianbian'
  21.     [pass] => 'bianbian.org'
  22.     [age]  => 100
  23.     [flag] => 0
  24. )
  25. ...

pack应该跟unpack相反。

标签: , , , , , , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in Technology, php | 4 Comments »

[原] IDL剪贴板绘图在其他语言调用下失效的bug

Posted by bianbian on 2008-03-26 02:32


本文Tags: , , ,

经过运行剪贴板监视器程序,我确认这是IDL的bug:如果用VC等其他语言调用IDL绘图,IDL并不能通过IDLgrClipboard对象将绘图结果保存到系统剪贴板(而在IDL的编辑器环境下运行,剪贴板是能得到绘图结果的)。
下面是英文描述:
IDLgrClipboard not works in callable mode (I use VC++ to execute IDL procedure to draw some plots, and copy the result to system clipboard). After running a system-clipboard-viewer-tool, I conclude it must be a bug (I tested with IDL6.2, IDL7.0).

oClipbrd = OBJ_NEW(’IDLgrClipboard’, DIMENSIONS=windowSize)
oClipbrd->Draw, oView

It works well under IDL-workbench, but when call from VC++, everything else goes OK except clipboard is empty.

解决办法:只好制定FILENAME参数,将结果保存到文件:
Finally, I have no choise but to save the drawing to a file, and it works:

oClipbrd->Draw, oView, FILENAME=’tempout.bmp’

标签: , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, ENVI & IDL, Technology | No Comments »

[原] 也许是prototype框架的bug

Posted by bianbian on 2008-03-08 08:03


本文Tags: , , , ,

不知道怎么给prototype提交bug report,就写在这里吧:
其实我没有测试过,只是看prototype源码的时候,觉得是个bug:
对String的原型(prototype)添加toJSON()函数只是转义了引号,未转义”\”:

  1. if (useDoubleQuotes) return '"' + escapedString.replace(/"/g, '\\"') + '"';
  2. return "'" + escapedString.replace(/'/g, '\\\'') + "'";

如果用户在input里输入\\,转成string应该是”\\\\”,如果没有转义,再写回去的时候就丢了一个\
应该在转义引号前先转义\:

  1. .replace(/\\/g, '\\\\').replace(/"/g, '\\"')
标签: , , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in JavaScript, Technology | No Comments »

[晕] 仙剑4 bug,走到这个点绝对走不出来了

Posted by bianbian on 2008-02-27 04:54


本文Tags: , ,

被翅膀跟尾巴挡住,绝对挂了:
pal4.jpg
要是存档在一万年前,你就哭吧。。。

标签: , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in Entertainment, Not IT | 3 Comments »