示例#1
0
/**
 * Takes a dvector and implodes it to a string based on a specific 
 * character to delimit by.
 * Returns a dstrbuf
 */
dstrbuf *
implode(dvector vec, char delim)
{
	dstrbuf *buf = dsbNew(100);
	size_t veclen = dvLength(vec);
	uint i=0;

	for (i=0; i < veclen; i++) {
		dsbCat(buf, vec[i]);
		if ((i+1) < veclen) {
			/* Only append a ',' if we're not at the end. */
			dsbCat(buf, &delim);
		}
	}
	return buf;
}
示例#2
0
dstrbuf *
mimeFiletype(const char *filename)
{
	bool found=false;
	int i=0, veclen=0;
	dstrbuf *type=NULL;
	dstrbuf *buf=DSB_NEW;
	dvector vec=NULL;
	const char *ext=NULL;
	dstrbuf *filen=NULL;
	FILE *file = fopen(MAGIC_FILE, "r");

	if (!file) {
		goto exit;
	}
	filen = mimeFilename(filename);
	ext = strrchr(filen->str, '.');

	/* If we don't know  the extension, we don't know what type
	 * of file it's going to be. Therefore, skip all of this.  */
	if (!ext) {
		goto exit;
	}
	/* Get past . in extension name. */
	ext++;

	while (!feof(file)) {
		dsbReadline(buf, file);
		if (buf->str[0] == '#' || buf->str[0] == '\n') {
			continue;
		}
		chomp(buf->str);

		/* If we still have an allocated type, free it */
		if (type) {
			dsbDestroy(type);
		}
		type = getMimeType(buf->str);
		if (type->len == 0) {
			continue;
		}
		vec = explode(buf->str, " \t");
		veclen = dvLength(vec);
		/* Start i at 1 since the first element in the
		 * vector is the mime type. The exts are after that. */
		for (i=1; i < veclen; i++) {
			if (strcmp((char *)vec[i], ext) == 0) {
				found = true;
				break;
			}
		}
		dvDestroy(vec);
		if (found) {
			/* Found it! */
			break;
		}
	}

exit:
	dsbDestroy(filen);
	dsbDestroy(buf);
	if (file) {
		fclose(file);
	}
	if (!type || type->len == 0) {
		type = DSB_NEW;
		dsbCopy(type, "application/unknown");
	}
	return type;
}