我知道libexpect,但它的来源是巨大的,需要TCL。我希望能找到像ruby's'expect.rb'这样小的文件。有任何想法吗?有没有像Ruby的'expect.rb',但对于Objective C?
回答
解决了它。我根本没有运用libexpect。相反,我刚刚使用CocoaOniguruma将rubys'expect.rb'移植到objective-c。随意使用它,只要你喜欢。
/*
NSFileHandle+Expect.h
direct port of rubys 'expect.rb' to objective c
by Simon Strandgaard on 26/04/11.
public domain or BSD license
requires CocoaOniguruma
http://limechat.net/cocoaoniguruma/
*/
#import <Foundation/Foundation.h>
@class ExpectResult;
@interface NSFileHandle (Expect)
/*
wait for activity on the file descriptor.
stops waiting if it takes longer than X seconds.
*/
-(BOOL)waitForData:(float)seconds;
/*
buffer data on the filedescriptor until it matches the specified pattern.
*/
-(ExpectResult*)expect:(NSString*)pattern timeout:(float)seconds debug:(BOOL)debug;
/*
write to filedescriptor
*/
-(void)writeAsciiString:(NSString*)s;
@end
/*
NSFileHandle+Expect.m
direct port of rubys 'expect.rb' to objective c
by Simon Strandgaard on 26/04/11.
public domain or BSD license
requires CocoaOniguruma
http://limechat.net/cocoaoniguruma/
*/
#import "NSFileHandle+Expect.h"
#import "OnigRegexp.h"
#import "ExpectResult.h"
@implementation NSFileHandle (Expect)
-(BOOL)waitForData:(float)seconds {
struct timeval t;
t.tv_sec = (int)seconds;
float remain = seconds - t.tv_sec;
t.tv_usec = (int)(remain * 1000000);
int fd = [self fileDescriptor];
fd_set ready;
FD_ZERO(&ready);
FD_SET((unsigned int)fd, &ready);
int res = select(fd+1, &ready, NULL, NULL, &t);
if(res == 0) {
return NO; // timeout
}
if(FD_ISSET(fd, &ready)) {
return YES; // we have data, one or more bytes is ready
}
return NO; // error
}
-(ExpectResult*)expect:(NSString*)pattern timeout:(float)seconds debug:(BOOL)debug {
OnigRegexp* regexp = [OnigRegexp compile:pattern];
NSMutableString* buffer = [NSMutableString stringWithCapacity:100];
ExpectResult* result = nil;
while(1) {
// wait until 1 byte is ready
if(![self waitForData:seconds]) {
// timeout or error
result = nil;
break;
}
// read out the byte and append it to the buffer
NSData* char_data = [self readDataOfLength:1];
NSString* char_string = [[NSString alloc] initWithData:char_data encoding: NSASCIIStringEncoding];
[buffer appendString:char_string];
if(debug) {
NSLog(@"%s %@", _cmd, char_string);
}
[char_string release];
// see if the new buffer now satisfies the pattern
OnigResult* r = [regexp search:buffer];
if(r) {
result = [[[ExpectResult alloc] init] autorelease];
result.bufferString = [NSString stringWithString:buffer];
result.onigResult = r;
break;
}
}
return result;
}
-(void)writeAsciiString:(NSString*)s {
[self writeData:[s dataUsingEncoding:NSASCIIStringEncoding]];
}
@end
/*
ExpectResult.h
direct port of rubys 'expect.rb' to objective c
by Simon Strandgaard on 26/04/11.
public domain or BSD license
requires CocoaOniguruma
http://limechat.net/cocoaoniguruma/
*/
#import <Foundation/Foundation.h>
@class OnigResult;
@interface ExpectResult : NSObject {
NSString* m_buffer_string;
OnigResult* m_onig_result;
}
@property (nonatomic, retain) NSString* bufferString;
@property (nonatomic, retain) OnigResult* onigResult;
@end
/*
ExpectResult.h
direct port of rubys 'expect.rb' to objective c
by Simon Strandgaard on 26/04/11.
public domain or BSD license
requires CocoaOniguruma
http://limechat.net/cocoaoniguruma/
*/
#import "ExpectResult.h"
#import "OnigRegexp.h"
@implementation ExpectResult
@synthesize bufferString = m_buffer_string;
@synthesize onigResult = m_onig_result;
-(void)dealloc {
self.bufferString = nil;
self.onigResult = nil;
[super dealloc];
}
@end
NSArray* arguments = [NSArray arrayWithObject:@"ftp.ruby-lang.org"];
NSTask* task = [[[NSTask alloc] init] autorelease];
[task setLaunchPath:@"/usr/bin/ftp"];
NSPipe* readPipe = [NSPipe pipe];
NSPipe* writePipe = [NSPipe pipe];
[task setStandardInput: writePipe];
[task setStandardOutput: readPipe];
[task setArguments:arguments];
[task launch];
NSFileHandle* readHandle = [readPipe fileHandleForReading];
NSFileHandle* writeHandle = [writePipe fileHandleForWriting];
{
NSString* pattern = @"^Name.*: ";
[readHandle expect:pattern timeout:5 debug:YES];
[writeHandle writeAsciiString:@"ftp\n"];
}
{
NSString* pattern = @"word:";
[readHandle expect:pattern timeout:5 debug:YES];
[writeHandle writeAsciiString:@"[email protected]\n"];
}
{
NSString* pattern = @"> ";
[readHandle expect:pattern timeout:5 debug:YES];
[writeHandle writeAsciiString:@"cd pub/ruby\n"];
}
{
NSString* pattern = @"> ";
[readHandle expect:pattern timeout:5 debug:YES];
[writeHandle writeAsciiString:@"dir\n"];
}
{
NSString* pattern = @"> ";
ExpectResult* er = [readHandle expect:pattern timeout:5 debug:YES];
NSLog(@"%s versions: %@", _cmd, er.bufferString);
[writeHandle writeAsciiString:@"quit\n"];
}
output:
drwxrwxr-x 2 0 103 4096 Jul 06 2009 1.0
drwxrwxr-x 2 0 103 4096 Aug 04 2003 1.1a
drwxrwxr-x 2 0 103 4096 Jul 16 1998 1.1b
drwxrwxr-x 2 0 103 4096 Jan 18 1999 1.1c
drwxrwxr-x 2 0 103 54 Dec 25 1998 1.1d
drwxrwxr-x 2 0 103 4096 Sep 18 1999 1.2
drwxrwxr-x 2 0 103 4096 Sep 18 1999 1.3
drwxrwxr-x 2 0 103 4096 Apr 05 2001 1.4
drwxrwxr-x 2 0 103 4096 Sep 20 2005 1.6
drwxrwxr-x 2 0 103 8192 Feb 18 12:49 1.8
drwxrwxr-x 2 0 103 4096 Feb 18 13:39 1.9
drwxrwxr-t 6 0 103 89 Jun 15 2004 binaries
drwxrwxr-x 2 1027 100 12288 Apr 05 15:12 doc
lrwxrwxrwx 1 1023 100 27 Sep 23 2010 ruby-1.8.6-p420.tar.bz2 ->
您可能会考虑将macruby用于该项目。
没有解决方案。我正在研究目标c中的文件管理器。我没有为文件管理器编写脚本。如果它是一个脚本,我肯定会使用红宝石。 btw:文件管理器的单元测试使用ruby,并使用expect.rb。 – neoneye 2011-04-23 21:14:11
不是脚本[链接到文章](http://developer.apple.com/library/mac/#featuredarticles/UsingMacRuby/_index.html):“使用Ruby编写Cocoa应用程序不仅是可能的,但也很容易,性能,这要归功于MacRuby及其与Mac OS X核心技术的紧密集成“ – seph 2011-04-24 11:39:17
可能的展示瓶塞:需要Snow Leopard,尚未1.0(几乎) – seph 2011-04-24 11:49:40
- 1. 有没有类似于Objective-c的AutoMapper?
- 2. 有没有像ANTS性能分析器,但对于C++?
- 3. 有没有setTimeout(像)在objective-c
- 4. 有没有C#等价于Ruby的`respond_to?`?
- 5. 有没有像Ruby的bpython?
- 6. Objective-C中有没有像SHA1CryptoServiceProvider(即C#)的东西?
- 7. 有没有像Ext JS那样的库,但对于C#,特别是WPF?
- 8. 的Objective-C:对于选择没有已知的类方法initWithRequest
- 9. Java中有没有像Objective-C类别的东西?
- 10. 有没有Objective-C的web框架?
- 11. 有没有Cassandra的Objective-C客户端?
- 12. 有没有像Ruby on Rails的Grails taglibs?
- 13. Python有没有像Ruby的雾?
- 14. 有没有像GetDlgItemInt但是对于已经创建的窗口? Win32 Api
- 15. 有点像phpMyAdmin,但对于SQL Server?
- 16. 在.NET/C#中有没有类似于Ruby的Or-Equals?
- 17. 对于在Objective-C
- 18. 有没有办法将b2Body投射到Objective-C对象
- 19. 对于Grails来说有没有Ruby的宝石?
- 20. 真的有像Objective C++的东西吗?
- 21. Objective-C等价于Ruby中的string.unpack('N')
- 22. Objective-C有像php的include_once调用吗?
- 23. Ruby中没有对象
- 24. Objective-C:我的init值没有设置
- 25. 的Objective-C - UIControlEventTouchUpInside没有被解雇?
- 26. initWithSize被触发在Objective-C,但没有斯威夫特
- 27. '如果'有条件的工作与NSLog但不是没有? Objective-C
- 28. 有没有人有使用YAJL的Objective-C绑定的经验?
- 29. 释放对象还没有被释放的Objective-C的
- 30. 带有NSMutableDictionary的对象键(Objective-C)
您正在开发什么操作系统? – 2011-04-23 20:11:06
我正在使用Mac OS X的文件管理器。 – neoneye 2011-04-23 20:12:32
在这种情况下,我建议您通过NSTask使用'expect.rb'(或'/ usr/bin/expect');我没有意识到Cocoa的任何等价物,并且在Mac上没有与资源有关的理由。 – 2011-04-23 20:14:59