Posted by bianbian on 2008-06-19 08:08
本文Tags: bug, FTP, ftp4j, 解决
上次推荐的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 了。嘿嘿嘿嘿。。。
修正后的代码(省略后面没有变化的部分):
- public class UnixListParser implements FTPListParser {
- // bianbian.org: Pattern有问题,修正
- private static final Pattern PATTERN = Pattern
- .compile("^([dlcbsp\\-])[r\\-][w\\-][xsS\\-][r\\-][w\\-][xsS\\-][r\\-][w\\-][xtT\\-]\\s+"
- + "(?:\\d+\\s+)?\\S+\\s*\\S+\\s+(\\d+)\\s+(?:(\\w{3})\\s+(\\d{1,2}))\\s+"
- + "(?:(\\d{4})|(?:(\\d{1,2}):(\\d{1,2})))\\s+"
- + "([^\\\\/*?\"<>|]+)(?: -> ([^\\\\*?\"<>|]+))?$");
- private static final DateFormat DATE_FORMAT = new SimpleDateFormat(
- "MMM dd yyyy HH:mm", Locale.US);
- public FTPFile[] parse(String[] lines) throws FTPListParseException {
- int currentYear = new GregorianCalendar().get(Calendar.YEAR);
- int i, jump = 0, size = lines.length;
- //bianbian.org: glftpd 的第一行和 ls -l 一样,会丢个"total N"过来,先去掉
- if (size > 0 && lines[0].startsWith("total")) {
- size--;
- jump = 1;
- }
- FTPFile[] ret = new FTPFile[size];
- for (i = 0; i < size; i++) {
- Matcher m = PATTERN.matcher(lines[i + jump]);
- if (m.matches()) {
- ret[i] = new FTPFile();
- // Retrieve the data.
- char typeChar = m.group(1).charAt(0);
- String sizeString = m.group(2);
- String monthString = m.group(3);
- String dayString = m.group(4);
- String yearString = m.group(5);
- String hourString = m.group(6);
- String minuteString = m.group(7);
- String nameString = m.group(8);
- String linkedString = m.group(9);
- // Parse the data.
- if ("-cbsp".indexOf(typeChar) > -1) {
- ret[i].setType(FTPFile.TYPE_FILE);
- } else if (typeChar == 'd') {
- ret[i].setType(FTPFile.TYPE_DIRECTORY);
- } else if (typeChar == 'l') {
- ret[i].setType(FTPFile.TYPE_LINK);
- ret[i].setLink(linkedString);
- } else {
- throw new FTPListParseException();
- }
- ...
标签:
bug,
FTP,
ftp4j,
解决遵守创作共用协议,转载请链接形式注明来自
http://bianbian.org 做人要厚道
相关日志
Posted in Java, Technology | 3 Comments »
Posted by bianbian on 2008-04-20 03:43
本文Tags: JavaScript, 正则表达式, 解决
发信人: Net (BBS上没有什么事情是bg不能解决的), 信区: WebDesign
标 题: Re: 如何使用正则分别替换?
发信站: 南京大学小百合站 (Sun Apr 20 15:21:23 2008)
不太可能一句话搞定,不过有相对而言的解决办法:
1) 简单易懂
- function arrayReplace(str, from, to) {
- for(var i=0; i < from.length; i++)
- str = str.replace(new RegExp(from[i], "g"), to[i]);
- return str;
- }
- var from = ["A", "B", "C"], to = ["asdf", "fdsa", "......"];
- str = arrayReplace(str, from, to);
2) 勉强算一句话吧。。。
- str = str.replace(/(A|B|C)/g, function ($0, $1) {
- return {"A": "asdf", "B": "fdsa", "C": "......"}[$1]
- });
【 在 superphoenix (格云朱雀) 的大作中提到: 】
: 比如要将一个字符串中的A替换为asdf,B替换成fdsa,C替换成……
: 能不能用一个正则话就替换成功?
: 而不是写成str=str.replace(/A/g,”asdf”).replace(/B/g,”fdsa”)….
: 谢谢!
※ 来源:.南京大学小百合站 bbs.nju.edu.cn.[FROM: Net.nEt.neT.Orz]
标签:
JavaScript,
正则表达式,
解决遵守创作共用协议,转载请链接形式注明来自
http://bianbian.org 做人要厚道
相关日志
Posted in JavaScript, Technology | No Comments »
Posted by bianbian on 2008-04-20 01:08
本文Tags: binary, bug, pack, php, struct, unpack, 二进制, 数据, 解决
尽管php是用C语言开发的,不过令我不解的是php没有提供对结构体struct的直接支持。
不过php提供了pack和unpack函数,用来进行二进制数据(binary data)和php内部数据的互转:
- string pack ( string $format [, mixed $args [, mixed $...]] )
- //Pack given arguments into binary string according to format.
-
- array unpack ( string $format, string $data )
- //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截取:
- $name = substr($name, 0, strpos($name, "\0"));
不过很Faint的事情发生了,不知道是strpos的bug还是substr的bug(其实测试一下就知道,懒得试),有些字符串没问题,有些字符串却只能得到空值(即$name == ”)。很是郁闷,后来找了个strtok函数,这下没有问题了:
- $name = strtok($name, "\0");
难为大家看了那么多,下面写个完整的php读取二进制数据流(C语言结构体struct数据)文件的示例代码:
首先是C的struct定义示例,为了演示,我就写个简单点的,实际对照上面那个$format格式表应该没有问题:
- struct BIANBIAN {
- char name[10];
- char pass[33];
- int age;
- unsigned char flag;
- };
比如有个“bianbian.org”文件,内容就是上面的N个BIANBIAN结构体构成的。读取的php代码:
- //下面根据struct确定$format,注意int类型跟机器环境有关,我的32位Linux是4个长度
- $format = 'a10name/a33pass/iage/Cflag';
- //确定一个struct占用多少长度字节,如果只是读取单个结构体这是不需要的
- $length = 10 + 33 + 4 + 1;
- //也可以用fopen + fread + fclose,不过file_get_contents因为可以mmap,效率更高
- $data = file_get_contents('bianbian.org', 'r');
- for ($i = 0, $c = strlen($data); $i < $c; $i += $length) {
- $bianbian = unpack("@$i/$format", $data);
- //reference传递是php 5才支持的,如果用php4,得用其他办法
- foreach ($bianbian as &$value) {
- if (is_string($value)) {
- $value = strtok($value, "\0");
- }
- }
- print_r($bianbian);
- }
- //输出为array,即类似:
- Array
- (
- [name] => 'bianbian'
- [pass] => 'bianbian.org'
- [age] => 100
- [flag] => 0
- )
- ...
pack应该跟unpack相反。
标签:
binary,
bug,
pack,
php,
struct,
unpack,
二进制,
数据,
解决遵守创作共用协议,转载请链接形式注明来自
http://bianbian.org 做人要厚道
相关日志
Posted in Technology, php | 4 Comments »
Posted by bianbian on 2008-04-09 09:16
本文Tags: php, UTF-8, 乱码, 解决
php 5的流读取函数好像默认编码是UTF-8,以前在php 4里直接file_get_contents()读取gb2312编码的正常,到了5就乱码了。网上的解决办法说抓取后用iconv()转码。看后我就觉得不对劲:一个是不一定编译了iconv库,更大的问题是编码都跟流转换的时候有关(如果用了iconv实际上php转了两次码:流 -> UTF-8 -> GB2312):这不是白忙乎了吗?
仔细看了下php的文档(不知道大家都是怎么写代码的,其实文档上很清楚啊),上面关于fopen()及file_get_contents()都提到了“默认是UTF-8,但是用户可以用stream_default_encoding()或者用户自定义上下文属性改变编码”(If unicode semantics are enabled, the default encoding of the read data is UTF-8. You can specify a different encoding by creating a custom context or by changing the default using stream_default_encoding().)。于是用stream_default_encoding(’gb2312′);测试:但是faint的是,这个函数不存在?!似乎php 6才支持。不过天无绝人之路,还有“用户自定义上下文属性”可以用。
经过更仔细的看文档,最后解决了这个问题:
- //设置流的编码格式,这是文件流(file),如果是网络访问,file改成http
- $opts = array('file' => array('encoding' => 'gb2312'));
- $ctxt = stream_context_create($opts);
- file_get_contents(文件名, FILE_TEXT, $ctxt);
标签:
php,
UTF-8,
乱码,
解决遵守创作共用协议,转载请链接形式注明来自
http://bianbian.org 做人要厚道
相关日志
Posted in Technology, php | No Comments »
Posted by bianbian on 2008-02-05 04:33
本文Tags: 解决, 饮水机
前几天受大雪影响,家里饮水机断水也没人送水。
于是到超市买水,回来后发现:哈哈哈哈

真是太有才了….
标签:
解决,
饮水机遵守创作共用协议,转载请链接形式注明来自
http://bianbian.org 做人要厚道
相关日志
Posted in Not IT | 2 Comments »