鱼C论坛

 找回密码
 立即注册

C语言的面向对象编程(原创)

已有 324 次阅读2016-5-4 13:55 |个人分类:C语言

#define _XOPEN_SOURCE //它是使程序符合系统环境的不可缺少的部分

#include "stdio.h" //基本输入输出函数库

#include "stdlib.h" //基础函数库

#include "string.h" //字符串处理函数库

#include "sys/types.h" //数据类型定义

#include "sys/socket.h" //提供socket函数及数据结构

#include "netinet/in.h" //定义数据结构sockaddr_in

#include "arpa/inet.h" //提供IP地址转换函数

#include "netdb.h" //提供设置及获取域名的函数

#include "sys/ioctl.h" //提供对I/O控制的函数

#include "sys/poll.h" //提供socket等待测试机制的函数

#include "unistd.h" //提供通用的文件、目录、程序及进程操作的函数

#include "errno.h" //提供错误号errno的定义,用于错误处理

#include "fcntl.h" //提供对文件控制的函数

#include "time.h" //提供有关时间的函数

#include "crypt.h" //提供使用DES加密算法的加密函数

#include "pwd.h" //提供对/etc/passwd文件访问的函数

#include "shadow.h" //提供对/etc/shadow文件访问的函数

#include "pthread.h" //提供多线程操作的函数

#include "signal.h" //提供对信号操作的函数

#include "sys/wait.h" //提供进程等待函数

#include "sys/ipc.h" //提供进程间通讯(IPC)函数

#include "sys/shm.h" //提供共享内存的函数

#include "netinet/if_ether.h" //ether_arp的数据结构

#include "netinet/ether.h" //以太祯的网络字节和ascii字节的转换,包括ether_ntoa(),ether_aton这样的函数定义

//#include "netinet/ip.h" //这个是gnu一开始就定义的头文件,同时还包括了bsd中的ipheader结构定义。同理的还有该目录下的tcp.h等文件

#include "linux/ip.h" //iphdr的数据结构,以及一些ip层的数据定义,同理的还有tcp.h,udp.h等等

#include "linux/if.h" //主要的socket头文件,似乎修改自unix的if.h,定义了网卡的接口信息的宏

#include "linux/if_packet.h" //原始数据包的数据结构定义,包括sockaddr_pkt,sockaddr_ll,想接收原始数据包的不能错过这个文件。同理的还有if_ppp.h,if_tun.h等等

//#include "net/bpf.h" //berkeley的数据包过滤头文件,想用bpf进行包过滤的要重视一下这个文件

#include "net/ethernet.h" //包括几个以太网的数据结构,ether_addr(mac帧结构),ether_header(以太帧的头部)

#define OBJECT_MAX_SIZE 1000000 //当前框架所允许的对象实例最大值为1000000

#define OBJECT_VARS_MAX_SIZE 65535 //一个类实例最大可以声明的属性数量为65535
#define OBJECT_FUNCS_MAX_SIZE 65535 //一个类实例最大可以声明的方法数量为65535
#define OBJECT_CLASS_NAME_MAX_SIZE 255 //类实例属性名称最大值
#define OBJECT_OBJECT_NAME_MAX_SIZE 255 //类实例属性名称最大值
#define OBJECT_VAR_NAME_MAX_SIZE 255 //类实例属性名称最大值
#define OBJECT_FUNC_NAME_MAX_SIZE 255 //类实例属性名称最大值

struct returnData{ //返回值(统一)
long type;
unsigned long size;
void * data;
};

struct var{ //属性类
long type;
unsigned long size;
char name[OBJECT_VAR_NAME_MAX_SIZE];
void * data;
};

struct func{ //方法类
long return_value_type;
unsigned long return_value_size;
void * return_value;
char name[OBJECT_FUNC_NAME_MAX_SIZE];
void * function;
};

struct object{ //根类(统一)
struct object * parnet; //parent 指针
struct object * this; //this 指针
char class_name[OBJECT_CLASS_NAME_MAX_SIZE]; //类名称
char object_name[OBJECT_OBJECT_NAME_MAX_SIZE]; //对象名称
struct var * vars[OBJECT_VARS_MAX_SIZE]; //属性集合
struct func * funcs[OBJECT_FUNCS_MAX_SIZE]; //方法集合
long vars_self_index; //当前的属性索引
long funcs_self_index; //当前的属性索引
long vars_length; //当前实际的属性数量
long funcs_length; //当前实际的方法数量
};

struct object * __construct(struct object * returnObject){ //构造函数
unsigned int index=0;
returnObject->this=returnObject;
for(index=0;index<OBJECT_VARS_MAX_SIZE;index++){
returnObject->vars[index]=NULL;
}
for(index=0;index<OBJECT_FUNCS_MAX_SIZE;index++){
returnObject->funcs[index]=NULL;
}
return returnObject;
}

void * __destruct(struct object * objectThis){ //析构函数

if(objectThis!=NULL){
unsigned int index=0;
for(index=0;index<OBJECT_VARS_MAX_SIZE;index++){
if(objectThis->vars[index]!=NULL){
free(objectThis->vars[index]);
objectThis->vars[index]=NULL;
}
}
for(index=0;index<OBJECT_FUNCS_MAX_SIZE;index++){
if(objectThis->funcs[index]!=NULL){
free(objectThis->funcs[index]);
objectThis->funcs[index]=NULL;
}
}
free(objectThis);
}
objectThis=NULL;
return objectThis;
}

struct object * create_object(){ //创建对象
struct object * returnObject=malloc(sizeof(struct object));
return __construct(returnObject);
}

struct object * delete_object(struct object * objectThis){ //删除对象
return __destruct(objectThis);
}

struct var * object_get_var(struct object * objectThis,char varName[255]){ //获得对象属性
void * returnVar=NULL;
unsigned int index=0;
for(index=0;index<OBJECT_VARS_MAX_SIZE;index++){
if(objectThis->vars[index]!=NULL){
struct var * tmpVarObject = (struct var *)objectThis->vars[index];
if(strcmp(tmpVarObject->name,varName)==0){
returnVar=malloc(sizeof(struct var));
memset(returnVar,0,sizeof(struct var));
memcpy(returnVar,objectThis->vars[index],sizeof(struct var));
break;
}
}
}

return returnVar;
}

struct object * object_set_var(struct object * objectThis,struct var varObject){ //设置对象属性

unsigned int index=0,exist=0,isNull=1;

for(index=0;index<OBJECT_VARS_MAX_SIZE;index++){
if(objectThis->vars[index]!=NULL){
isNull=0;
struct var * tmpVarObject = (struct var *)objectThis->vars[index];
if(strcmp(tmpVarObject->name,varObject.name)==0){
exist=1;
memset(objectThis->vars[index],0,sizeof(struct var));
memcpy(objectThis->vars[index],&varObject,sizeof(struct var));
break;
}
}
}

if(!exist){
if(isNull){
objectThis->vars_self_index = 0;
}
objectThis->vars[objectThis->vars_self_index]=malloc(sizeof(struct var));
memcpy(objectThis->vars[objectThis->vars_self_index],&varObject,sizeof(struct var));

objectThis->vars_self_index++;
}

return objectThis;
}

struct object * object_set_func(struct object * objectThis,struct func funcObject){ //设置对象方法

unsigned int index=0,exist=0,isNull=1;

for(index=0;index<OBJECT_FUNCS_MAX_SIZE;index++){
if(objectThis->funcs[index]!=NULL){
isNull=0;
struct func * tmpFuncObject = (struct func *)objectThis->funcs[index];
if(strcmp(tmpFuncObject->name,funcObject.name)==0){
exist=1;
memset(objectThis->funcs[index],0,sizeof(struct func));
memcpy(objectThis->funcs[index],&funcObject,sizeof(struct func));
break;
}
}
}

if(!exist){
if(isNull){
objectThis->funcs_self_index = 0;
}
objectThis->funcs[objectThis->funcs_self_index]=malloc(sizeof(struct func));
memcpy(objectThis->funcs[objectThis->funcs_self_index],&funcObject,sizeof(struct func));

objectThis->funcs_self_index++;
}

return objectThis;
}

struct var * object_get_func(struct object * objectThis,char funcName[255]){ //获得对象方法
void * returnFunc=NULL;
unsigned int index=0;
for(index=0;index<OBJECT_FUNCS_MAX_SIZE;index++){
if(objectThis->funcs[index]!=NULL){
struct func * tmpFuncObject = (struct func *)objectThis->funcs[index];
if(strcmp(tmpFuncObject->name,funcName)==0){
returnFunc=malloc(sizeof(struct func));
memset(returnFunc,0,sizeof(struct func));
memcpy(returnFunc,objectThis->funcs[index],sizeof(struct func));
break;
}
}
}

return returnFunc;
}

struct object * new_object(){ //新的对象

struct object * returnObject = create_object();

struct func setVarObject;
setVarObject.return_value_type=1,
setVarObject.return_value_size=sizeof(struct object *);
setVarObject.return_value=returnObject;
setVarObject.function=object_set_var;
memcpy(setVarObject.name,"object_set_var",OBJECT_FUNC_NAME_MAX_SIZE);
struct func getVarObject;
getVarObject.return_value_type=2,
getVarObject.return_value_size=sizeof(struct object *);
getVarObject.return_value=returnObject;
getVarObject.function=object_get_var;
memcpy(getVarObject.name,"object_get_var",OBJECT_FUNC_NAME_MAX_SIZE);

struct func setFuncObject;
setFuncObject.return_value_type=3,
setFuncObject.return_value_size=sizeof(struct object *);
setFuncObject.return_value=returnObject;
setFuncObject.function=object_set_func;
memcpy(setFuncObject.name,"object_set_func",OBJECT_FUNC_NAME_MAX_SIZE);
struct func getFuncObject;
getFuncObject.return_value_type=4,
getFuncObject.return_value_size=sizeof(struct object *);
getFuncObject.return_value=returnObject;
getFuncObject.function=object_get_func;
memcpy(getFuncObject.name,"object_get_func",OBJECT_FUNC_NAME_MAX_SIZE);

object_set_func(returnObject,setVarObject);
object_set_func(returnObject,getVarObject);
object_set_func(returnObject,setFuncObject);
object_set_func(returnObject,getFuncObject);

return returnObject;
}

struct object * free_object(struct object * returnObject){ //销毁已有对象
return delete_object(returnObject);
}

struct object * objects[OBJECT_MAX_SIZE]; //全局对象数组

int init_objects(){
unsigned long index=0;
for(index=0;index<OBJECT_MAX_SIZE;index++){
objects[OBJECT_MAX_SIZE]=NULL;
}
return index;
}

typedef struct object * (*object_get_var_function)(struct object * ,char [255]);
typedef struct object * (*object_set_var_function)(struct object * ,struct var);
typedef struct object * (*object_get_func_function)(struct object * ,char [255]);
typedef struct object * (*object_set_func_function)(struct object * ,struct func);

//test code
int main(int argc, char* argv[]){
init_objects(); //初始化对象集合
unsigned int i=0;
struct object * obj=new_object();
strncpy(obj->class_name,"object",OBJECT_VAR_NAME_MAX_SIZE);
strncpy(obj->object_name,"obj",OBJECT_VAR_NAME_MAX_SIZE);

for(i=0;i<OBJECT_FUNCS_MAX_SIZE;i++){
if(obj->funcs[i]!=NULL){
printf("func_name : %s\n",obj->funcs[i]->name);
if(strcmp(obj->funcs[i]->name,"object_set_var")==0){
struct var varObj;
varObj.type=1;
strncpy(varObj.name,"var_name",OBJECT_VAR_NAME_MAX_SIZE);
char tmpStr[100];
sprintf(tmpStr, "%d", 10);
varObj.size=strlen(tmpStr);
varObj.data = malloc(varObj.size);
memcpy(varObj.data,tmpStr,varObj.size);
((object_set_var_function)(obj->funcs[i]->function))(obj,varObj);
}
}
}

for(i=0;i<OBJECT_VARS_MAX_SIZE;i++){
if(obj->vars[i]!=NULL){
printf("var_name : %s\n",obj->vars[i]->name);
printf("var_value : %s\n",(char *)obj->vars[i]->data);
}
}

printf("class_name : %s\n",obj->class_name);
printf("object_name : %s\n",obj->object_name);
return 0;
}


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)

facelist

您需要登录后才可以评论 登录 | 立即注册

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-12 21:54

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

返回顶部