示例#1
0
// 将位于文件中的邮件内容进行解析,并将邮件体数据转储于另一个内存缓冲中
static void mime_test2(acl::mime& mime, const char* path)
{
	// 以下仅解析邮件头部分

	printf("\r\n");
	ACL_METER_TIME("---------------parse mail begin--------------------");
	acl::ifstream fp;
	if (fp.open_read(path) == false) {
		printf("open %s error %s\n", path, strerror(errno));
		return;
	}
	acl::string buf;
	const char* ptr;
	size_t n;

	// 开始邮件解析过程
	mime.update_begin(path);

	while (1) {
		if (fp.gets(buf, false) == false)
			break;
		ptr = buf.c_str();
		n = buf.length();

		// 如果返回 true 表示头部解析完毕, 为了使该函数返回 true,
		// 必须保证调用此函数的最后一行数据为 "\r\n" 即空行

		//printf(">>>>%s", ptr);
		if (mime.update(ptr, n) == true)
		{
			printf(">>> parse over, last line: %s\n", ptr);
			break;
		}
		buf.clear();
	}

	// 必须调用 update_end
	mime.update_end();

	acl::mime_body* pBody;
	pBody = mime.get_body_node(false);
	if (pBody)
	{
		acl::string buf2;
		pBody->save_body(buf2);
		printf(">>>>>>>body: %s\n", buf2.c_str());
	}

	header_out(&mime);
	ACL_METER_TIME("---------------parse mail end  --------------------");
}
示例#2
0
// 带偏移量的MIME解析过程,并将邮件体数据转储于另一个文件中
static void mime_test4(acl::mime& mime, const char* path)
{
	mime.reset();
	acl::ifstream in;
	if (in.open_read(path) == false)
	{
		printf("open %s error %s\n", path, strerror(errno));
		return;
	}
	acl::string buf;
	size_t off = 0;

	// 先读文件头, 并略过文件头
	while (true)
	{
		if (in.gets(buf, false) == false)
			break;

		off += buf.length();

		if (buf == "\n" || buf == "\r\n")
		{
			buf.clear();
			break;
		}
		buf.clear();
	}

	// 开始解析邮件头及邮件体部分

	mime.update_begin(path);
	// 开始读邮件
	while (true)
	{
		if (in.gets(buf, false) == false)
			break;
		mime.update(buf.c_str(), buf.length());
		buf.clear();
	}
	mime.update_end();

	printf("\n-----------------------------------------------------\n\n");
	acl::mime_body* pBody = mime.get_body_node(false, true, "gb2312", (off_t) off);
	if (pBody)
	{
		acl::string buf2;
		pBody->save_body(buf2);
		printf(">>>>>>>body(%d): %s\n", (int) off, buf2.c_str());
	}
}
示例#3
0
// 解析邮件并输出邮件头信息
static void test_mime_header(acl::mime& mime, const char* path)
{
	// 以下仅解析邮件头部分

	printf("\r\n");
	ACL_METER_TIME("---------------parse header begin--------------------");
	acl::ifstream fp;
	if (fp.open_read(path) == false) {
		printf("open %s error %s\n", path, strerror(errno));
		return;
	}
	acl::string buf;
	const char* ptr;
	size_t n;

	// 开始邮件解析过程
	mime.update_begin(path); // update_begin 内部会自动调用 reset()

	while (1) {
		if (fp.gets(buf, false) == false)
			break;
		if (buf == "\r\n" || buf == "\n")
			break;

		ptr = buf.c_str();

		n = buf.length();

		// 如果返回 true 表示头部解析完毕, 为了使该函数返回 true,
		// 必须保证调用此函数的最后一行数据为 "\r\n" 即空行

		(void) mime.update(ptr, n);
	}

	mime.update_end();

	header_out(&mime);
	ACL_METER_TIME("---------------parse header end  --------------------");
}