Пример #1
0
void update_ini_file(char *ini_name)
{
    dictionary  *ini ;
    char bstr[33];
    FILE *fini ;

    ini = iniparser_load(ini_name);
    if (ini==NULL){
        logErrDate("update_ini_file: cannot parse ini-file: %s!\n Exit!\n", ini_name);
        exit_all(-1);
    }
    sprintf(bstr,"%i",pga[0]);
    iniparser_set(ini, "default:pga_ch1", bstr);    
    
    sprintf(bstr,"%i",pga[1]);
    iniparser_set(ini, "default:pga_ch2", bstr);

    sprintf(bstr,"%i",pga[2]);
    iniparser_set(ini, "default:pga_ch3", bstr);

    sprintf(bstr,"%i",pga[3]);
    iniparser_set(ini, "default:pga_ch4", bstr);

    fini = fopen(ini_name, "w");
    if (fini==NULL){
        logErrDate("update_ini_file: cannot open ini-file %s to update!\nExit!\n", ini_name);
        exit_all(-1);
    }
    fprintf(fini,"%s\n",ini_comments);
    iniparser_dumpsection_ini(ini, "default", fini);
    iniparser_dumpsection_ini(ini, "output", fini);
    fclose(fini);

    iniparser_freedict(ini);
}
Пример #2
0
void Test_dictionary_wrapper(CuTest *tc)
{
    dictionary *dic;

    dic = dictionary_new(10);

    CuAssertIntEquals(tc, -1, iniparser_set(dic, NULL, NULL));
    CuAssertIntEquals(tc, -1, iniparser_set(NULL, "section", "value"));

    CuAssertIntEquals(tc, 0, iniparser_set(dic, "section", NULL));
    CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key", "value"));

    CuAssertStrEquals(tc, "value", iniparser_getstring(dic, "section:key", NULL));
    /* reset the key's value*/
    CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key", NULL));
    CuAssertStrEquals(tc, NULL, iniparser_getstring(dic, "section:key", "dummy"));
    CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key", "value"));
    CuAssertStrEquals(tc, "value", iniparser_getstring(dic, "section:key", NULL));

    iniparser_unset(dic, "section:key");
    CuAssertStrEquals(tc, "dummy", iniparser_getstring(dic, "section:key", "dummy"));
    CuAssertStrEquals(tc, NULL, iniparser_getstring(dic, "section", "dummy"));

    CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key", NULL));
    CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key1", NULL));
    CuAssertIntEquals(tc, 0, iniparser_set(dic, "section:key2", NULL));

    iniparser_unset(dic, "section");
    CuAssertStrEquals(tc, NULL, iniparser_getstring(dic, "section", NULL));

    iniparser_freedict(dic);
}
Пример #3
0
void update_config(char *token, char *username)
{
    bstring file = locate_config_file();

    bstring tmp = bstrcpy(file);
    bcatcstr(tmp, ".tmp_XXXXXX");

    int fd = mkstemp((char *) tmp->data);
    if (fd < 0)
    {
	perror("mkstemp");
	exit(1);
    }
    FILE *fp = fdopen(fd, "w");
    if (fp == 0)
    {
	perror("fdopen");
	exit(1);
    }

    dictionary *config;

    struct stat statbuf;
    int rc = stat((char *) file->data, &statbuf);
    if (rc < 0 || statbuf.st_size == 0)
    {
	/* create a new empty dictionary */
	config = dictionary_new(0);
	dictionary_set(config, "authentication", 0);
    }
    else
    {
	config = iniparser_load(bdata(file));
    }

    iniparser_set(config, "authentication:token", token);
    if (username)
	iniparser_set(config, "authentication:user_id", username);
    iniparser_dump_ini(config, fp);

    iniparser_freedict(config);

    fclose(fp);

    if (rename(bdata(tmp), bdata(file)) < 0)
    {
	fprintf(stderr, "Error rename %s to %s: %s\n",
		bdata(tmp), bdata(file), strerror(errno));
	exit(1);
    }

    bdestroy(tmp);
    bdestroy(file);
}
Пример #4
0
void camera_control_backup_system_settings(CameraControl* cc, const char* file) {
    int AutoAEC = 0;
    int AutoAGC = 0;
    int Gain = 0;
    int Exposure = 0;
    int Contrast = 0;
    int Brightness = 0;

    int fd = open_v4l2_device(cc->cameraID);

    if (fd != -1) {
        AutoAEC = v4l2_get_control(fd, V4L2_CID_EXPOSURE_AUTO);
        AutoAGC = v4l2_get_control(fd, V4L2_CID_AUTOGAIN);
        Gain = v4l2_get_control(fd, V4L2_CID_GAIN);
        Exposure = v4l2_get_control(fd, V4L2_CID_EXPOSURE);
        Contrast = v4l2_get_control(fd, V4L2_CID_CONTRAST);
        Brightness = v4l2_get_control(fd, V4L2_CID_BRIGHTNESS);
        v4l2_close(fd);

        dictionary* ini = dictionary_new(0);
        iniparser_set(ini, "PSEye", 0);
        iniparser_set_int(ini, "PSEye:AutoAEC", AutoAEC);
        iniparser_set_int(ini, "PSEye:AutoAGC", AutoAGC);
        iniparser_set_int(ini, "PSEye:Gain", Gain);
        iniparser_set_int(ini, "PSEye:Exposure", Exposure);
        iniparser_set_int(ini, "PSEye:Contrast", Contrast);
        iniparser_set_int(ini, "PSEye:Brightness", Brightness);
        iniparser_save_ini(ini, file);
        dictionary_del(ini);
    }
}
Пример #5
0
int modify_cfg(char *section, char *key, char *value)
{
	char *query = malloc(strlen(section) + strlen(key) + strlen(":") + 1);
	if (section == NULL) {
		write_to_log(WARNING, "Unable to modify config, parameter is NULL - Section");
		goto err;
	} else if (key == NULL) {
		write_to_log(WARNING, "Unable to modify config, parameter is NULL - Key");
		goto err;
	} else if (value == NULL) {
		write_to_log(WARNING, "Unable to modify config, parameter is NULL - Value");
		goto err;
	}
	if (!snprintf(query, (strlen(section) + strlen(key) + strlen(":") + 1), "%s:%s", section, key)) {
		write_to_log(WARNING, "%s - %d - %s - %s:%s", __LINE__, __func__, "Unable to set config query for", section, key);
		goto err;
	}
	if (iniparser_set(ini, query, value) != 0) {
		write_to_log(WARNING, "Unable to modify config");
		goto err;
	}
	free(query);
	return 0;
err:
	free(query);
	return -1;
}
Пример #6
0
bool parameters_set_integer(struct parameters *params, const char *key, int value)
{
	char buffer[32];

	if (!parameters_check_key(params, key)) return false;

	snprintf(buffer, 32, "%i", value);
	return iniparser_set(params->iniparser_dict, params->key, buffer) != 0;
}
Пример #7
0
int faptime_config_set_table(struct faptime_table *ft) {
    if (config == NULL) {
        return FT_NO_CONFIG;
    }
    const char *pretty_table = faptime_table_tostring(ft);
    if (0 != iniparser_set(config, CFG_TABLE, pretty_table)) {
        return FT_BAD_CONFIG;
    }
    return FT_CONFIG_OK;
}
Пример #8
0
bool WriteDictString(dictionary *dict, const char *section, const string &str,
    string &err)
{
    int r = iniparser_set((struct _dictionary_ *)dict, section, str.c_str());
    if (-1 == r) {
        char buff[128];
        snprintf(buff, sizeof(buff) - 1, "cannot write %s into config file", section);
        err = buff;
        return false;
    }
    return true;
}
Пример #9
0
int flib_ini_set_str(flib_ini *ini, const char *key, const char *value) {
	int result = INI_ERROR_OTHER;
	if(!log_badargs_if4(ini==NULL, ini->currentSection==NULL, key==NULL, value==NULL)) {
		char *dictKey = createDictKey(ini->currentSection, key);
		if(dictKey) {
			result = iniparser_set(ini->inidict, dictKey, value);
			if(result) {
				flib_log_e("Error setting ini entry %s to %s", dictKey, value);
			}
		}
		free(dictKey);
	}
	return result;
}
int unpack_sequence(int r, int c,t_driver dinfo,struct SeqBuf *pulseseq,unsigned int *seq_buf) {
  int i,j,rep,rep_usec,code,steps,count;
  int state_time_usec=0;
  struct timeval t0,t1;
  char *time_string,command_string[80],value[80],entry_string[80];
  int strlength=0;
  gettimeofday(&t0,NULL);
  if(strcmp(dinfo.type,"TIMING")==0) {
    state_time_usec=dinfo.info.timing.state_time_usec;
  } else if (strcmp(dinfo.type,"DDS")==0){
    state_time_usec=dinfo.info.dds.state_time_usec;
  } else {
    iniparser_set(diagnostic_INI,"unpack_sequence", NULL,NULL);
    iniparser_set(diagnostic_INI,"unpack_sequence:err_string", "Bad driver type",NULL);
    return 0; 
  }
  rep_usec=pulseseq->prm.step_usec;
  if(verbose > 1 ) printf("Unpack: %d %d\n",pulseseq->prm.len,dinfo.max_seq_length);
  if(verbose > 1 ) printf("Unpack: driver_state_time %d seq_state_time %d\n",state_time_usec,rep_usec);
  count=0;

  for(i=0;i<pulseseq->prm.len;i++) {
    rep=pulseseq->rep[i];
    steps=((double)rep*(double)rep_usec)/(double)state_time_usec;
    code=pulseseq->code[i];
    if(verbose > 2 ) printf("%d :: Steps: %d Code %d\n",i,steps,code);
    if(count<dinfo.max_seq_length) {
      for(j=0;j<steps;j++) {
        seq_buf[count]=code;
        count++;
      }
    } else {
      iniparser_set(diagnostic_INI,"unpack_sequence", NULL,NULL);
      iniparser_set(diagnostic_INI,"unpack_sequence:err_string", "sequence too long to process",NULL);
      return 0;
    }
  }
  gettimeofday(&t1,NULL);

  iniparser_set(diagnostic_INI,"unpack_sequence", NULL,NULL);
  sprintf(entry_string,"%s:%s",command_string,"elapsed_secs");
  iniparser_set(diagnostic_INI,entry_string, "0.0",NULL);
  sprintf(entry_string,"%s:%s",command_string,"time_stamp");
  time_string=ctime(&t0.tv_sec);
  strlength=strlen(time_string)-1;
  strncpy(value,time_string,strlength);
  value[strlength]='\0';
  iniparser_set(diagnostic_INI,entry_string, value,NULL);


  return count;
}
Пример #11
0
int flib_ini_create_section(flib_ini *ini, const char *section) {
	int result = INI_ERROR_OTHER;
	if(!log_badargs_if2(ini==NULL, section==NULL)) {
		result = flib_ini_enter_section(ini, section);
		if(result == INI_ERROR_NOTFOUND) {
			if(iniparser_set(ini->inidict, section, NULL)) {
				flib_log_e("Error creating ini section %s", section);
				result = INI_ERROR_OTHER;
			} else {
				result = flib_ini_enter_section(ini, section);
			}
		}
	}
	return result;
}
Пример #12
0
int main(int argc, char**argv) {
    init(argc, argv);

    if(iniparser_getboolean(gConfig, ":visual", false) == true) {
        // doing it visually
        printf("Visual\n");
        doVisual();
    } else {
        // set logging to true
        iniparser_set(gConfig, ":log", "true");
        // run the simulation
        doConsole();
    }

    iniparser_freedict(gConfig);
    delete dir_distrib;
}
Пример #13
0
static int change_to_usb_config(char *stb_inipath)
{
	int j = 0;
	dictionary *ini = NULL;

	cfg_debug("%s\n", stb_inipath);
	ini = iniparser_load(stb_inipath);
	for (j = 0; j < g_usb_config_line_count; j++) {
		if (strstr(stb_inipath, g_usb_config_name[j])) {
			cfg_debug("set %s to %s in %s\n",
				g_usb_config_cmd[j], g_usb_config_value[j], stb_inipath);
			iniparser_set(ini, g_usb_config_cmd[j], g_usb_config_value[j]);
		}
	}
	ini_dump(ini, stb_inipath);
	return FV_OK;
}
Пример #14
0
int
config_load(void)
{
	int n;

	config = iniparser_load("spoold.conf");
	if(!config)
	{
		return -1;
	}
	for(n = 0; n < overrides->n; n++)
	{
		iniparser_set(config, overrides->key[n], overrides->val[n]);
	}
	dictionary_del(overrides);
	overrides = NULL;
	return 0;
}
Пример #15
0
void INI::set(std::string what, std::string value)
{
	size_t pos = what.find(':');
	if (pos != std::string::npos)
	{
		// User's adding a section - like "section:key".
		// Let's make sure it exists
		std::string section = what.substr(0, pos);

		if (iniparser_find_entry(this->ini, section.c_str()) == 0)
		{
			// Doesn't exist - creating...
			this->set(section, "");
		}
	}

	iniparser_set(this->ini, what.c_str(), value.c_str());
}
Пример #16
0
void camera_control_backup_system_settings(CameraControl* cc, const char* file) {
#if !defined(CAMERA_CONTROL_USE_CL_DRIVER) && !defined(CAMERA_CONTROL_USE_PS3EYE_DRIVER) && defined(PSMOVE_USE_PSEYE)
	HKEY hKey;
	DWORD l = sizeof(DWORD);
	DWORD AutoAEC = 0;
	DWORD AutoAGC = 0;
	DWORD AutoAWB = 0;
	DWORD Exposure = 0;
	DWORD Gain = 0;
	DWORD wbB = 0;
	DWORD wbG = 0;
	DWORD wbR = 0;
	char* PATH = CL_DRIVER_REG_PATH;
	int err = RegOpenKeyEx(HKEY_CURRENT_USER, PATH, 0, KEY_ALL_ACCESS, &hKey);
	if (err != ERROR_SUCCESS) {
		printf("Error: %d Unable to open reg-key:  [HKCU]\\%s!", err, PATH);
		return;
	}
	RegQueryValueEx(hKey, "AutoAEC", NULL, NULL, (LPBYTE) &AutoAEC, &l);
	RegQueryValueEx(hKey, "AutoAGC", NULL, NULL, (LPBYTE) &AutoAGC, &l);
	RegQueryValueEx(hKey, "AutoAWB", NULL, NULL, (LPBYTE) &AutoAWB, &l);
	RegQueryValueEx(hKey, "Exposure", NULL, NULL, (LPBYTE) &Exposure, &l);
	RegQueryValueEx(hKey, "Gain", NULL, NULL, (LPBYTE) &Gain, &l);
	RegQueryValueEx(hKey, "WhiteBalanceB", NULL, NULL, (LPBYTE) &wbB, &l);
	RegQueryValueEx(hKey, "WhiteBalanceG", NULL, NULL, (LPBYTE) &wbG, &l);
	RegQueryValueEx(hKey, "WhiteBalanceR", NULL, NULL, (LPBYTE) &wbR, &l);

	dictionary* ini = dictionary_new(0);
	iniparser_set(ini, "PSEye", 0);
	iniparser_set_int(ini, "PSEye:AutoAEC", AutoAEC);
	iniparser_set_int(ini, "PSEye:AutoAGC", AutoAGC);
	iniparser_set_int(ini, "PSEye:AutoAWB", AutoAWB);
	iniparser_set_int(ini, "PSEye:Exposure", Exposure);
	iniparser_set_int(ini, "PSEye:Gain", Gain);
	iniparser_set_int(ini, "PSEye:WhiteBalanceB", wbB);
	iniparser_set_int(ini, "PSEye:WhiteBalanceG", wbG);
	iniparser_set_int(ini, "PSEye:WhiteBalanceR", wbG);
	iniparser_save_ini(ini, file);
	dictionary_del(ini);
#endif
}
Пример #17
0
void Test_iniparser_getboolean(CuTest *tc)
{
    unsigned i;
    char key_name[64];

    dictionary *dic;
    const char *token_true[] = {
        "1",
        "true",
        "t",
        "TRUE",
        "T",
        "yes",
        "y",
        "YES"
        "Y",
        NULL
    };
    const char *token_false[] = {
        "0",
        "false",
        "f",
        "FALSE",
        "F",
        "no",
        "n",
        "NO",
        "N",
        NULL
    };

    /* NULL test */
    CuAssertIntEquals(tc, 1, iniparser_getboolean(NULL, NULL, 1));
    CuAssertIntEquals(tc, 1, iniparser_getboolean(NULL, "dummy", 1));

    /* Check the def return element */
    dic = dictionary_new(10);
    CuAssertIntEquals(tc, 1, iniparser_getboolean(dic, "dummy", 1));
    CuAssertIntEquals(tc, 0, iniparser_getboolean(dic, NULL, 0));
    CuAssertIntEquals(tc, 1, iniparser_getboolean(dic, "dummy", 1));

    for (i = 0; token_true[i] != NULL; ++i) {
        sprintf(key_name, "bool:true%d", i);
        iniparser_set(dic, key_name, token_true[i]);
    }
    for (i = 0; token_false[i] != NULL; ++i) {
        sprintf(key_name, "bool:false%d", i);
        iniparser_set(dic, key_name, token_false[i]);
    }

    for (i = 0; token_true[i] != NULL; ++i) {
        sprintf(key_name, "bool:true%d", i);
        CuAssertIntEquals(tc, 1, iniparser_getboolean(dic, key_name, 0));
    }
    for (i = 0; token_false[i] != NULL; ++i) {
        sprintf(key_name, "bool:false%d", i);
        CuAssertIntEquals(tc, 0, iniparser_getboolean(dic, key_name, 1));
    }

    /* Test bad boolean */
    iniparser_set(dic, "bool:bad0", "");
    iniparser_set(dic, "bool:bad1", "m'kay");
    iniparser_set(dic, "bool:bad2", "42");
    iniparser_set(dic, "bool:bad3", "_true");
    CuAssertIntEquals(tc, 0xFF, iniparser_getboolean(dic, "bool:bad0", 0xFF));
    CuAssertIntEquals(tc, 0xFF, iniparser_getboolean(dic, "bool:bad1", 0xFF));
    CuAssertIntEquals(tc, 0xFF, iniparser_getboolean(dic, "bool:bad2", 0xFF));
    CuAssertIntEquals(tc, 0xFF, iniparser_getboolean(dic, "bool:bad3", 0xFF));

    dictionary_del(dic);
}
Пример #18
0
int iniparser_set_double(dictionary * ini, const char * entry, double val) {
	char str[256];
	sprintf(str, "%f", val);
	return iniparser_set(ini, entry, str);
}
Пример #19
0
int iniparser_set_int(dictionary * ini, const char * entry, int val) {
	char str[256];
	sprintf(str, "%d", val);
	return iniparser_set(ini, entry, str);
}
Пример #20
0
bool parameters_set_string(struct parameters *params, const char *key, const char *value)
{
	if (!parameters_check_key(params, key)) return false;
	return iniparser_set(params->iniparser_dict, params->key, value) != 0;
}
Пример #21
0
bool parameters_set_boolean(struct parameters *params, const char *key, bool value)
{
	if (!parameters_check_key(params, key)) return false;
	return iniparser_set(params->iniparser_dict, params->key, value ? "true" : "false") != 0;
}
Пример #22
0
/*******************  FUNCTION  *********************/
void CMRCmdOptions::setupDumpEntry ( const std::string& key, const char* value )
{
	iniparser_set(dumpUsedDic,extractSectionName(key).c_str(),NULL);
	iniparser_set(dumpUsedDic,key.c_str(),value);
}
Пример #23
0
int
config_set(const char *key, const char *value)
{
	iniparser_set((overrides ? overrides : config), key, value);
	return 0;
}
Пример #24
0
Файл: util.c Проект: odmann/mca
int
parse_ini_file(char *key, char *def, char *val, char *res)
{
	FILE		*fd;
	dictionary	*d;
	int		ini_update = 0;
	char		section[256] = {0};

	switch( check_inifile() ) {
	case 0:
		return 0;
	case 1:
		return 1;
	case 2:
		ini_cur = ini_default;
		d = iniparser_load(ini_default);
		if (d) {
			fd = fopen(ini_name, "w");
			if (fd) {
				iniparser_dump_ini(d, fd);
				fclose(fd);
				iniparser_freedict(d);
				ini_cur = ini_name;
				ini_update = 1;
				break;
			}
		} else {
			fprintf(stderr, "cannot parse file: %s\n",ini_cur);
		}
		return 0;
		break;
	case 3:
		ini_cur = ini_name;
		break;
	case 4:
		ini_cur = "/mnt/sd/mcaboot.ini";
		break;
	default:
		break;
	}

	d = iniparser_load(ini_cur);
	if (!d) {
		fprintf(stderr, "cannot parse file: %s\n", ini_cur);
		return -1;
	}

	if (key && val) {
		char *s = iniparser_getstring(d, key, NULL);
		if (s) {
			if (strcmp(s, val)) {
				iniparser_set(d, key, val);
				ini_update = 1;
			}
		} else {
			iniparser_get_section_string(key, section);
			iniparser_set(d, section, NULL);
			iniparser_set(d, key, val);
			ini_update = 1;
		}
	}

	if (key && res) {
		char *s = iniparser_getstring(d, key, def);
		strcpy(res, s);
	}

	if (ini_update) {
		fd = fopen(ini_name, "w");
		if (fd) {
			iniparser_dump_ini(d, fd);
			fflush(fd);
			fclose(fd);
		}
	}
	iniparser_freedict(d);
	return 0;
}
Пример #25
0
/*  ...
 */
void CBaIniParse::TwistedIni() {

   IBaIniParser *pNewHdl = 0;
   dictionary   *pOrgHdl = 0;
   std::string   tmp;

   CPPUNIT_ASSERT(!IBaIniParserCreate("You/Shall/Not/Path"));
   CPPUNIT_ASSERT(!iniparser_load("You/Shall/Not/Path"));
   CPPUNIT_ASSERT(!IBaIniParserCreate(ERRORINI));
   CPPUNIT_ASSERT(!iniparser_load(ERRORINI));

   CPPUNIT_ASSERT(!iniparser_load(OFKEYINI));
   CPPUNIT_ASSERT(!IBaIniParserCreate(OFKEYINI));


   CPPUNIT_ASSERT(!IBaIniParserCreate(OFVALINI));
   CPPUNIT_ASSERT(!iniparser_load(OFVALINI));

   pNewHdl = IBaIniParserCreate(TWISTEDINI);
   pOrgHdl = iniparser_load(TWISTEDINI);
   CPPUNIT_ASSERT(pNewHdl);
   CPPUNIT_ASSERT(pOrgHdl);

   pNewHdl->Dump(stdout);
   std::cout << "a==============================================================\n\n";
   iniparser_dump(pOrgHdl, stdout);
   std::cout << "a==============================================================\n\n";

   pNewHdl->DumpIni(stdout);
   std::cout << "a==============================================================\n\n";
   iniparser_dump_ini(pOrgHdl, stdout);


   CPPUNIT_ASSERT_EQUAL(
         (bool) iniparser_find_entry(pOrgHdl, "open["),
         pNewHdl->Exists("open["));

   // Multi-lines
   tmp = pNewHdl->GetString("multi:multi line key", "");
   CPPUNIT_ASSERT_MESSAGE(tmp, tmp == "multi line value");
   tmp = pNewHdl->GetString("multi:visible", "");
   CPPUNIT_ASSERT_MESSAGE(tmp, tmp == "1");
   tmp = pNewHdl->GetString("multi:a", "");
   CPPUNIT_ASSERT_MESSAGE(tmp, tmp == "beginend");
   tmp = pNewHdl->GetString("multi:c", "");
   CPPUNIT_ASSERT_MESSAGE(tmp, tmp == "begin  end");

   CPPUNIT_ASSERT(IBaIniParserDestroy(pNewHdl));
   iniparser_freedict(pOrgHdl);
   pNewHdl = 0;
   pOrgHdl = 0;

   pNewHdl = IBaIniParserCreate(0);
   pOrgHdl = dictionary_new(10);
   CPPUNIT_ASSERT(pNewHdl);
   CPPUNIT_ASSERT(pOrgHdl);

   CPPUNIT_ASSERT_EQUAL(pNewHdl->Set(0, 0), !iniparser_set(pOrgHdl, 0, 0));

   CPPUNIT_ASSERT_EQUAL(pNewHdl->Set("section", 0),
         !iniparser_set(pOrgHdl, "section", 0));

   CPPUNIT_ASSERT_EQUAL(pNewHdl->Set("section:key", "value"),
         !iniparser_set(pOrgHdl, "section:key", "value"));

   tmp = pNewHdl->GetString("section:key", 0);
   CPPUNIT_ASSERT_MESSAGE(tmp, tmp == iniparser_getstring(pOrgHdl, "section:key", 0));

   /* reset the key's value*/
   CPPUNIT_ASSERT_EQUAL(pNewHdl->Set("section:key", 0),
         !iniparser_set(pOrgHdl, "section:key", 0));

   tmp = iniparser_getstring(pOrgHdl, "section:key", "dummy") ?
         iniparser_getstring(pOrgHdl, "section:key", "dummy") : "";
   CPPUNIT_ASSERT_MESSAGE(tmp, tmp == pNewHdl->GetString("section:key", "dummy"));
   CPPUNIT_ASSERT_EQUAL(pNewHdl->Set("section:key", "value"),
         !iniparser_set(pOrgHdl, "section:key", "value"));

   tmp = pNewHdl->GetString("section:key", 0);
   CPPUNIT_ASSERT_MESSAGE(tmp, tmp == iniparser_getstring(pOrgHdl, "section:key", 0));

   iniparser_unset(pOrgHdl, "section:key");
   pNewHdl->Reset("section:key");

   tmp = pNewHdl->GetString("section:key",  "dummy");
   CPPUNIT_ASSERT_MESSAGE(tmp, tmp == iniparser_getstring(pOrgHdl, "section:key",  "dummy"));

   CPPUNIT_ASSERT_EQUAL(pNewHdl->Set("section:key", 0), !iniparser_set(pOrgHdl, "section:key", 0));
   CPPUNIT_ASSERT_EQUAL(pNewHdl->Set("section:key1", 0), !iniparser_set(pOrgHdl, "section:key1", 0));
   CPPUNIT_ASSERT_EQUAL(pNewHdl->Set("section:key2", 0), !iniparser_set(pOrgHdl, "section:key2", 0));

}
Пример #26
0
/*
 * main program
 */
int main(int argc, char** argv) {
    //global var
    time_t sec0, sec1;
    unsigned int delayPixel= 120000, delayGyrodataSending = 25000;
    unsigned int idPayload = 303, useKeyboardCmd = 0, buffer_max_num = 64;
    unsigned char keyChar;
    int gpioOK = 0;
	dictionary *iniDict;	//ini dictionery  
	char strPathCfg[255];
	
	GPS_DATA last_posx;
	
	//init last gps pos
	last_posx.gps_status = last_posx.gps_loc.lat = last_posx.gps_loc.lon = 0;
	last_posx.gps_speed = last_posx.gps_altitude = last_posx.gps_time = 0;	
    
    //show titel
#if VERBOSE==1
    printf("%s", APP_TITEL);
#endif

	//init pin switch
	gpioOK = wiringPiSetup();
	
	if(gpioOK != -1)
	{
		pinMode(6, INPUT);
		pullUpDnControl(6, PUD_UP);
	}
	else
		printf("gpio init eror\r\n");
		
	//parsing argumen jika ada
	if(argc>2)
	{
		//default cfg file
		getcwd(strPathCfg, sizeof(strPathCfg));
		strcat(strPathCfg, "/");
		strcat(strPathCfg, CFG_NAME);
	
		int i;
		for(i=1;i<(argc-1);i++)
		{
			//delay pixel
			if(strcmp("-d",argv[i])==0)
				delayPixel = atoi(argv[++i]);
				
			//id payload
			if(strcmp("-i",argv[i])==0)
				idPayload = atoi(argv[++i]);
				
			//pake keyboard ato tidak
			if(strcmp("-k",argv[i])==0)
				useKeyboardCmd = atoi(argv[++i]);
				
			//gyro send
			if(strcmp("-g",argv[i])==0)
				delayGyrodataSending = atoi(argv[++i]);
				
			//buffer size
			if(strcmp("-b",argv[i])==0)
				buffer_max_num = atoi(argv[++i]);
		}
	}
	else //baca dari cfg file
	{				
		//load ini file
		//char strCfg[80];
		
		if(argc==2)
		{
			strcpy(strPathCfg, argv[1]);
		}
		else
		{		
			//get cwd
			if (getcwd(strPathCfg, sizeof(strPathCfg)) != NULL)
			   printf("Current working dir: %s\r\n", strPathCfg);
			else
			{
				strcpy(strPathCfg, argv[1]);			
			}
		}
		
		//check file exists
		strcat(strPathCfg, "/");
		strcat(strPathCfg, CFG_NAME);
		if(!file_exist(strPathCfg))
		{
			printf("Configuration file %s not found!!!\r\n", strPathCfg );
			return(EXIT_FAILURE);
		}
		
		//load from config file
		printf("Loading configuration file from %s\r\n", strPathCfg);
		
		//load ini parser
		iniDict = iniparser_load(strPathCfg);
		
		//read cfg value
		if(iniDict)
		{
			idPayload = iniparser_getint(iniDict,"payload:id",100);
			delayPixel = iniparser_getint(iniDict,"payload:cam_delay",120000);
			delayGyrodataSending = iniparser_getint(iniDict,"payload:g_delay",20000);
			buffer_max_num = iniparser_getint(iniDict,"payload:buffer",64);
		}
	}
	
	//show config setup
	printf("======================================\r\n");
	printf("Configuration :\r\n");
	printf("Delay Pixel = %d uS\r\n", delayPixel);
	printf("ID Payload = %d\r\n", idPayload);
	printf("Use Keyboard = %d\r\n", useKeyboardCmd);
	printf("Gyro Delay = %d uS\r\n", delayGyrodataSending);
	printf("Buffer = %d byte\r\n", buffer_max_num);
	printf("======================================\r\n");    
	
	//init bus i2c
	int i2c_hdl = I2C_Init_Bus(i2c_dev, &statusPeripheral);
	
	//init start adxl345
	ADXL345_Init_Start(i2c_hdl, adxl345_addr, &statusPeripheral);
	
	//init itg3200
	ITG3200_Init_Start(i2c_hdl, itg3200_addr, &statusPeripheral);
	
    //open port
    unsigned char buffRX[COM_BUFF_NUM];
    int buffNumRX;
    
    //make it as null string
    buffRX[COM_BUFF_NUM-1] = 0;

    if (OpenComport(COM_PORT, COM_SPEED)) {
        fprintf(stderr, "Open port %d failed : %s\r\n!!!", COM_PORT, strerror(errno));
        return errno;
    }

#if VERBOSE==1
    printf("communication port opened\r\n");
#endif   

	//tes port com
	//while(1){
		////ambil perintah dari comport
        //buffNumRX = PollComport(COM_PORT, buffRX, (COM_BUFF_NUM - 2));
        //if (buffNumRX > 0) {
			////printf("%d = %s\n", buffNumRX, buffRX);
			//int i;
			//for(i=0;i<buffNumRX;i++)
			//{
				//printf("%X %s", buffRX[i], (i<buffNumRX-1) ? "":"\n");
			//}
			
			//char ss[10];
			//snprintf(ss,6,"%d",(buffRX[1]<<8)+buffRX[2]);
			//printf("id = %s\n", ss);
		//}
	//}

	//open cam
	CvCapture *camHdl = cvCaptureFromCAM(CV_CAP_ANY);
	Cam_Init_Start(camHdl, &statusPeripheral);

    //main loop
    while (1) {
		if(useKeyboardCmd)
		{
			//dummy keyboard input
			printf("press command key\r\n");
			keyChar = getchar();

			//if (keyChar == cmdList[3][0])
				//opsState = STATE_EXIT;

			//if (keyChar == cmdList[2][0])
				//opsState = STATE_GET_CAM_DATA;
				
			//if (keyChar == cmdList[1][0])
				//opsState = STATE_GET_G_DATA;
				
			if (keyChar == cmdList[3])
				opsState = STATE_EXIT;

			if (keyChar == cmdList[2])
				opsState = STATE_GET_CAM_DATA;
				
			if (keyChar == cmdList[1])
				opsState = STATE_GET_G_DATA;
		}

        //ambil perintah dari comport
        buffNumRX = PollComport(COM_PORT, buffRX, (COM_BUFF_NUM - 2));

        //data diterima -> cek printah
        if (buffNumRX > 0) {
            //int i;
            //unsigned char *chrPos;

            ////make it null string
            //if (buffRX[buffNumRX] != 0)
                //buffRX[buffNumRX] = 0;
                
            ////default ops state = idle
            //opsState = STATE_IDLE;

			////add new state
            //for (i = 0; i < CMD_COUNTER; i++) {
                //chrPos = strstr(buffRX,cmdList[i]);

                //if ((chrPos != NULL) && ((chrPos - buffRX + 1) == 1)) { //got a match?
                    ////proses state
                    //opsState = i;

                    //break;
                //}
            //}
            
            opsState = CheckCommand(cmdList, buffNumRX, buffRX);
        }
        
        //cek tobmol
        if(gpioOK != -1)
        {
			//printf("%d\r\n", digitalRead(6));
			//SendByte(COM_PORT, digitalRead(6)+0x30);
			//usleep(1e5);
		if(digitalRead(6) == 0)
		{
			usleep(5e5);
			if(digitalRead(6) == 0)
				opsState = STATE_SHUTDOWN_OS;
		}
	}

        //lakukan proses seuai ops state
        if (opsState == STATE_EXIT) {
#if VERBOSE==1
            printf("exiting...\r\n");
            //fflush(stdout);   
#endif            
            break;
        }
        
        //shutdown os
        if (opsState == STATE_SHUTDOWN_OS)
        {
			printf("shutdown...\r\n");
			break;
		}
		
		//restart os
        if (opsState == STATE_RESTART_OS)
        {
			printf("restart...\r\n");
			break;
		}

		//other state
        switch (opsState) {
            case STATE_IDLE: //idle state
                //SendByte(COM_PORT, 's');
                break;

            case STATE_GET_G_DATA: //ambil data g
                //SendByte(COM_PORT, 'g');
                
                GetAndSendAccGyro(i2c_hdl, adxl345_addr, itg3200_addr, idPayload, delayGyrodataSending, &last_posx, buffer_max_num);
                break;

            case STATE_GET_CAM_DATA://ambil data cam
                //SendByte(COM_PORT, 'c');
#if VERBOSE==1
                printf("grab camera start\r\n");
                sec0 = time(NULL);
#endif                                

                GrabAndSendCameraData(camHdl, i2c_hdl, 5, delayPixel, idPayload, &last_posx, buffer_max_num);                
#if VERBOSE==1
                sec1 = time(NULL);
                printf("grab camera finish in %lds\r\n", (sec1 - sec0));
#endif                
				opsState = STATE_IDLE;
				
				//usleep(5e4);
				//ComportFlush(COM_PORT);
				//usleep(1.5e6);
                break;

            case STATE_GET_STATUS: //ambil status payload
                snprintf((char*)buffRX, 16, "s,%.3d,%d,%d\r", idPayload, 990, statusPeripheral);
                //SendBuf(COM_PORT, buffRX, 10);
                //snprintf((char*)buffRX, 16, "s,%.3d,%d\r", idPayload, 990);
                cprintf(COM_PORT, buffRX);
                opsState = STATE_IDLE; //go back to idle
                
                #if VERBOSE==1
                printf("sendstatus reply = %s\r\n", buffRX);
                #endif
                break;
                
            case STATE_REINIT_PERIPHERAL:	//reinit peripheral
				//init cam
				Cam_Init_Start(camHdl, &statusPeripheral);
				
				//init bus i2c
				i2c_hdl = I2C_Init_Bus(i2c_dev, &statusPeripheral);
				
				//init start adxl345
				ADXL345_Init_Start(i2c_hdl, adxl345_addr, &statusPeripheral);
				
				//init itg3200
				ITG3200_Init_Start(i2c_hdl, itg3200_addr, &statusPeripheral);
				
				opsState = STATE_IDLE; //go back to idle
				break;
				
			case STATE_READ_WRITE_CONFIGURATION: //read/write config file
				printf("Opening configuration file %s\r\n", strPathCfg);
				
				//load ini file
				iniDict = iniparser_load(strPathCfg);
				
				//write if neccessary
				if(buffRX[1]==1)	//write setting
				{
					char stmp[10];
					
					//id
					snprintf(stmp,10,"%d", (buffRX[2]<<8)+buffRX[3]);					
					iniparser_set(iniDict,"payload:id",stmp);
					
					//cam delay in ms
					snprintf(stmp,10,"%d", buffRX[4]*1000);					
					iniparser_set(iniDict,"payload:cam_delay",stmp);
					
					//g delay in ms
					snprintf(stmp,10,"%d", buffRX[5]*1000);					
					iniparser_set(iniDict,"payload:g_delay",stmp);
					
					//buffer max
					snprintf(stmp,10,"%d", buffRX[6]);					
					iniparser_set(iniDict,"payload:buffer",stmp);
					
					Save_INI_File(iniDict, strPathCfg);
				}	
				
				//reload ini file
				iniDict = iniparser_load(strPathCfg);
				
				//read cfg value
				if(iniDict)
				{
					idPayload = iniparser_getint(iniDict,"payload:id",100);
					delayPixel = iniparser_getint(iniDict,"payload:cam_delay",120000);
					delayGyrodataSending = iniparser_getint(iniDict,"payload:g_delay",20000);
					buffer_max_num = iniparser_getint(iniDict,"payload:buffer",64);
				}
				
				//send reply always
				snprintf((char*)buffRX, 25, "f,%d,%d,%d,%d\r", idPayload, delayPixel, delayGyrodataSending,buffer_max_num);
                cprintf(COM_PORT, buffRX);
                opsState = STATE_IDLE; //go back to idle
                
                #if VERBOSE==1
                printf("send config reply = %s\r\n", buffRX);
                #endif				
				break;

            default:
                break;
        }
    }

    //release cam
    cvReleaseCapture(&camHdl);

    //release port
    CloseComport(COM_PORT);
    
    //close bus
	closeBus(i2c_hdl);
	
	//free ini handler
	if(iniDict)
		iniparser_freedict(iniDict);
	
	//cek shutdown atau restart
	switch (opsState) 
	{
		case STATE_SHUTDOWN_OS:
			system("sudo shutdown now");
			break;
			
		case STATE_RESTART_OS:
			system("sudo shutdown -r now");
			break;
			
		default:
			break;
	}

    //return
    return (EXIT_SUCCESS);
}
Пример #27
0
static int ini_set_string(struct config *c, const char *key, const char *val)
{
    dictionary *ini = (dictionary *)c->priv;
    return iniparser_set(ini, key, val);
}