`
桔红糕
  • 浏览: 41315 次
  • 性别: Icon_minigender_2
  • 来自: 上海
社区版块
存档分类
最新评论

简易内存数据库(六)CREATE

    博客分类:
  • CPP
F# 
阅读更多

前面CREATE完成。当中有一个细节我想你应该已经注意到了。

就是当读到 float(4)或者char(100) 时,我们知道这个字符串中需要保存2个内容。一个是column类型,一个是column长度。

我就用了getColumnTypeLength(buffer, &column->type, &column->length); 这个函数,希望把buffer中读到的这两个内容,分别保存到column->type和column->length中。

 

显而易见,我把解析这种字符串的事情交给了getColumnTypeLength这个函数来做。

这个函数第一个buffer是输入参数。

column->type和column->length是需要函数内部设置值的返回参数。

返回参数必须以传递地址的方式,才能够使函数内部的设置保存下来。

 

那这个函数究竟怎么实现呢?我觉得这是一个基本功。每个同学都应该自己会写,而且也有很多写法。只要能实现就可以。

下面是我的实现方式。当中会用到一个函数strncmp。他的用处是比较两个字串的特定长度。

strncmp(string,"float(",6) 就是想比较一下float(4)和float(的前6位是否相同

 

/*
 * 描述:float(4)或者char(100)把括号前后的内容分别保存
 * 参数:string -- 整体字符串
 * 参数:type -- 解析后返回的type(float/char)(返回)
 * 参数:length -- 解析后返回的长度(返回)
 * 返回:返回0说明创建失败。返回1则创建成功
 */
int getColumnTypeLength(char * string, DataType * type, int * length)
{
	char * floatHead = "float(";
	char * charHead = "char(";
	//如果是float打头的
	if (strncmp(string,floatHead,strlen(floatHead))==0)
	{
		*type = floatType;
		string = string+strlen(floatHead);
		*length = getLength(string);
		return 1;
	}
	else if (strncmp(string,charHead,strlen(charHead))==0)
	{
		*type = stringType;
		string = string+strlen(charHead);
		*length = getLength(string);
		return 1;
	}
	else
	{
		printf("文件格式有误\n");
		return 0;
	}
}

 

解析完类型后,我又用了getLength来解析长度。

 

比如说我们传递进来的字符串是float(4),则string一开始指向字符串的头地址,也就是指向f

string = string+strlen(floatHead)) 就是把string指针往后挪6位,这样string就指向了4。string指向的字符串就是’4)‘

getLength(string)就是让getLength这个函数解析‘4)’这个字符串得到4这个数值。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics