简单的从调试配置文件获取调试参数

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include <sys/stat.h>
#include <fcntl.h>

#define DEBUG_FILE1 "/tmp/dbg_param.txt"
#define DEBUG_FILE2 "/tmp/mnt/sdcard/dbg_param.txt"


typedef enum {
	DBG_VALUE_TYPE_INT,
	DBG_VALUE_TYPE_BOOL,
	DBG_VALUE_TYPE_STRING,
	DBG_VALUE_TYPE_FLOAT,
}DBG_VALUE_TYPE;



int debug_getinfo(char* key, char** value, DBG_VALUE_TYPE type)
{

	if (!key || !value)
	{
		printf ("para is null\n");
		return -1;
	}

	if (type > DBG_VALUE_TYPE_FLOAT || type < DBG_VALUE_TYPE_INT)
	{
		printf ("value type error\n");
		return -1;
	}
	
	FILE * fp1; 
	FILE * fp2;
	
	bool check1 = false;
	bool check2 = false;
	
	int ret = access(DEBUG_FILE1, F_OK);
	if (ret == 0 && (fp1 = fopen(DEBUG_FILE1, "r")))
	{
		check1 = true;	
	}

	ret = access(DEBUG_FILE2, F_OK);
	if (ret == 0 && (fp2 = fopen(DEBUG_FILE2, "r")))
	{
		check2 = true;	
	}


	char *line = NULL;
	char*    p = NULL;
	char* goal = NULL;
	
	size_t len = 0;
	ssize_t read;
	
	if (check1)
	{
		while ((read = getline(&line, &len, fp1)) != -1) 
		{
			printf("Retrieved line of length %zu :\n", read);
			printf("%s", line);
			p = strstr(line, key);
			if (p)
			{
				p += strlen(key);
				printf ("find the %s\n", key);
				
				for ( ; *p == ' ' || *p == '\t';p++);

				if (*p++ == '=')
				{
					goal = (char*)calloc(1, strlen(p)+1);
					strcpy(goal, p);
					*value = goal;
					printf("Retrieved line of length %zu :\n", read);
					printf("%s", line);
					break;
				}
			}
		}
		
		free(line);
		fclose(fp1);
		if (goal)
		{
			printf ("find the key:%s value:%s at %s\n",key, *value, DEBUG_FILE1);
			return 0;
		}
	}


	line = NULL;
	p = NULL;
	goal = NULL;	
	len = 0;

	if (check2)
	{
		while ((read = getline(&line, &len, fp2)) != -1) 
		{
			printf("Retrieved line of length %zu :\n", read);
			printf("%s", line);
			p = strstr(line, key);
			if (p)
			{
				p += strlen(key);
				for ( ; *p == ' ' || *p == '\t';p++);

				if (*p++ == '=')
				{
					goal = (char*)calloc(1, strlen(p)+1);
					strcpy(goal, p);
					*value = goal;
					printf("Retrieved line of length %zu :\n", read);
					printf("%s", line);
					break;
				}
			}
		}

		free(line);
		fclose(fp2);
		if (goal)
		{
			printf ("find the key:%s value:%s at %s\n",key, *value, DEBUG_FILE1);
			return 0;
		}
	}

	printf ("not find the the %s\n", key);
	return -1;	
}

    这就是读一个指定路径下的指定配置文件,来获取配置参数。

例如:

	ret = debug_getinfo("cameraAngle", &vstr, DBG_VALUE_TYPE_FLOAT);
	if (ret == 0 && vstr)
	{
		f_ca = atof(vstr);
		free (vstr);
		vstr = NULL;
	}

配置文件:

cameraAngle=44.4    标定餐数
cameraHeight=111.1  标定参数
fl=222.2        标定参数
debug=1        是否获取图灵后台上传的原始图和调整后的图
takepicture=1  是否使能抓图放在本地

这个还是比较方便的,你可以在程序的任何地方可以设置控制点,后改变参数。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值