📄 config.cpp
字号:
/*
Robot Interface
(C) 2006 Jason Hunt
nulluser@gmail.com
File: config.cpp
*/
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "network.h"
#include "types.h"
#include "display.h"
#include "serial.h"
#include "camera.h"
#define LINE_MAX 1024
/* gets the nextr string in a line, up to a space or endline */
void get_next_string(char *line, unsigned int &i, char *str)
{
int done_str = 0; // flag for done with string
unsigned int s = 0; // index into target string
// eat white space at front of line
while ((line[i] == ' ' || line[i] == '\t') && i < LINE_MAX) i++;
while (!done_str && i < LINE_MAX)
{
char ch = line[i++];
if (ch == ' ' || ch == '\t' || ch == 0)
done_str = 1;
else
str[s++] = ch;
}
str[s] = 0; // null terminate
}
/* end of get_next_string */
/* parse a config line for commands */
void parse_line(char *line)
{
unsigned int index = 0;
char str[LINE_MAX];
get_next_string(line, index, str);
if (!strcmp("serial_port", str)) // set hostname
{
// read hostname from line
get_next_string(line, index, str);
if (strlen(str))
strcpy(serial_port, str);
}
if (!strcmp("camera_number", str)) // set hostname
{
// read hostname from line
get_next_string(line, index, str);
if (strlen(str))
camera.set_camera_number(atoi(str));
}
}
/* end of parse_line */
/* loads configuration values from config file */
void load_config(char *config_filename)
{
FILE *f = fopen(config_filename, "rt");
if (!f)
{
add_line("load_config: Unable to load config file");
return;
}
// load each line
while (!feof(f))
{
char line[LINE_MAX]; // temperary line
unsigned int i = 0; // index
int line_done = 0; // line end
// read line
while (i < LINE_MAX && !line_done)
{
char ch = fgetc(f); // get char from file
// eat rest of line
if (ch == '#')
{
while (ch != 0x0a && ch != 0x0d && !feof(f)) ch = fgetc(f);
} else
// limit check
if (ch == 0x0d || ch == 0x0a || feof(f)) line_done = 1; else
line[i++] = ch;
}
line[i] = 0; // null terminate
// check for blank line
if (i > 0)
parse_line(line);
}
fclose(f);
}
/* end of load config */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -