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

📄 config.c

📁 串口调试工具。非常好的串口调试助手
💻 C
📖 第 1 页 / 共 4 页
字号:
gint Load_configuration_from_file(gchar *config_name){  int max, i, j, k, size;  gchar *string = NULL;  gchar *str;  macro_t *macros = NULL;  cfgList *t;  max = cfgParse(config_file, cfg, CFG_INI);  if(max == -1)    return -1;  else    {      for(i = 0; i < max; i++)	{	  if(!strcmp(config_name, cfgSectionNumberToName(i)))	  {	    Hard_default_configuration();	    	    if(port[i] != NULL)	      strcpy(config.port, port[i]);	    if(speed[i] != 0)	      config.vitesse = speed[i];	    if(bits[i] != 0)	      config.bits = bits[i];	    if(stopbits[i] != 0)	      config.stops = stopbits[i];	    if(parity[i] != NULL)	      {		if(!g_ascii_strcasecmp(parity[i], "none"))		  config.parite = 0;		else if(!g_ascii_strcasecmp(parity[i], "odd"))		  config.parite = 1;		else if(!g_ascii_strcasecmp(parity[i], "even"))		  config.parite = 2;	      }	    if(flow[i] != NULL)	      {		if(!g_ascii_strcasecmp(parity[i], "none"))		  config.flux = 0;		else if(!g_ascii_strcasecmp(parity[i], "xon"))		  config.flux = 1;		else if(!g_ascii_strcasecmp(parity[i], "rts"))		  config.flux = 2;	      }	    config.delai = wait_delay[i];	    	    if(wait_char[i] != 0)	      config.car = (signed char)wait_char[i];	    else	      config.car = -1;	    if(echo[i] != -1)	      config.echo = (gboolean)echo[i];	    else	      config.echo = FALSE;	    if(crlfauto[i] != -1)	      config.crlfauto = (gboolean)crlfauto[i];	    else	      config.crlfauto = FALSE;	    g_free(term_conf.font);	    term_conf.font = g_strdup(font[i]);	    t = macro_list[i];	    size = 0;	    if(t != NULL)	      {		size++;		while(t->next != NULL)		  {		    t = t->next;		    size++;		  }	      }	    if(size != 0)	      {	      		t = macro_list[i];		macros = g_malloc(size * sizeof(macro_t));		if(macros == NULL)		  {		    perror("malloc");		    return -1;		  }		for(j = 0; j < size; j++)		  {		    for(k = 0; k < (strlen(t->str) - 1); k++)		      {			if((t->str[k] == ':') && (t->str[k + 1] == ':'))			  break;		      }		    macros[j].shortcut = g_strndup(t->str, k);		    str = &(t->str[k + 2]);		    macros[j].action = g_strdup(str);		    		    t = t->next;		  }	      }	    remove_shortcuts();	    create_shortcuts(macros, size);	    g_free(macros);	    if(transparency[i] != -1)	      term_conf.transparency = (gboolean)transparency[i];	    else	      term_conf.transparency = FALSE;	    if(show_cursor[i] != -1)	      term_conf.show_cursor = (gboolean)show_cursor[i];	    else	      term_conf.show_cursor = FALSE;	    if(rows[i] != 0)	      term_conf.rows = rows[i];	    if(columns[i] != 0)	      term_conf.columns = columns[i];	    if(visual_bell[i] != -1)	      term_conf.visual_bell = (gboolean)visual_bell[i];	    else	      term_conf.visual_bell = FALSE;	    term_conf.foreground_color.red = foreground_red[i];	    term_conf.foreground_color.green = foreground_green[i];	    term_conf.foreground_color.blue = foreground_blue[i];			    term_conf.background_color.red = background_red[i];	    term_conf.background_color.green = background_green[i];	    term_conf.background_color.blue = background_blue[i];	    	    if(background_saturation[i] != 0)	      term_conf.background_saturation = background_saturation[i];	    /* rows and columns are empty when the conf is autogenerate in the 	       first save; so set term to default */	    if(rows[i] == 0 || columns[i] == 0)	      {	   		term_conf.transparency = FALSE;		term_conf.show_cursor = TRUE;		term_conf.rows = 80;		term_conf.columns = 25;		term_conf.visual_bell = FALSE;		term_conf.foreground_color.red = 43253;		term_conf.foreground_color.green = 43253;		term_conf.foreground_color.blue = 43253;				term_conf.background_color.red = 0;		term_conf.background_color.green = 0;		term_conf.background_color.blue = 0;	    		term_conf.background_saturation = 0.5;	      }	    i = max + 1;	  }	}      if(i == max)	{	  string = g_strdup_printf(_("No section \"%s\" in configuration file\n"), config_name);	  show_message(string, MSG_ERR);	  g_free(string);	  return -1;	}    }  vte_terminal_set_font_from_string(VTE_TERMINAL(display), term_conf.font);  vte_terminal_set_background_transparent(VTE_TERMINAL(display), term_conf.transparency);  vte_terminal_set_size (VTE_TERMINAL(display), term_conf.rows, term_conf.columns);  vte_terminal_set_color_foreground (VTE_TERMINAL(display), &term_conf.foreground_color);  vte_terminal_set_color_background (VTE_TERMINAL(display), &term_conf.background_color);  vte_terminal_set_background_saturation(VTE_TERMINAL(display), (gdouble)term_conf.background_saturation);  gtk_widget_queue_draw(display);  return 0;}void Verify_configuration(void){  gchar *string = NULL;  switch(config.vitesse)    {    case 300:    case 600:    case 1200:    case 2400:    case 4800:    case 9600:    case 19200:    case 38400:    case 57600:    case 115200:      break;          default:      string = g_strdup_printf(_("Unknown speed : %d bauds\nFalling back to default speed : %d bauds\n"), config.vitesse, DEFAULT_SPEED);      show_message(string, MSG_ERR);      config.vitesse = DEFAULT_SPEED;      g_free(string);    }  if(config.stops != 1 && config.stops != 2)    {      string = g_strdup_printf(_("Impossible stopbits number : %d\nFalling back to default stop bits number : %d\n"), config.stops, DEFAULT_STOP);      show_message(string, MSG_ERR);      config.stops = DEFAULT_STOP;      g_free(string);    }    if(config.bits < 5 || config.bits > 8)    {      string = g_strdup_printf(_("Impossible bits number : %d\nFalling back to default stop bits : %d\n"), config.bits, DEFAULT_BITS);      show_message(string, MSG_ERR);      config.bits = DEFAULT_BITS;      g_free(string);    }    if(config.delai < 0 || config.delai > 500)    {      string = g_strdup_printf(_("Impossible delay : %d ms\nFalling back to default delay : %d ms\n"), config.delai, DEFAULT_DELAY);      show_message(string, MSG_ERR);      config.delai = DEFAULT_DELAY;      g_free(string);    }  if(term_conf.font == NULL)    term_conf.font = g_strdup_printf(DEFAULT_FONT);}gint Check_configuration_file(void){  struct stat my_stat;  gchar *string = NULL;  /* is configuration file present ? */  if(stat(config_file, &my_stat) == 0)    {      /* If bad configuration file, fallback to _hardcoded_ defaults ! */      if(Load_configuration_from_file("default") == -1)	{	  Hard_default_configuration();	  return -1;	}    }    /* if not, create it, with the [default] section */  else    {      string = g_strdup_printf(_("Configuration file (%s) with\n[default] configuration has been created.\n"), config_file);      show_message(string, MSG_WRN);      cfgAllocForNewSection(cfg, "default");      Hard_default_configuration();      Copy_configuration(0);      cfgDump(config_file, cfg, CFG_INI, 1);      g_free(string);    }  return 0;}void Hard_default_configuration(void){  strcpy(config.port, DEFAULT_PORT);  config.vitesse = DEFAULT_SPEED;  config.parite = DEFAULT_PARITY;  config.bits = DEFAULT_BITS;  config.stops = DEFAULT_STOP;  config.flux = DEFAULT_FLOW;  config.delai = DEFAULT_DELAY;  config.car = DEFAULT_CHAR;  config.echo = DEFAULT_ECHO;  config.crlfauto = FALSE;   term_conf.font = g_strdup_printf(DEFAULT_FONT);  term_conf.transparency = FALSE;  term_conf.show_cursor = TRUE;  term_conf.rows = 80;  term_conf.columns = 25;  term_conf.visual_bell = TRUE;  Selec_couleur(&term_conf.foreground_color, 0.66, 0.66, 0.66);  Selec_couleur(&term_conf.background_color, 0, 0, 0);  term_conf.background_saturation = 0.50;}void Copy_configuration(int pos){  gchar *string = NULL;  macro_t *macros = NULL;  gint size, i;  string = g_strdup(config.port);  cfgStoreValue(cfg, "port", string, CFG_INI, pos);  g_free(string);  string = g_strdup_printf("%d", config.vitesse);  cfgStoreValue(cfg, "speed", string, CFG_INI, pos);  g_free(string);  string = g_strdup_printf("%d", config.bits);  cfgStoreValue(cfg, "bits", string, CFG_INI, pos);  g_free(string);  string = g_strdup_printf("%d", config.stops);  cfgStoreValue(cfg, "stopbits", string, CFG_INI, pos);  g_free(string);  switch(config.parite)    {    case 0:      string = g_strdup_printf("none");      break;    case 1:      string = g_strdup_printf("odd");      break;    case 2:      string = g_strdup_printf("even");      break;    default:      string = g_strdup_printf("none");    }  cfgStoreValue(cfg, "parity", string, CFG_INI, pos);  g_free(string);  switch(config.flux)    {    case 0:      string = g_strdup_printf("none");      break;    case 1:      string = g_strdup_printf("xon");      break;    case 2:      string = g_strdup_printf("rts");      break;    default:      string = g_strdup_printf("none");    }  cfgStoreValue(cfg, "flow", string, CFG_INI, pos);  g_free(string);  string = g_strdup_printf("%d", config.delai);  cfgStoreValue(cfg, "wait_delay", string, CFG_INI, pos);  g_free(string);  string = g_strdup_printf("%d", config.car);  cfgStoreValue(cfg, "wait_char", string, CFG_INI, pos);  g_free(string);    if(config.echo == FALSE)    string = g_strdup_printf("False");  else    string = g_strdup_printf("True");  cfgStoreValue(cfg, "echo", string, CFG_INI, pos);  g_free(string);  if(config.crlfauto == FALSE)    string = g_strdup_printf("False");

⌨️ 快捷键说明

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