Archive

Posts Tagged ‘c’

调整im 机器人的数据发送方式

June 24th, 2009 陈毓端 No comments

之前的im机器人是用c的system调用shell的curl来实现数据传送。测试了两天感觉上不是很舒服。决定调整为c的libcurl API发送数据。
就这个小小的调整,涉及到一堆的修改。
最主要的如下:
1 制作 curl发送的动态链接库 so文件
2 修改im机器人的makefile 文件

curl 发送的so动态连接库:
curl_so_head.h
#include “stdio.h”
#include “curl/curl.h”
#include “stdlib.h”
#include “string.h”
#include “dlfcn.h”
void c2(char *msg,char *from,char *robot); // 设置了msg:消息 from:来源 robot:机器人类型

curl_so.c

#include “curl_so_head.h”
void c2(char *msg,char *from,char *robot)
{
CURL *curl;
CURLcode res;
char *s=”&msg=”;
char *s1=”&from=”;
char *s2=”&robot=”;
char pp[200];
strcpy(pp,s);
strcat(pp,msg);
strcat(pp,s1);
strcat(pp,from);
strcat(pp,s2);
strcat(pp,robot);

curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL,”http://xxxx.xxx.xxx.php”);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS,pp);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}

}
这个是发送调用程序:

void *SoLib;
int (*So)();
SoLib=dlopen(“so_curl.so”,RTLD_LAZY); //so_curl.so是上面操作生成的so文件
So = dlsym( SoLib, “c2″);
(*So)( msg,from,robot); //msg,from,robot表示需要的 消息 来源 robot 机器人类型

修改makefile文件 主要是要在 gcc 后面加上 -ldl 参数 .

ok 目前这项应用就到此吧。后期可能还要加新东西。

Categories: linux, 编程语言 Tags: , , ,

linux c 链接库 so制作及调用

June 23rd, 2009 陈毓端 No comments

最近的一个程序因为比较复杂,需要自己编写so动态链接库来给c语言程序调用。
下面就简单的写下so制作到调用的整个流程。
首先做个头文件
head.h:

  1. /*
  2. head.h
  3.  
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. void method_1(); //未设置参数
  8. void method_2(char *s); // 设置了一个参数

method_1.c:

  1. /*
  2.  method_1.c
  3. */
  4. #include "head.h"
  5. void method_1()
  6. {
  7. printf("我是陈毓端,您执行的是方法1(method_1)");
  8. }

method_2.c

  1. /*
  2. method_2.c
  3. */
  4. #include "head.h"
  5. void method_2(char *s)
  6. {
  7. printf("我是陈毓端,您执行的是方法2(method_2):%s",s);
  8. }

关键一步,生成so(动态链接库)
gcc head.h method_1.c method_2.c -fPIC -shared -o method.so

到现在 method.so 文件已经制做完毕。

接下来是调用
work_so.c

  1. #include  "stdio.h"
  2. #include  "stdlib.h"
  3. #include  "dlfcn.h"
  4. int main()
  5. {
  6. void  *SoLib;
  7. int   (*So)();
  8. SoLib=dlopen("./method.so",RTLD_LAZY); //加载method.so
  9. So   = dlsym( SoLib, "method_1");  //声名method_1方法
  10. (*So)( "" );                                                  //执行method_1方法
  11.  
  12. So    = dlsym(SoLib, "method_2");
  13. (*So)( "method_2" ); //设置参数
  14. }

编译 :

  1. gcc work_so.c -o word_so  -ldl


不出意外 结果为:

我是陈毓端,您执行的是方法1(method_1)我是陈毓端,您执行的是方法1(method_2):method_2

好了这个流程完毕。

Categories: 编程语言 Tags: ,

c 实现urlencode转义

June 23rd, 2009 陈毓端 No comments
  1. #include "stdio.h"
  2. #include "malloc.h"
  3. #include "string.h"
  4. #include "stdlib.h"
  5.  
  6. char from_hex(char ch) {
  7.   return isdigit(ch) ? ch - '0' : tolower(ch) - 'a' + 10;
  8. }
  9.  
  10.  
  11. char to_hex(char code) {
  12.   static char hex[] = "0123456789abcdef";
  13.   return hex[code & 15];
  14. }
  15.  
  16.  
  17. char *url_encode(char *str) {
  18.   char *pstr = str, *buf = malloc(strlen(str) * 3 + 1), *pbuf = buf;
  19.   while (*pstr) {
  20.     if (isalnum(*pstr) || *pstr == '-' || *pstr == '_' || *pstr == '.' || *pstr == '~') 
  21.       *pbuf++ = *pstr;
  22.     else if (*pstr == ' ') 
  23.       *pbuf++ = '+';
  24.     else 
  25.       *pbuf++ = '%', *pbuf++ = to_hex(*pstr >> 4), *pbuf++ = to_hex(*pstr & 15);
  26.     pstr++;
  27.   }
  28.   *pbuf = '\0';
  29.   return buf;
  30. }
  31.  
  32. char *url_decode(char *str) {
  33.   char *pstr = str, *buf = malloc(strlen(str) + 1), *pbuf = buf;
  34.   while (*pstr) {
  35.     if (*pstr == '%') {
  36.       if (pstr[1] && pstr[2]) {
  37.         *pbuf++ = from_hex(pstr[1]) << 4 | from_hex(pstr[2]);
  38.         pstr += 2;
  39.       }
  40.     } else if (*pstr == '+') { 
  41.       *pbuf++ = ' ';
  42.     } else {
  43.       *pbuf++ = *pstr;
  44.     }
  45.     pstr++;
  46.   }
  47.   *pbuf = '\0';
  48.   return buf;
  49. }
  50. int main(){
  51. char *s1="贪官站住,回头看看,你的脚下充满血迹,你的脑后充满无奈的干枯的目光,你的祖上为你羞愧......---------陈毓端06/23/2009 北京.";
  52. char *s=url_encode(s1);
  53.  printf ("value:%s", s);
  54. }
Categories: 愤青牢骚, 编程语言 Tags: , ,