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

📄 config.cpp

📁 WinLIRC软件:WinLIRC是一个以 LIRC为基础而在Windows环境发展出来的模块, 而什么是LIRC呢...它其实是 Linux InfraredRemote Control的缩写, 本
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* 
 * This file is part of the WinLIRC package, which was derived from
 * LIRC (Linux Infrared Remote Control) 0.5.4pre9.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Copyright (C) 1998 Pablo d'Angelo <pablo@ag-trek.allgaeu.org>
 * Copyright (C) 1999 Jim Paris <jim@jtan.com>
 */

#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

#include "winlirc.h"
#include "remote.h"
#include "config.h"
#include "globals.h"

void **init_void_array(struct void_array *ar,size_t chunk_size, size_t item_size)
{
	ar->chunk_size=chunk_size;
	ar->item_size=item_size;
	ar->nr_items=0;
	if(!(ar->ptr=calloc(chunk_size, ar->item_size))){
		DEBUG("out of memory\n");
		//free(ar->ptr);
		parse_error=1;
		return(NULL);
	}
	return((void **)(ar->ptr));
}

int add_void_array (struct void_array *ar, void * dataptr)
{
	void *ptr;
	
	if ((ar->nr_items%ar->chunk_size)==(ar->chunk_size)-1){
		/* I hope this works with the right alignment,
		if not we're screwed */
		if (!(ptr=realloc(ar->ptr,ar->item_size*((ar->nr_items)+(ar->chunk_size+1))))){
			DEBUG("out of memory\n");
			parse_error=1;
			return(0);
		}
		ar->ptr=ptr;
	}
	memcpy((char *)(ar->ptr)+(ar->item_size*ar->nr_items), dataptr, ar->item_size);
	ar->nr_items=(ar->nr_items)+1;
	memset((char *)(ar->ptr)+(ar->item_size*ar->nr_items), 0, ar->item_size);
	return(1);
}

inline void *get_void_array(struct void_array *ar)
{
    return(ar->ptr);
}

void *s_malloc(size_t size)
{
    void *ptr;
    if((ptr=malloc(size))==NULL){
        DEBUG("out of memory\n");
        parse_error=1;
        return(NULL);
    }
    memset(ptr, 0, size);
    return (ptr);
}

inline char *s_strdup(char * string)
{
    char *ptr;
    if(!(ptr=strdup(string))){
        DEBUG("out of memory\n");
        parse_error=1;
        return(NULL);
    }
    return (ptr);
}

/* my very own strtouq */
unsigned __int64 strtouq(char *val, char **endptr, int base)
{
	while(*val=='\t' || *val==' ') val++;
	if(base==0)
		if(val[0]=='0')
			if(val[1]=='x' || val[1]=='X')
			{
				base=16;
				val+=2;
			}
			else
			{
				val++;
				base=8;
			}
		else
			base=10;
	
	char convert[256];
	for(int i=0;i<255;i++)
	{
		if(i>='0' && i<='9') convert[i]=i-'0';
		else if(i>='a' && i<='f') convert[i]=(i-'a')+10;
		else if(i>='A' && i<='F') convert[i]=(i-'A')+10;
		else convert[i]=-1;
	}

	unsigned __int64 result=0;
	while(*val && convert[*val]!=-1)
	{
		result*=base;
		result+=convert[*val];
		val++;
	}
	*endptr=val;
	return result;
}


inline ir_code s_strtocode(char *val)
{
	ir_code code=0;
	char *endptr;

	errno=0;
#ifdef LONG_IR_CODE
	code=strtouq(val,&endptr,0);
	if((code==(unsigned __int64) -1 && errno==ERANGE) ||
	    strlen(endptr)!=0 || strlen(val)==0)
	{
		DEBUG("error in configfile line %d:\n",line);
		DEBUG("\"%s\": must be a valid (unsigned long long) number\n",val);
		parse_error=1;
		return(0);
	}
#else
	code=strtoul(val,&endptr,0);
	if(code==ULONG_MAX && errno==ERANGE)
	{
		DEBUG("error in configfile line %d\n",line);
		DEBUG("code is out of range\n");
		DEBUG("try compiling lircd with the LONG_IR_CODE option\n");
		parse_error=1;
		return(0);
	}
	else if(strlen(endptr)!=0 || strlen(val)==0)
	{
		DEBUG("error in configfile line %d:\n",line);
		DEBUG("\"%s\": must be a valid (unsigned long) number\n",val);
		parse_error=1;
		return(0);
	}
#endif
	return(code);
}

unsigned long s_strtoul(char *val)
{
	unsigned long n;
	char *endptr;

	errno=0;
	n=strtoul(val,&endptr,0);
	if((n==ULONG_MAX && errno==ERANGE)
	   || strlen(endptr)!=0 || strlen(val)==0)
	{
		DEBUG("error in configfile line %d:\n",line);
		DEBUG("\"%s\": must be a valid (unsigned long) number\n",val);
		parse_error=1;
		return(0);
	}
	return(n);
}

int s_strtoi(char *val)
{
	char *endptr;
	long n;
	int h;
	
	errno=0;
	n=strtol(val,&endptr,0);
	h=(int) n;
	if(((n==LONG_MAX || n==LONG_MIN) && errno==ERANGE)
	   || strlen(endptr)!=0 || strlen(val)==0 || n!=((long) h))
	{
		DEBUG("error in configfile line %d:\n",line);
		DEBUG("\"%s\": must be a valid (int) number\n",val);
		parse_error=1;
		return(0);
	}
	return(n);
}

unsigned int s_strtoui(char *val)
{
        char *endptr;
        unsigned long n;
        unsigned int h;

        errno=0;
		n=strtoul(val,&endptr,0);
        h=(unsigned int) n;
		if((n==ULONG_MAX && errno==ERANGE)
		   || strlen(endptr)!=0 || strlen(val)==0 || n!=((unsigned long) h))
        {
                DEBUG("error in configfile line %d:\n",line);
                DEBUG("\"%s\": must be a valid (unsigned int) number\n",val);
                parse_error=1;
                return(0);
        }
        return(n);
}

int checkMode(int is_mode, int c_mode, char *error)
{

    if (is_mode!=c_mode)
	{
		DEBUG("error in configfile line %d:\n",line);
		DEBUG("\"%s\" isn磘 valid at this position\n",error);
		parse_error=1;
		return(0);
	}
    return(1);
}

int addSignal(struct void_array *signals, char *val)
{
	unsigned long t;
	
	t=s_strtoul(val);
	if (parse_error) return(0);
	if (!add_void_array(signals, &t)){
		return(0);
	}
    return(1);
}
	      
struct ir_ncode *defineCode(char *key, char *val, struct ir_ncode *code)
{
        code->name=s_strdup(key);
        code->code=s_strtocode(val);
        return(code);
}

int parseFlags(char *val,char *val2)
{
        struct flaglist *flaglptr;
	int flags=0;
	char *flag,*help;

	if(val2!=NULL)
	{
		DEBUG("error in configfile line %d:\n",line);
		DEBUG("bad flags \"%s %s\"\n",val,val2);
		parse_error=1;
		return(0);
	}
	
	flag=help=val;


	while(flag!=NULL)
	{
		while(*help!='|' && *help!=0) help++;
		if(*help=='|')
		{
			*help=0;help++;
		}
		else
		{
			help=NULL;
		}
	
		flaglptr=all_flags;
		while(flaglptr->name!=NULL){
			if(strcasecmp(flaglptr->name,flag)==0){
				flags=flags|flaglptr->flag;
				break;
			}
			flaglptr++;
		}
		if(flaglptr->name==NULL)
		{
			DEBUG("error in configfile line %d:\n",line);
			DEBUG("unknown flag: \"%s\"\n",flag);
			parse_error=1;
			return(0);
		}
		flag=help;
	}

        return(flags);
}

void defineRemote(char * key, char * val, char *val2, struct ir_remote *rem)
{
	if ((strcasecmp("name",key))==0){
		if(rem->name!=NULL) free(rem->name);
		rem->name=s_strdup(val);
	}

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

	else if (strcasecmp("flags",key)==0){
		rem->flags|=parseFlags(val,val2);
	}

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

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

	else if (strcasecmp("header",key)==0){
		rem->phead=s_strtoi(val);
		rem->shead=s_strtoi(val2);
	}

	else if (strcasecmp("one",key)==0){
		rem->pone=s_strtoi(val);
		rem->sone=s_strtoi(val2);
	}

	else if (strcasecmp("zero",key)==0){
		rem->pzero=s_strtoi(val);
		rem->szero=s_strtoi(val2);
	}

⌨️ 快捷键说明

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