Re: Windows _findfirst , _findnext (koko)
Re: Windows _findfirst , _findnext (koko)
- Subject: Re: Windows _findfirst , _findnext (koko)
- From: Sander Stoks <email@hidden>
- Date: Thu, 06 Oct 2011 14:06:20 +0200
> Thanks for all the input. I am doing x-platform development and we like
> to keep mainline code identical so I implemented functions _findfirst and
> _findnext using NSFileManager ... it work s pretty cool as follows:
>
> intptr_t _findfirst(CString search, _finddata_t *data)
> {
> intptr_t rtx = 0;
> _breakdown(search);
> if([results count])
> {
> const char* name = [[results objectAtIndex:0]
> cStringUsingEncoding:NSUTF8StringEncoding];
> strcpy(data->name,name);
> rtx = 1;
> }
> return rtx;
> }
>
> int _findnext(intptr_t& next, _finddata_t *data)
> {
> int rtx = 0;
> if(next < [results count])
> {
> const char* name = [[results objectAtIndex:next]
> cStringUsingEncoding:NSUTF8StringEncoding];
> strcpy(data->name,name);
> next++;
> }
> else rtx = 1;
> return rtx;
> }
>
> void _breakdown(CString search)
> {
> nssearch = [NSString stringWithCString:(const char*)search
> encoding:NSUTF8StringEncoding];
> path = [nssearch stringByDeletingLastPathComponent];
> fileManager = [NSFileManager defaultManager];
> directoryContents = [fileManager contentsOfDirectoryAtPath:path
> error:&error];
> lastPathComponent = [nssearch lastPathComponent];
> predicate = [NSPredicate predicateWithFormat:@"SELF like [c] %@",
> lastPathComponent];
> results = [directoryContents filteredArrayUsingPredicate:predicate];
> }
The MSDN documentation says that _findfirst returns -1 if it can't find
anything, instead of 0 (your client code may depend on that). See
http://msdn.microsoft.com/en-us/library/zyzxfzac(v=vs.71).aspx
Also, what does your _finddata_t look like? If it contains a fixed-size
char array for its "name" member, be aware that the "maximum sensible path
name" is a platform-dependent number. When I see the unguarded strcpy(),
I'd rather replace it with a strncpy instead.
Also, CString seems like a typedef for char*, but "normally" if it's a
class encapsulating a string (which its name suggests), you can't simply
cast it to a char*.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden