bianbian coding life

便便代码人生: 关注技术, 翻译文档, 偶尔动动手

Archive for the 'C/C++' Category

C/C++

[原] 根据struct生成基于JSON-C的json_object代码(auto generate function of C struct to JSON)

Posted by bianbian on 2008-04-09 11:13


本文Tags: ,

上次讨论了C的struct结构体与JSON交互,并提出了能否自动把struct转为JSON的问题。
今天我总算解决了这个问题,虽然中间也想过传参数给一个函数集中处理,不过都是比较麻烦。我写了一个脚本,自动生成把struct的成员插入json_object的函数,底下页面有演示:auto generate function of C struct to JSON

标签: ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, Technology | 3 Comments »

[原] 未来分布式BBS的可能实现架构

Posted by bianbian on 2008-04-04 02:22


本文Tags: ,

libevent
http://www.monkey.org/~provos/libevent/

memcached
http://www.danga.com/memcached

libmemcached
http://tangent.org/552/libmemcached.html

memcachedb (新浪博客技术团队,赞一个)
http://memcachedb.org/

ncache (同上)
http://code.google.com/p/ncache/

标签: ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, Technology | No Comments »

[原] IDL剪贴板绘图在其他语言调用下失效的bug

Posted by bianbian on 2008-03-26 02:32


本文Tags: , , ,

经过运行剪贴板监视器程序,我确认这是IDL的bug:如果用VC等其他语言调用IDL绘图,IDL并不能通过IDLgrClipboard对象将绘图结果保存到系统剪贴板(而在IDL的编辑器环境下运行,剪贴板是能得到绘图结果的)。
下面是英文描述:
IDLgrClipboard not works in callable mode (I use VC++ to execute IDL procedure to draw some plots, and copy the result to system clipboard). After running a system-clipboard-viewer-tool, I conclude it must be a bug (I tested with IDL6.2, IDL7.0).

oClipbrd = OBJ_NEW(’IDLgrClipboard’, DIMENSIONS=windowSize)
oClipbrd->Draw, oView

It works well under IDL-workbench, but when call from VC++, everything else goes OK except clipboard is empty.

解决办法:只好制定FILENAME参数,将结果保存到文件:
Finally, I have no choise but to save the drawing to a file, and it works:

oClipbrd->Draw, oView, FILENAME=’tempout.bmp’

标签: , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, ENVI & IDL, Technology | No Comments »

[原] 模仿Google Map的FY2C实时云图平台上线~

Posted by bianbian on 2008-03-25 10:03


本文Tags: ,

模仿Google Map(图片切片载入在此平台不需要实现):

支持IE、Firefox等,支持鼠标移动、鼠标双击放大(左键)缩小(右键)、鼠标滚轮放大缩小

云图实时处理和发布(因为在学校内网,实时版本大家不能看到啦)

这里贴下演示地址(只有最后两个时次的图片):FY2C实时云图平台演示

标签: ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, JavaScript, Meteorology, Technology | No Comments »

[原] 自定义IDL调色板指南

Posted by bianbian on 2008-03-21 12:06


本文Tags: , ,

IDL的调色板功能很好很强大,以至于我还没琢磨透到底怎么调。。。。

不过一般都是用自己定义的调色板,就讲讲怎么往IDL里加入自己的调色板:
(加了以后在系统内LOADCT的时候,就能直接读取自定义的调色板,非常方便)

IDL提供了 MODIFYCT 命令用来更改或加入系统的预定义调色板:

MODIFYCT, Itab, Name, R, G, B [, FILE=filename]

Itab是索引值,0-40是系统的,一般都要41开始
Name是命名,32个字符内
R、G、B就是256长度的BYTE(单字节正数)数组
File是文件名,如果不制定就操作默认的(resouce目录的color1.tbl)

实际发现File指定了其他文件名,MODIFYCT会出错。看了源码,发现
MODIFYCT并没有处理文件读取错误(不存在)的异常。(不知道是不是IDL7.0的问题)

IDL也没有公开colortable的文件格式。不过我把MODIFYCT的源码结合color1.tbl
分析了一下,得到IDL的colortable文件格式:

总数 + R 256 + G 256 + B 256 + …. + 32长度名称(在文件末尾)

例(如果有两个表):[2][R..R][G..G][B..B][R..R][G..G][B..B][名称1][名称2]

这样就可以自己输出调色板文件了,不需要用IDL的默认文件(防止多用户冲突)。
之后可以用 LOADCT, 0, FILE=’mytable.tbl’ 动态载入

附C语言输出调色板文件代码:

  1. FILE *fp = fopen(file, "wb");
  2. BYTE n = 1;
  3. fwrite(&n, 1, 1, fp);
  4. fwrite(m_byteRGB, 1, 256 * 3, fp);
  5. fprintf(fp, "%-32s", "bianbian Default Table");
  6. fclose(fp);
标签: , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, ENVI & IDL, Technology | 2 Comments »

[原] IDL的数组存储是行顺序,下标引用是列在前

Posted by bianbian on 2008-03-17 01:29


本文Tags: , , ,

IDL的数组在内存中存储还是以行顺序的,只是多维下标引用的时候列为先。即:
A=
[r r r]
[g g g]
[b b b]
在内存中是 rrrgggbbb(和C是一样的!fortran是rgbrgbrgb)

而引用的时候列在前:(C是行在前!)
A[0,0] A[1,0] A[2,0]
A[0,1] A[1,1] A[2,1]
A[0,2] A[1,2] A[2,2]

三维的情况和C也一样(注意,IDL和C唯一的差别就是IDL的下标列在前,然后是行);存储结构一致也就意味着C里的多维数组内存块可以直接丢到IDL里处理。其实我觉得IDL在下标上太小白了,干嘛不和C保持一致?某IDL大牛说:之所以下标在前,是为了更直观的知道X方向上有几个、Y方向上有几个。比如:A[3, 2],X方向有3个,Y方向有2个(2行3列):Is IDL column-major or row-major?

某本书(似乎国内的书都不认真写 - -)说IDL是列顺序的,这是不对的。可以拿三维数据做简单试验(也便于对下标引用理解):

IDL> a=bindgen(3,3,3)
IDL> print,a
   0   1   2
   3   4   5
   6   7   8

   9  10  11
  12  13  14
  15  16  17

  18  19  20
  21  22  23
  24  25  26
IDL> print,a[1,0,*]
   1

  10

  19
IDL> print,a[*,1,1]
  12  13  14
标签: , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, ENVI & IDL, Technology | 1 Comment »

[原] 减小nginx编译后的文件大小 (Reduce file size of nginx)

Posted by bianbian on 2008-03-13 10:21


本文Tags: , , ,

默认的nginx编译选项里居然是用debug模式(-g)的(debug模式会插入很多跟踪和ASSERT之类),编译以后一个nginx有好几兆。
去掉nginx的debug模式编译,编译以后只有375K(nginx-0.5.33, gcc4)。
在 auto/cc/gcc,最后几行有:
# debug
CFLAGS=”$CFLAGS -g”
注释掉或删掉这几行,重新编译即可。-g参数用法详见这篇blog:[原]Linux下的c/c++ GDB调试

Nginx uses debug mode (-g) in compiling by default, which result in several MB of it’s size.
After debug disabled, the file size of nginx reduced to 375K (nginx-0.5.33, gcc4).
open auto/cc/gcc, find this at last several lines:
# debug
CFLAGS=”$CFLAGS -g”
delete it, and recompile. More of -g: Linux c/c++ GDB debug

标签: , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, Linux, Technology | No Comments »

[原] Intel平台gcc4,gcc3,icc测试

Posted by bianbian on 2007-09-24 12:20


本文Tags: , , , ,

[from zhch begin]
以一个简单的计算程序为例:

int main() {
double s=0;
int i;
for(i=200000001; i>=1; i-=4) s+=1.0/i-1.0/(i+2);
printf(”Pi=%f\n”, s*4);
}

这是一个常见的算Pi的程序,基本算法是Pi/4=1-1/3+1/5-1/7+…..
[from zhch end]

Slackware 12.0预装了gcc 4.1.2,我手动装了gcc 3.4.6,和icc 10.0。下面是测试结果:

bbs@bbs:~/test$ gcc -o PI.gcc PI.c -O3 -march=pentium4 -msse2 -mmmx
PI.c: In function ‘main’:
PI.c:5: warning:incompatible implicit declaration of built-in function ‘printf’

bbs@bbs:~/test$ icc -o PI.icc PI.c -O3 -xP
PI.c(5): warning #266: function “printf” declared implicitly
printf(”Pi=%f\n”, s*4);
^
PI.c(4): (col. 9) remark: LOOP WAS VECTORIZED.

bbs@bbs:~/test$ gcc3 -o PI.gcc3 PI.c -O3 -march=pentium4 -msse2 -mmmx

bbs@bbs:~/test$ time PI.gcc; time PI.gcc3; time PI.icc
Pi=3.141593

real 0m1.568s
user 0m1.544s
sys 0m0.016s
Pi=3.141593

real 0m1.551s
user 0m1.544s
sys 0m0.008s
Pi=3.141593

real 0m1.246s
user 0m1.212s
sys 0m0.012s

测试环境:Intel(R) Xeon(TM) MP CPU 3.00GHz * 4, 8G MEM

文件大小:
bbs@bbs:~/test$ v PI*
-rw-r–r– 1 bbs bbs 140 2007-09-23 22:28 PI.c
-rwxr-xr-x 1 bbs bbs 6365 2007-09-23 23:08 PI.gcc*
-rwxr-xr-x 1 bbs bbs 6343 2007-09-23 23:47 PI.gcc3*
-rwxr-xr-x 1 bbs bbs 19769 2007-09-23 23:08 PI.icc*

果然跟一些文章说的,gcc4比gcc3编译结果和运算速度都有所下降,icc在Intel平台下确实优化比较明显,毕竟是自己家的东西。

标签: , , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, Linux, Technology | 1 Comment »

[原]Linux下的c/c++ GDB调试

Posted by bianbian on 2007-09-18 10:13


本文Tags: , , ,

还是比较厉害的,比较准。
编译的时候加入 -g -rdynamic 选项,之后就可以跑调试了:
linux $ gcc -g -rdynamic -o error error.c
linux $ gdb error
GNU gdb 6.5
Copyright (C) 2006 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type “show copying” to see the conditions.
There is absolutely no warranty for GDB. Type “show warranty” for details.
This GDB was configured as “i686-pc-linux-gnu”…Using host libthread_db library “/lib/libthread_db.so.1″.

(gdb) run
Starting program: /home/linux/error

Program received signal SIGSEGV, Segmentation fault.
错误内容
行数 源码;
(gdb) kill
(gdb) q

linux $

标签: , , ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, Linux, Technology | 1 Comment »

[晕] 居然还有这种东西:C++ Trigraph

Posted by bianbian on 2007-08-01 03:34


本文Tags:

听说为了照顾某些键盘坏了又换不起的,可以使用这种3字符的串来代替一些特殊字符,preprocess的时候会替换掉:

#: ??=
\: ??/
^: ??’
[: ??(
]: ??)
{: ??<
}: ??>
|: ??!
~: ??-

于是程序就这样了:

  1. //: S03:trigraph.cpp
  2. ??=include <iostream>
  3.  
  4. int main(int argc, char* argv??(??)) ??<
  5.     using namespace std;
  6.     unsigned int n;
  7.     if (argc != 2 ??!??! (n = atoi(argv??(1??))) <= 0)
  8.         return 1;
  9.     cout << ??-(n ??' 0xf0f0) << '??/n';
  10.     return 0;
  11. ??>

我就奇怪了,键盘换不起,眼睛换换就那么便宜吗?

标签:

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, Technology | 2 Comments »

[问] fwrite的问题

Posted by bianbian on 2007-07-28 12:52


本文Tags:

fwrite(const void *buffer,size_t size, size_t count, FILE *fp);
这个似乎是把内存里的一股脑儿write到文件里去。
如果buffer是new出来的一大堆数据,有可能在内存里是分片存的,这时候一大堆一起写就有问题。
我不清楚是VC6的问题,还是fwrite本来就有这个问题:

  1. ....
  2. double *_bt[k] = new double[_header.wHeight * _header.wWidth];
  3. ....
  4. for(k=0; k < _GRAY_BANDS; k++)
  5. {
  6.   //用:fwrite(_bt[k], sizeof(double), _header.wHeight * _header.wWidth, fp2); 就不对
  7.   //必须一个一个保存
  8.   for(int j=0; j < _header.wHeight; j++)
  9.   {
  10.     for(int i=0; i < _header.wWidth; i++)
  11.     {
  12.        int idx = j * _header.wHeight + i;
  13.        fwrite(& _bt[k][idx], sizeof(double), 1, fp2);
  14.     }
  15.   }
  16.   delete[] _bt[k];
  17. }
  18. fclose(fp2);

哪位经过告诉我一声。

标签:

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, Technology | 3 Comments »

[原] C的struct和JSON交互

Posted by bianbian on 2007-07-15 04:55


本文Tags: ,

经过一天努力,现在百合涂鸦板用了JSON传递数据:http://bbs.nju.cn/pntdoc?board=beginner
虽然把数据和页面分开了,不过觉得过程还是比较麻烦:
1)新建JSON对象,C读取struct,把值插入
2)输出to_JSON_String
3)Javascript解析JSON,通过JST模板生成页面

实在是比较麻烦,有没有可能把一个struct直接转成JSON对象?
或者直接丢struct的字节码(加上描述信息)过去让Javascript负责解析成JSON对象?

struct就是一段长度固定的二进制码,如果想个办法能和JSON对象之间转换,应该能大大解放C作FastCGI的生产力。我的意思是这样:
C:
struct obj {
char name[3];
int age;
};

JSON:
obj {
name: “12″,
age: int
}

让我想想。

标签: ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, JavaScript, Technology | No Comments »

[原]C语言连接postgres的实例 - libpq使用

Posted by bianbian on 2006-12-20 03:28


本文Tags: ,

看过长篇文档后,啥废话也不说了,来个实例总结:

  1. char *sql = "SELECT userid,password FROM users WHERE ....";
  2. PGconn *conn = PQconnectdb("host=localhost user=postgres password= dbname=db port=5432");
  3. if (PQstatus(conn) == CONNECTION_OK)
  4. {
  5.   PQsetClientEncoding(conn, "UNICODE");
  6.   PGresult *res = PQexec(conn, sql);
  7.   if (res != null)
  8.   {
  9.     int status = PQresultStatus(res);
  10.     if (status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK)
  11.     {
  12.       char *res_userid = PQgetvalue(res, 0, 0);
  13.       char *res_password = PQgetvalue(res, 0, 1);
  14.       ....
  15.     }
  16.     PQclear(res);
  17.   }
  18. }
  19. PQfinish(conn);
标签: ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, Database, Technology | 2 Comments »

[原]涂鸦板oekakibbs和Shi-PainterPRO数据格式分析

Posted by bianbian on 2006-12-19 06:33


本文Tags: ,

去年用C语言实现的涂鸦板:http://bbs.nju.edu.cn/pntmain
想当年,一夜没睡,不停地debug、截获网络数据,分析涂鸦板oekakibbs和Shi-PainterPRO的数据格式。
最后保存成功带给我的喜悦我是终身难忘。这也是我喜欢编程的部分原因吧。呵呵
oekakibbs和Shi-PainterPRO是比较有名的涂鸦板程序(实际是Java Applet程序),通过http协议和自定义的数据格式往后台提交数据。解析数据网络上比较多的是php和asp的代码,但是因为和彼此自己的系统结合,所以看起来比较费尽。不过从来没有数据格式说明文档。
今天偶然又翻了一下这些代码,顺便在blog里记一下。要是能给其他人省点力气,也算功德一件。呵呵

  1. /*
  2. 解析涂鸦板传过来的数据并保存图片
  3. Net@Lilybbs, 2005-11-14 7:18
  4. /////////////////////////////
  5. 参数传递:pntupload?临时key_工具名
  6. ====================================
  7. oekakibbs数据格式:
  8. -------------------------------
  9. ...Content-type:string
  10.  
  11. Content-type:string
  12.  
  13. Content-type:image/0
  14.  
  15. {PNG图片数据}
  16. Content-type:animation/
  17.  
  18. {oekakibbs动画数据}
  19. ====================================
  20. Shi-PainterPRO数据格式:
  21. -------------------------------
  22. S0000000000{当前PNG图片的字节数}
  23. {PNG图片数据}{pch长度}{pch文件内容}
  24. -------------------------------
  25. //以下是pch文件内容:
  26. layer_count=3
  27. layer_max=35
  28. layer_last=35
  29. image_width=300
  30. image_height=300
  31. count_lines=25
  32. quality=1
  33.  
  34. {其他信息}
  35. -------------------------------
  36. */
  37. #include "www.h"
  38.  
  39. int pntupload_main()
  40. {
  41.     int n, i, j, key=0;
  42.     char tmp[80], *buf, tool[30], *p;
  43.     FILE *fp;
  44.  
  45.     chdir(BBSHOME);
  46.     http_header(200);
  47.     printf("Content-type: text/html; charset=gb2312\n\n");
  48.     n = atoi(getsenv("CONTENT_LENGTH"));
  49.     if (n<0 || n>3000000)
  50.         http_fatal("too big");
  51.     buf = malloc(n);
  52.     if(buf==0) http_fatal("out of memory");
  53.     fread(buf, 1, n, stdin); //读入buf
  54. /*
  55.     fp = fopen("paint/data", "w");
  56.     fwrite(buf, 1, n, fp);
  57.     fclose(fp);
  58. */
  59.     //-------- 获得_U_KEY
  60.     key = atoi(getsenv("QUERY_STRING"));
  61.     //-------- 获得tool
  62.     p = strchr(getsenv("QUERY_STRING"), '_');
  63.     strsncpy(tool, p+1, 20);
  64.  
  65.     //-------- 根据不同tool, 不同处理数据
  66.     //oekakibbs和oekakibbs2的处理是一样的
  67.     if(!strncmp("oekakibbs", tool, 9))
  68.     {
  69.         //-------- 获得图片
  70.         i = memstr(buf, n, "Content-type:image/0");
  71.         j = memstr(buf+i+24, n, "Content-type:animation/"); //24是"Content-type:image/0\r\n\r\n"的长度
  72.         sprintf(tmp, "paint/tmp/%d.png", key);
  73.         fp = fopen(tmp, "w");
  74.         fwrite(buf + i + 24, 1, j, fp); //j实际是png的长度
  75.         fclose(fp);
  76.         //-------- 获得动画
  77.         j += i+24; //j现在是从buf开始指向Content-type:animation的偏移量
  78.         sprintf(tmp, "paint/tmp/%d.oeb", key);
  79.         fp = fopen(tmp, "w");
  80.         fwrite(buf+j+27, 1, n - j - 27, fp); //p2-buf是长度,27是"Content-type:animation/\r\n\r\n"的长度
  81.         fclose(fp);
  82.         //需要输出success告诉applet保存成功
  83.         printf("success");
  84.     }
  85.     else if(!strcmp("Shi-Painter", tool))
  86.     {
  87.         //png 头:HEX 89 50 4E 47 0D 0A
  88.         //png 尾:HEX 49 45 4E 44 AE 42 60 82
  89.         //unsigned char pngend[] = {0x49, 0x45, 0x4E, 0x44, 0xAE, 0x42, 0x60, 0x82};
  90.         i = memstr(buf, n, "\r\n"); //第2行开始就是PNG图片
  91.         j = atoi(buf+1); //buf[0]固定是S,之后就是PNG的长度
  92.         sprintf(tmp, "paint/tmp/%d.png", key);
  93.         fp = fopen(tmp, "w");
  94.         fwrite(buf+i+2, 1, j, fp);
  95.         fclose(fp);
  96.         //-------- 获得动画
  97.         j += i+2; //j现在是从buf开始指向PNG末尾的偏移量
  98.         i = memstr(buf+j, n, "layer_count="); //这里的i就相当于oekakibbs里面的27:就是PNG末尾到layer_count的长度
  99.         sprintf(tmp, "paint/tmp/%d.oeb", key);
  100.         fp = fopen(tmp, "w");
  101.         fwrite(buf+j+i, 1, n-j-i, fp);
  102.         fclose(fp);
  103.     }
  104.     exit(0);
  105. }
标签: ,

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, Technology | No Comments »

[译]JSON的C语言实现: JSON-C - A JSON implementation in C

Posted by bianbian on 2006-12-17 03:44


本文Tags:

[翻译] JSON-C能让你在C语言里轻松创建JSON对象,输出JSON字符串,解析JSON字符串成JSON对象。
test1.c的部分实例代码:

  1. struct json_object *my_array;
  2.   my_array = json_object_new_array();
  3.   json_object_array_add(my_array, json_object_new_int(1));
  4.   json_object_array_add(my_array, json_object_new_int(2));
  5.   json_object_array_add(my_array, json_object_new_int(3));
  6.   json_object_array_put_idx(my_array, 4, json_object_new_int(5));
  7.   printf("my_array=\n");
  8.   for(i=0; i < json_object_array_length(my_array); i++) {
  9.     struct json_object *obj = json_object_array_get_idx(my_array, i);
  10.     printf("\t[%d]=%s\n", i, json_object_to_json_string(obj));
  11.   }
  12.   printf("my_array.to_string()=%s\n", json_object_to_json_string(my_array));

地址: http://oss.metaparadigm.com/json-c/

[Original] JSON-C implements a reference counting object model that allows you to easily construct JSON objects in C, output them as JSON formatted strings and parse JSON formatted strings back into the C representation of JSON objects.

Copyright Metaparadigm Pte. Ltd. 2004, 2005. Michael Clark

如果不清楚JSON,请参考:http://www.json.org

标签:

遵守创作共用协议,转载请链接形式注明来自http://bianbian.org 做人要厚道

相关日志

Posted in C/C++, Technology | 2 Comments »