+ (BOOL)isPDF:(NSString *)filePath
{
BOOL state = NO;
if (filePath != nil) // Must have a file path
{
const char *path = [filePath fileSystemRepresentation];
int fd = open(path, O_RDONLY); // Open the file
if (fd > 0) // We have a valid file descriptor
{
const char sig[1024]; // File signature buffer
ssize_t len = read(fd, (void *)&sig, sizeof(sig));
state = (strnstr(sig, "%PDF", len) != NULL);
close(fd); // Close the file
}
}
return state;
}
c函数strnstr(char* s1, chars2, int pos1) 各个参数的含义
/*
- strnstr - Find the first substring in a %NUL terminated string
- @s1: The string to be searched
- @s2: The string to search for
- @pos1: 在s1的前pos1字符中查找
*/
网友评论