I’m integrating a C library into an Objective-C app for iOS. I’m not very C-savvy… I know just enough to be dangerous.
The C function declaration is this:
extern int ZEXPORT unzGetCurrentFileInfo OF((unzFile file,
unz_file_info *pfile_info,
char *szFileName,
uLong fileNameBufferSize,
void *extraField,
uLong extraFieldBufferSize,
char *szComment,
uLong commentBufferSize));
I am really only interested in the szFileName. I know I can ignore the void* and char* by passing NULL. Can I pass some sort of NULL-equivalent to the uLong params? I’m getting a compiler warning about converting to pointer without a cast.
Here’s my loop for calling this function, in case anyone wants to comment on that. Did I do the malloc/free correctly? I’m not used to the low-level C stuff. I know people complain about Objective-C reference counting but in comparison it’s pretty luxurious 🙂
unz_file_info pfile_info;
char *szFileName = malloc(1024);
uLong fileNameBufferSize;
uLong commentBufferSize;
uLong extraFieldBufferSize;
do {
int ret = unzGetCurrentFileInfo(zipFile, &pfile_info, szFileName, fileNameBufferSize, NULL, extraFieldBufferSize, NULL, commentBufferSize);
NSLog(@"get info ret %i filename size %lu, filename %s", ret, pfile_info.size_filename, szFileName);
// do other stuff here with the filename
}
while (unzGoToNextFile(zipFile) == UNZ_OK);
free(szFileName);
The ulong parameters are the sizes of the buffer arguments. This is so the function knows how big those buffers are so it doesn’t overflow them.
If you need the fileName argument, you must supply a correct fileNameBufferSize.
As for whether you actually can pass in a NULL pointer to the pointer arguments, only the documentation(or source code) for this function can tell you. Or if the documentation doesn’t tell you, you’ll have to do some basic science experiments on the function and see how it behaves.
Assuming the function accepts NULL pointers for parameters you don’t want filled in, you’d likely pass 0 as the value for the ulong parameters.
You’ll have to do:
You should also investigate the meaning of the return value of unzGetCurrentFileInfo. If it fails, it’s unlikely you can use
szFileNameor any of the other arguments to the function – so don’t call NSLog with those variables if the function failsIn this case, malloc seems unneccesary. Just use a local array, and drop the free() call.