随笔分类 -  C&C++

C&C++ skills
摘要:C++开发 cgi, 经常需要输出xml格式的数据。借鉴个to_json.hpp的库, 实现了个to_xml库, 可以方便输出xml数据。使用范例:#include "to_xml.hpp"#include <stdio.h>struct A{ int a; struct B { int i; std::string name; std::vector<std::string> other; DEF_TO_XML_MEM3(B, i, name, other) }; std::vector<int> b; B c; std::string 阅读全文
posted @ 2011-09-20 23:43 napoleon_liu 阅读(1798) 评论(1) 推荐(1)
摘要:C++开发 cgi, 经常需要输出json格式的数据。有些数据有重复的元素,每次写这些东西既容易出错,又繁琐。 在查看msgpack这个序列化库代码的时候,受到启发,写了个to_json.hpp的库,可以方便输出json数据。使用范例:#include "to_json.hpp"#include <stdio.h>struct A{ int a; struct B { int i; std::string name; std::vector<std::string> other; DEF_TO_JSON_MEM3(i, name, other) }; 阅读全文
posted @ 2011-09-20 11:25 napoleon_liu 阅读(3358) 评论(2) 推荐(2)
摘要:配置文件读取程序 conf.h#ifndef CONF_H_INCLUDE__#define CONF_H_INCLUDE__#include <string>#include <map>#include <sstream>#include <stdio.h>#include <stdlib.h>#include <string.h>#define CONF_BLANK_SEP "\t\v\r\n "class Conf { public: static std::string right_trim(s 阅读全文
posted @ 2011-05-16 18:30 napoleon_liu 阅读(714) 评论(0) 推荐(0)
摘要:多读多写下,先进后出队列,可以不加锁,下面是实现代码 lifo.h#include "cas.h" #include <stddef.h>struct lifo_node { volatile struct lifo_node *next; };struct lifo { void init() { top = NULL; cnt=0; } void push(lifo_node *node) { struct lifo old_val, new_val; do { old_val = *this; node->next = old_val.top; ne 阅读全文
posted @ 2011-04-06 10:25 napoleon_liu 阅读(2370) 评论(0) 推荐(0)
摘要:This tutorial will show you how to manually rebuild a backtrace with GDB on x86 using the stack frame pointer and current instruction pointer. Consider the following gdb backtrace: It's pretty clear t... 阅读全文
posted @ 2011-02-14 14:04 napoleon_liu 阅读(1615) 评论(0) 推荐(0)
摘要:以堆栈溢出为代表的缓冲区溢出已成为最为普遍的安全漏洞。由此引发的安全问题比比皆是。早在 1988 年,美国康奈尔大学的计算机科学系研究生莫里斯 (Morris) 利用 UNIX fingered 程序的溢出漏洞,写了一段恶意程序并传播到其他机器上,结果造成 6000 台 Internet 上的服务器瘫痪,占当时总数的 10%。各种操作系统上出现的溢出漏洞也数不胜数。为了尽可能避免缓冲区溢出漏洞被攻... 阅读全文
posted @ 2011-02-14 00:59 napoleon_liu 阅读(12719) 评论(1) 推荐(3)
摘要:简介 ctags − Generate tag files for source code ctags 最先是用来生成C代码的tags文件,后来扩展成可以生成各类语言的tags, 有些语言也有专有的tags生成工具(比如java的jtags, python的 ptags). ctags 生成的 tags文件可用于 vi 来做代码导航和 vi的onmicppcomplete 插件来做代码补全。 ctags -e生成的 TAGS文件可以用于 emacs. 技巧 最简单使用是 : ctags –R . -R 表示递归子目录, 这个命令的意思就是为 当前目录下和其子目录下的文件建立索引( 索引只包 阅读全文
posted @ 2011-01-23 23:33 napoleon_liu 阅读(16034) 评论(0) 推荐(2)
摘要:引 C++0x把 auto 关键字改成了 自动类型声明关键字。 比如: mapstring, mapint, vectorstring ::const_iterator it = c.begin(); 可以写成 auto it = c.begin(); 因为类型可以从 c.begin()推导,所以前面的类型声明是重复的。DRY(Don't Repeat Yourself) gcc 4.4 就开始支持这 C++ 0x特性, Visual C++ 2010也支持。 现实 但我们平时使用的 gcc 很多版本都还很低,这该怎么办呢? 方案 gcc 扩展 typeof 帮你忙 阅读全文
posted @ 2011-01-19 09:47 napoleon_liu 阅读(461) 评论(1) 推荐(1)
摘要:#include stdint.h #include stdlib.h #include assert.h #include string.h inline uint32_t kr_hash(char const *str, size_t len) {    uint32_t hash =0;    unsigned char const*p =(unsigned char con... 阅读全文
posted @ 2010-12-28 20:10 napoleon_liu 阅读(954) 评论(0) 推荐(0)
摘要:命令行处理和 gperf 的作用 命令行处理一直以来都是软件开发中最容易被忽视的领域。几乎所有比较复杂的软件都具有一些可用的命令行选项。事实上,大量 if-else 语句经常被用来处理用户输入,因此维护这种遗留代码相当费时,对资深程序员亦是如此。这种情形下,很多 C 开发人员通常使用冗长(通常都嵌套使用)的 if-else 语句,以及 ANSI C 库函数,例如 strcmp、strcasecm... 阅读全文
posted @ 2010-12-27 11:12 napoleon_liu 阅读(1883) 评论(3) 推荐(0)
摘要:FNV是 Glenn Fowler, Landon Curt Noll, and Phong Vo 三人的缩写。 FNV-1 哈希算法的核心思想如下: 实现源码 uint32_t fnv_hash(char const *str, int len) { unsigned long hash = 2166136261; //offset_basis //FNV prime for 32 bit is 16777619//#define FNV_OP() hash = (hash*16777619)^*str++#define FNV_OP() hash += (hash1) + (has 阅读全文
posted @ 2010-12-26 21:56 napoleon_liu 阅读(3105) 评论(1) 推荐(0)
摘要:http://burtleburtle.net/bob/hash/doobs.html Bob优化它的第二版本hash, 速度提高了3倍,http://burtleburtle.net/bob/c/lookup3.c 下面我提取的一个变长key, 小端版本(intel机器) #include stdint.h /* defines uint32_t etc */ #include sys/param.h /* attempt to define endianness */ #ifdef linux # include endian.h /* attempt to define 阅读全文
posted @ 2010-12-22 17:22 napoleon_liu 阅读(2111) 评论(1) 推荐(0)
摘要:最近在研究无锁算法, 参照Michael and Scott的伪码,实现了个c++版本。参考 http://www.cs.rochester.edu/research/synchronization/pseudocode/queues.html 伪代码是:Code highlighting prod... 阅读全文
posted @ 2010-08-07 09:53 napoleon_liu 阅读(7819) 评论(13) 推荐(1)