Exemplo n.º 1
0
int CGb2Utf8::TransformFile(const char *pFrom, const char *pTo)
{
	char *sBuf = NULL;
	size_t iLen;

#undef RETURN
#define RETURN(_x_) do \
{ \
	if (sBuf) \
		acl_myfree(sBuf); \
	return(_x_); \
} while(0);

	sBuf = acl_vstream_loadfile(pFrom);
	if (sBuf == NULL)
		RETURN (-1);
	if (*sBuf == 0)
		RETURN (-1);

	iLen = strlen(sBuf);

	acl::charset_conv conv;
	acl::string buf;
	if (conv.convert(m_fromCharset.GetString(), m_toCharset.GetString(),
		sBuf, iLen, &buf) == false)
	{
		logger_error("conver from %s to %s error: %s, file: %s",
			m_fromCharset.GetString(), m_toCharset.GetString(),
			conv.serror(), pFrom);
		RETURN (-1);
	}

	ACL_VSTREAM *fp;
	fp = acl_vstream_fopen(pFrom, O_RDWR | O_TRUNC | O_BINARY /* | O_APPEND */,
		0600, 1024);
	if (fp == NULL)
	{
		logger_error("open %s error %s", pFrom, acl::last_serror());
		RETURN (-1);
	}
 	int ret = acl_vstream_writen(fp, buf.c_str(), buf.length());
	acl_vstream_close(fp);
	if (ret == ACL_VSTREAM_EOF)
		logger_error("write to %s error %s", pFrom, acl::last_serror());
	else
		logger("transer from %s to %s ok, file: %s",
			m_fromCharset.GetString(), m_toCharset.GetString(), pFrom);
	RETURN (ret == ACL_VSTREAM_EOF ? -1 : 0);
}
Exemplo n.º 2
0
bool CDelBOM::DeleteBOM(CString& filePath)
{
	char* sBuf;

	sBuf = acl_vstream_loadfile(filePath);
	if (sBuf == NULL)
		return false;
	size_t len = strlen(sBuf);
	if (len < 3)
	{
		acl_myfree(sBuf);
		return false;
	}

	// 先判断文件内容前缀是否是BOM格式
	if (sBuf[0] != (char) 0xEF || sBuf[1] != (char) 0xBB || sBuf[2] != (char) 0xBF)
	{
		acl_myfree(sBuf);
		return false;
	}

	// 将内容指针偏移 3 个字节,即去掉BOM格式
	len -= 3;
	char* ptr = sBuf + 3;

	ACL_VSTREAM* fp = acl_vstream_fopen(filePath,
		O_WRONLY | O_APPEND | O_TRUNC, 0600, 4096);
	if (fp == NULL)
	{
		acl_myfree(sBuf);
		return false;
	}
	else if (len == 0)
	{
		acl_vstream_fclose(fp);
		acl_myfree(sBuf);
		return true;
	}
	else if (acl_vstream_writen(fp, ptr, len) == ACL_VSTREAM_EOF)
	{
		acl_msg_error("write to file: %s error: %s",
			filePath.GetString(), acl_last_serror());
	}

	acl_vstream_fclose(fp);
	acl_myfree(sBuf);
	return true;
}
Exemplo n.º 3
0
Arquivo: main.c Projeto: jhomble/redis
static void run_client(const char *addr, const char *filename)
{
    char *request = acl_vstream_loadfile(filename);
    ACL_VSTREAM *client;
    int   ret;
    char  buf[1024];

    if (request == NULL) {
        printf("load file(%s) error(%s)\n", filename, acl_last_serror());
        return;
    }

    client = acl_vstream_connect(addr, ACL_BLOCKING,
                                 0, 0, 4096);
    if (client == NULL) {
        printf("connect addr(%s) error(%s)\n", addr, acl_last_serror());
        acl_myfree(request);
        return;
    }

    acl_tcp_set_sndbuf(ACL_VSTREAM_SOCK(client), 10);
    if (acl_vstream_writen(client, request, strlen(request)) == ACL_VSTREAM_EOF) {
        printf("write to addr(%s) error(%s)\n", addr, acl_last_serror());
        acl_vstream_close(client);
        acl_myfree(request);
        return;
    }

    memset(buf, 0, sizeof(buf));

    while (1) {
        ret = acl_vstream_read(client, buf, sizeof(buf) - 1);
        if (ret == ACL_VSTREAM_EOF)
            break;
        buf[ret] = 0;
        usleep(100000);
        printf(">>>%s\n", buf);
    }

    printf(">>>last data(%s)\n", buf);
    acl_vstream_close(client);
    acl_myfree(request);
}
Exemplo n.º 4
0
Arquivo: main.c Projeto: 10jschen/acl
static void fmt_change(const char *filepath, const char *fmt)
{
	const char *myname = "fmt_change";
	ACL_VSTREAM *fp;
	char *buf, *ptr;
	const char *tmp;
	int   n, ret;

	buf = acl_vstream_loadfile(filepath);
	if (buf == NULL)
		acl_msg_fatal("%s: loadfile(%s, %s)",
			myname, filepath, strerror(errno));

	fp = acl_vstream_fopen(filepath, O_WRONLY | O_TRUNC, 0600, 4096);
	if (fp == NULL)
		acl_msg_fatal("%s: open file(%s, %s)",
			myname, filepath, strerror(errno));

	ptr = buf;

	while (*ptr) {
		n = 0;
		if (*ptr == '\r') {
			n++;
			ptr++;
			if (*ptr == '\n') {
				n++;
				ptr++;
				if (strstr(filepath, "samples") == NULL
				    && strstr(filepath, "unit_test") == NULL)
				{
					if (strrncasecmp(filepath, ".c", 2) == 0)
						__total_c_line++;
					else if (strrncasecmp(filepath, ".cpp", 4) == 0)
						__total_cpp_line++;
					else if (strstr(filepath, "mysql") == NULL
						&& strstr(filepath, "openssl") == NULL
						&& strstr(filepath, "dist") == NULL
						&& strstr(filepath, "bdb") == NULL
						&& strstr(filepath, "tc") == NULL
						&& strstr(filepath, "google") == NULL
						&& strstr(filepath, "iconv") == NULL
						&& strstr(filepath, "polarssl") == NULL
						&& strstr(filepath, "sqlite") == NULL
						&& strstr(filepath, "zlib") == NULL
						&& strstr(filepath, "cdb") == NULL)
					{
						if (strrncasecmp(filepath, ".h", 2) == 0)
							__total_h_line++;
						else if (strrncasecmp(filepath, ".hpp", 4) == 0)
							__total_hpp_line++;
					}
				}
			}
		} else if (*ptr == '\n') {
			n++;
			ptr++;
			if (strstr(filepath, "samples") == NULL
					&& strstr(filepath, "unit_test") == NULL)
			{
				if (strrncasecmp(filepath, ".c", 2) == 0)
					__total_c_line++;
				else if (strrncasecmp(filepath, ".cpp", 4) == 0)
					__total_cpp_line++;
				else if (strstr(filepath, "mysql") == NULL
					&& strstr(filepath, "openssl") == NULL
					&& strstr(filepath, "dist") == NULL
					&& strstr(filepath, "bdb") == NULL
					&& strstr(filepath, "tc") == NULL
					&& strstr(filepath, "google") == NULL
					&& strstr(filepath, "iconv") == NULL
					&& strstr(filepath, "polarssl") == NULL
					&& strstr(filepath, "sqlite") == NULL
					&& strstr(filepath, "zlib") == NULL
					&& strstr(filepath, "cdb") == NULL)
				{
					if (strrncasecmp(filepath, ".h", 2) == 0)
						__total_h_line++;
					else if (strrncasecmp(filepath, ".hpp", 4) == 0)
						__total_hpp_line++;
				}
			}
		}
		if (n) {
			tmp = fmt;
			n = strlen(tmp);
		} else {
			tmp = ptr++;
			n = 1;
		}
		ret = acl_vstream_buffed_writen(fp, tmp, n);
		if (ret == ACL_VSTREAM_EOF)
			acl_msg_fatal("%s: write to %s error(%s)",
				myname, filepath, strerror(errno));
	}

	if (acl_vstream_fflush(fp) == ACL_VSTREAM_EOF)
		acl_msg_fatal("%s: fflush to %s error(%s)",
			myname, filepath, strerror(errno));
	acl_vstream_close(fp);
	acl_myfree(buf);
}