⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 config.cpp

📁 WinLIRC软件:WinLIRC是一个以 LIRC为基础而在Windows环境发展出来的模块, 而什么是LIRC呢...它其实是 Linux InfraredRemote Control的缩写, 本
💻 CPP
📖 第 1 页 / 共 2 页
字号:

	else if (strcasecmp("plead",key)==0){
		rem->plead=s_strtoi(val);
	}

	else if (strcasecmp("ptrail",key)==0){
		rem->ptrail=s_strtoi(val);
	}

	else if (strcasecmp("foot",key)==0){
		rem->pfoot=s_strtoi(val);
		rem->sfoot=s_strtoi(val2);
	}

	else if (strcasecmp("repeat",key)==0){
		rem->prepeat=s_strtoi(val);
		rem->srepeat=s_strtoi(val2);
	}

	else if (strcasecmp("pre_data_bits",key)==0){
		rem->pre_data_bits=s_strtoi(val);
	}

	else if (strcasecmp("pre_data",key)==0){
		rem->pre_data=s_strtocode(val);
	}

	else if (strcasecmp("post_data_bits",key)==0){
		rem->post_data_bits=s_strtoi(val);
	}

	else if (strcasecmp("post_data",key)==0){
		rem->post_data=s_strtocode(val);
	}

	else if (strcasecmp("pre",key)==0){
		rem->pre_p=s_strtoi(val);
		rem->pre_s=s_strtoi(val2);
	}

	else if (strcasecmp("post",key)==0){
		rem->post_p=s_strtoi(val);
		rem->post_s=s_strtoi(val2);
	}

	else if (strcasecmp("gap",key)==0){
		rem->gap=s_strtoul(val);
	}
	
	else if (strcasecmp("repeat_gap",key)==0){
		rem->repeat_gap=s_strtoul(val);
	}

	else if (strcasecmp("toggle_bit",key)==0){
        rem->toggle_bit=s_strtoi(val);
	}
	/* obsolete name */
	else if (strcasecmp("repeat_bit",key)==0){
        rem->toggle_bit=s_strtoi(val);
	}
	else if (strcasecmp("min_repeat",key)==0){
        rem->min_repeat=s_strtoi(val);
	}
	else if (strcasecmp("frequency",key)==0){
        rem->freq=s_strtoui(val);
	}
	else if (strcasecmp("duty_cycle",key)==0){
        rem->duty_cycle=s_strtoui(val);
	/* WinLIRC ONLY! */
	}else if (strcasecmp("transmitter",key)==0){
        rem->transmitter=s_strtoui(val);
	}else{
		if(val2){
			DEBUG("error in configfile line %d:\n",line);
			DEBUG("unknown definition: \"%s %s %s\"\n",key,val,val2);
		}else{
			DEBUG("error in configfile line %d:\n",line);
			DEBUG("unknown definition: \"%s %s\"\n",key,val);
		}
		parse_error=1;
	}
}
    
struct ir_remote * read_config(FILE *f)
{
	char buf[LINE_LEN+1], *key, *val, *val2;
	int len;
	struct ir_remote *top_rem=NULL,*rem=NULL;
	struct void_array codes_list,raw_codes,signals;
	struct ir_ncode raw_code={NULL,0,0,NULL};
	struct ir_ncode name_code={NULL,0,0,NULL};
	int mode=ID_none;

	codes_list.ptr=raw_codes.ptr=signals.ptr=NULL;
	
	line=0;
	parse_error=0;
	
	while(!parse_error && fgets(buf,LINE_LEN,f)!=NULL)
	{
		line++;
		len=strlen(buf);
		if(len==LINE_LEN && buf[len-1]!='\n')
		{
			DEBUG("line %d too long in config file\n",line);
			parse_error=1;
			break;
		}
		
		/* ignore comments */
		len--;
		if(buf[len]=='\n') buf[len]=0;
		if(buf[0]=='#'){
			continue;
		}
		key=strtok(buf," \t");
		/* ignore empty lines */
		if(key==NULL) continue;
		val=strtok(NULL, " \t");
		if(val!=NULL){
			val2=strtok(NULL, " \t");
			if (strcasecmp("begin",key)==0){
				if (strcasecmp("codes", val)==0){
					/* init codes mode */
					if (!checkMode(mode, ID_remote,
						"begin codes")) { parse_error=1; continue; }
					if (rem->codes){
						DEBUG("error in configfile line %d:\n",line);
						DEBUG("codes are already defined\n");
						parse_error=1; continue;
					}
					
					init_void_array(&codes_list,30, sizeof(struct ir_ncode));
					mode=ID_codes;
				}
				else if(strcasecmp("raw_codes",val)==0){
					/* init raw_codes mode */
					if(!checkMode(mode, ID_remote,
						"begin raw_codes")) { parse_error=1; continue; }
					if (rem->codes){
						DEBUG("error in configfile line %d:\n",line);
						DEBUG("codes are already defined\n");
						parse_error=1; continue;
					}
					rem->flags|=RAW_CODES;
					raw_code.code=0;
					init_void_array(&raw_codes,30, sizeof(struct ir_ncode));
					mode=ID_raw_codes;
				}else if(strcasecmp("remote",val)==0){
					/* create new remote */
					if(!checkMode(mode, ID_none,
						"begin remote")) { parse_error=1; continue; }
					mode=ID_remote;
					if (!top_rem){
						/* create first remote */
						rem=top_rem=(struct ir_remote *)
							s_malloc(sizeof(struct ir_remote));
					}else{
						/* create new remote */
						rem->next=(struct ir_remote *)
							s_malloc(sizeof(struct ir_remote));;
						rem=rem->next;
					}
				}else if(mode==ID_codes){
					add_void_array(&codes_list, defineCode(key, val, &name_code));
				}else{
					DEBUG("error in configfile line %d:\n",line);
					DEBUG("unknown section \"%s\"\n",val);
					parse_error=1;
				}
			}else if (strcasecmp("end",key)==0){
				
				if (strcasecmp("codes", val)==0){
					/* end Codes mode */
					if (!checkMode(mode, ID_codes,
						"end codes")) { parse_error=1; continue; }
					rem->codes=(struct ir_ncode *)
						get_void_array(&codes_list);
					mode=ID_remote;     /* switch back */
					
				}else if(strcasecmp("raw_codes",val)==0){
					/* end raw codes mode */
					
					if(mode==ID_raw_name){
						raw_code.signals=(unsigned long *) //(ir_code *)
							get_void_array(&signals);
						raw_code.length=signals.nr_items;
						if(raw_code.length%2==0)
						{
							DEBUG("error in configfile line %d:\n",line);
							DEBUG("bad signal length\n");
							parse_error=1;
						}
						if(!add_void_array(&raw_codes, &raw_code))
						{ parse_error=1; continue; }
						mode=ID_raw_codes;
					}
					if(!checkMode(mode,ID_raw_codes,
						"end raw_codes")) { parse_error=1; continue; }
					rem->codes=(struct ir_ncode *)
						get_void_array(&raw_codes);
					mode=ID_remote;     /* switch back */
				}else if(strcasecmp("remote",val)==0){
					/* end remote mode */
					/* print_remote(rem); */
					if (!checkMode(mode,ID_remote,
						"end remote")) { parse_error=1; continue; }
					if (!rem->name){
						DEBUG("you must specify a remote name\n");
						parse_error=1; continue;
					}
					
					/* not really necessary because we
					clear the alloced memory */
					rem->next=NULL;
					rem->last_code=NULL;
					mode=ID_none;     /* switch back */
					if(has_repeat_gap(rem) && 
						is_const(rem))
					{
						DEBUG("WARNING: repeat_gap will be ignored if CONST_LENGTH flag is set\n");
					}
				}else if(mode==ID_codes){
					add_void_array(&codes_list, defineCode(key, val, &name_code));
				}else{
					DEBUG("error in configfile line %d:\n",line);
					DEBUG("unknown section \"%s\"\n",val);
					parse_error=1;
					
				}
			} else {
				
				switch (mode){
				case ID_remote:
					defineRemote(key, val, val2, rem);
					break;
				case ID_codes:
					add_void_array(&codes_list, defineCode(key, val, &name_code));
					break;
				case ID_raw_codes:
				case ID_raw_name:
					if(strcasecmp("name",key)==0){
						if(mode==ID_raw_name)
						{
							raw_code.signals=(unsigned long *) //(ir_code *)
								get_void_array(&signals);
							raw_code.length=signals.nr_items;
							if(raw_code.length%2==0)
							{
								DEBUG("error in configfile line %d:\n",line);
								DEBUG("bad signal length\n");
								parse_error=1;
							}
							if(!add_void_array(&raw_codes, &raw_code))
								{ parse_error=1; continue; }
						}
						if(!(raw_code.name=s_strdup(val))){
							{ parse_error=1; continue; }
						}
						raw_code.code++;
						init_void_array(&signals,50,sizeof(unsigned long));
						mode=ID_raw_name;
					}else{
						if(mode==ID_raw_codes)
						{
							DEBUG("no name for signal defined at line %d\n",line);
							{ parse_error=1; continue; }
						}
						if(!addSignal(&signals, key)) { parse_error=1; continue; }
						if(!addSignal(&signals, val)) { parse_error=1; continue; }
						if (val2){
							if (!addSignal(&signals, val2)){
								parse_error=1; continue;
							}
						}
						while ((val=strtok(NULL," \t"))){
							if (!addSignal(&signals, val)) { parse_error=1; continue; }
						}
					}
					break;
				}
			}
		}else if(mode==ID_raw_name){
			if(!addSignal(&signals, key)){
				{ parse_error=1; continue; }
			}
		}else{
			DEBUG("error in configfile line %d.\n",line);
			{ parse_error=1; continue; }
		}
	}
	if(mode!=ID_none)
	{
		switch(mode)
		{
		case ID_raw_name:
			   if(raw_code.name!=NULL)
			   {
					   free(raw_code.name);
					   if(get_void_array(&signals)!=NULL)
							   free(get_void_array(&signals));
			   }
		case ID_raw_codes:
			   rem->codes=(struct ir_ncode *)get_void_array(&raw_codes);
			   break;
		case ID_codes:
			   rem->codes=(struct ir_ncode *)get_void_array(&codes_list);
			   break;
		}
		if(!parse_error)
		{
			   DEBUG("unexpected end of file\n");
			   parse_error=1;
		}
	}
	if (parse_error){
		free_config(top_rem);
		top_rem=NULL;
	}

	return (top_rem);
}

void free_config(struct ir_remote *remotes)
{
	struct ir_remote *next;
	struct ir_ncode *codes;
	
	while(remotes!=NULL)
	{
		next=remotes->next;

		if(remotes->name!=NULL) 
			free(remotes->name);
		if(remotes->codes!=NULL)
		{
			codes=remotes->codes;
			while(codes->name!=NULL)
			{
				free(codes->name);
				if(codes->signals!=NULL)
					free(codes->signals);
				codes++;
			}
			free(remotes->codes);
		}
		free(remotes);
		remotes=next;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -