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; }
CMsnContactManager::~CMsnContactManager(void) { STRING_SAFE_FREE(cache_key_); STRING_SAFE_FREE(preferred_hostname_); STRING_SAFE_FREE(session_id_); if (groups_) delete groups_; if (memberShips_) delete memberShips_; if (contacts_) delete contacts_; if (addressAb_) delete addressAb_; if (debug_fpout_ == NULL) acl_vstream_fclose(debug_fpout_); }
static int test_file3(void) { char buf[1024]; int ret; ACL_VSTREAM *fp = acl_vstream_fopen("test3.txt", O_RDONLY, 0700, 8192); if (fp == NULL) { printf("fopen error %s\n", acl_last_serror()); return (-1); } while (1) { ret = acl_vstream_gets_nonl(fp, buf, sizeof(buf)); if (ret == ACL_VSTREAM_EOF) break; printf(">>>gets(%s), ret: %d\n", buf, ret); } acl_vstream_fclose(fp); return (0); }
static int test_vstream(void) { const char *filename = "test.txt"; ACL_VSTREAM *fp = acl_vstream_fopen(filename, O_RDWR | O_CREAT, 0644, 1024); struct acl_stat sbuf; char buf[256]; struct tm *local_time; acl_lib_init(); if (fp == NULL) { printf("open file(%s) error(%s)\r\n", filename, acl_last_serror()); end(); return (-1); } if (acl_vstream_fstat(fp, &sbuf) < 0) { acl_vstream_close(fp); printf("fstat file(%s) error(%s)\r\n", filename, acl_last_serror()); end(); return (-1); } printf("file(%s) stat:\r\n", filename); printf("size=%d\r\n", (int) sbuf.st_size); local_time = localtime(&sbuf.st_mtime); if (local_time) { strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S", local_time); printf("修改时间,mtime=%s\r\n", buf); } else { printf("mtime: error(%s)\r\n", acl_last_serror()); } local_time = localtime(&sbuf.st_ctime); if (local_time) { strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S", local_time); printf("创建时间,ctime=%s\r\n", buf); } else { printf("ctime: error(%s)\r\n", acl_last_serror()); } local_time = localtime(&sbuf.st_atime); if (local_time) { strftime(buf, sizeof(buf), "%Y/%m/%d %H:%M:%S", local_time); printf("访问时间,atime=%s\r\n", buf); } else { printf("atime: error(%s)\r\n", acl_last_serror()); } int ch, ch1; ch = ACL_VSTREAM_GETC(fp); ch = 'a'; for (int i = 0; i < 100; i++) { ch1 = ACL_VSTREAM_PUTC(ch, fp); assert(ch1 == ch); printf("ok write char: %c\n", ch1); } ACL_VSTREAM_PUTC('\n', fp); acl_vstream_fclose(fp); end(); FILE *fp1 = fopen("test.txt", "w+"); assert(fp1); int ret = fputs("hello", fp1); printf("ret: %d\n", ret); fclose(fp1); return (0); }
int main(int argc, char* argv[]) { char buf[8192], filepath[256]; int ret, n; ACL_VSTREAM* fp; ACL_XML* xml; struct timeval begin, end; double spent; int ch, use_slice = 0, cache_count = 1000; filepath[0] = 0; while ((ch = getopt(argc, argv, "hmc:f:")) > 0) { switch (ch) { case 'h': usage(argv[0]); return 0; case 'm': use_slice = 1; break; case 'c': cache_count = atoi(optarg); if (cache_count <= 0) cache_count = 1000; break; case 'f': snprintf(filepath, sizeof(filepath), "%s", optarg); break; default: break; } } if (use_slice) acl_mem_slice_init(8, 1024, 100000, ACL_SLICE_FLAG_GC2 | ACL_SLICE_FLAG_RTGC_OFF | ACL_SLICE_FLAG_LP64_ALIGN); if (filepath[0] == 0) { usage(argv[0]); return 1; } xml = acl_xml_alloc(); if (cache_count > 0) acl_xml_cache(xml, cache_count); fp = acl_vstream_fopen(filepath, O_RDONLY, 0600, 8192); if (fp == NULL) { printf("open file %s error %s\r\n", filepath, acl_last_serror()); acl_xml_free(xml); return 1; } gettimeofday(&begin, NULL); n = 0; ACL_METER_TIME("------begin------"); while (1) { ret = acl_vstream_fgets(fp, buf, sizeof(buf) - 1); if (ret == ACL_VSTREAM_EOF) break; buf[ret] = 0; acl_xml_parse(xml, buf); if (++n % 10000 == 0) { printf("line: %d\r\n", n); ACL_METER_TIME("-------ok------"); } if (n % cache_count == 0) { printf("reset xml, line: %d\r\n", n); acl_xml_reset(xml); } } gettimeofday(&end, NULL); spent = stamp_sub(&end, &begin); printf("\r\ntotal spent: %0.2f ms\r\n", spent); acl_xml_free(xml); acl_vstream_fclose(fp); return 0; }