From bcff48379714dc2a36db5ca54246a511f5102be2 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 20 May 2022 11:18:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E6=88=90=E4=BA=86=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=AF=BB=E5=8F=96=E5=87=BD=E6=95=B0=20?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=20=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=AF=BB=E5=8F=96=E5=87=BD=E6=95=B0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/bus_io.h | 1 + include/controller.h | 6 ++- include/define.h | 3 ++ src/bus_io.c | 110 ++++++++++++++++++++++++++++++++++++++++++- src/controller.c | 5 +- test/CMakeLists.txt | 6 +++ test/dict.dic | 6 +++ test/io_test.cpp | 28 +++++++++++ 8 files changed, 159 insertions(+), 6 deletions(-) create mode 100644 test/dict.dic diff --git a/include/bus_io.h b/include/bus_io.h index 1cf142b..38d5678 100644 --- a/include/bus_io.h +++ b/include/bus_io.h @@ -7,6 +7,7 @@ #include "rail.h" #include "query.h" #include "define.h" +#include "controller.h" #include "string.h" #include "stdio.h" diff --git a/include/controller.h b/include/controller.h index 2e9a0d4..480b667 100644 --- a/include/controller.h +++ b/include/controller.h @@ -14,6 +14,10 @@ * 当前正在处理的请求 */ extern bus_query_t *target_query; +/** + * 当前选择的策略 + */ +extern int chosen_strategy; /** * 在先来先服务策略下应该前进的方向 @@ -25,5 +29,5 @@ int FCFSDirection(); * 在先来先服务策略下给出处理的请求 * @return 需要处理的请求 */ -bus_query_t FCFSQuery(); +bus_query_t *FCFSQuery(); #endif //AUTO_PILOT_BUS_CONTROLLER_H diff --git a/include/define.h b/include/define.h index 00a1e61..80fe0ad 100644 --- a/include/define.h +++ b/include/define.h @@ -14,5 +14,8 @@ #define IO_CLOCK 0 // 读取时钟指令 #define IO_READING 1 // 读取请求指令 #define IO_END 2 // 读取结束指令 +#define BUS_FCFS 0 // 先来先服务 +#define BUS_SSTF 1 // 最短寻找时间优先 +#define BUS_SCAN 2 // 顺便服务 #endif //AUTO_PILOT_BUS_DEFINE_H diff --git a/src/bus_io.c b/src/bus_io.c index 3d623a8..b2cadd0 100644 --- a/src/bus_io.c +++ b/src/bus_io.c @@ -8,7 +8,7 @@ int ReadInput(char* inputString) char src[20]; int num; - sscanf(inputString,"%[a-z] %d",src,&num); + sscanf_s(inputString,"%[a-z] %d",src,&num); if (0 == strcmp("clock",src)) { return IO_CLOCK; @@ -37,5 +37,111 @@ int ReadInput(char* inputString) // 匹配失败则返回-1 return -1; } - +} + +rail_node_t *ReadConfigFile() +{ + FILE *config_file = NULL; + char buffer[30]; + int total_station = 0; + int distance = 0; + + config_file = fopen("dict.dic", "r"); + + // 循环读取文件的每一行 + while(fgets(buffer, sizeof buffer, config_file) != NULL) + { + char first_char = buffer[0]; + char *p; + + switch (first_char) + { + case '#': + // 如果读取到#什么都不做 + break; + case 'T': + // TOTAL_STATION + p = buffer; + + // 把数字前面的所有字符全部干掉 + while (*p < '0' || *p > '9') + { + p++; + } + + if(*p == '1' && *(p + 1) != '\n') + { + total_station = 10; + } + else if(*(p + 1) == '\n') + { + total_station = *p - 48; + } + + break; + case 'S': + // STRATEGY + p = buffer; + // 将=前的字符全部略去 + while(*p != '=') + { + p++; + } + // =也去掉 + p++; + // =和策略之间的空格也去掉 + while(*p == ' ') + { + p++; + } + + if(*p == 'F' && *(p + 1) == 'C') //FCFS + { + chosen_strategy = BUS_FCFS; + } + else if(*p == 'S' && *(p + 1) == 'S') //SSTF + { + chosen_strategy = BUS_SSTF; + } + else if(*p == 'S' && *(p + 1) == 'C') //SCAN + { + chosen_strategy = BUS_SCAN; + } + else + { + // 读取失败 + chosen_strategy = -1; + } + + break; + case 'D': + // DISTANCE + p = buffer; + + // 把数字前面的所有字符全部干掉 + while (*p < '0' || *p > '9') + { + p++; + } + + if(*(p + 1) == '\n') + { + distance = *p - 48; + } + + break; + default: + continue; + } + + } + + if(distance != 0 && total_station != 0) + { + return CreateRails(distance, total_station); + } + else + { + return NULL; + } } diff --git a/src/controller.c b/src/controller.c index 869e200..6121a52 100644 --- a/src/controller.c +++ b/src/controller.c @@ -3,6 +3,5 @@ // #include "controller.h" -int BUS_TIME = 0; - -bus_query_t *target_query = NULL; \ No newline at end of file +bus_query_t *target_query = NULL; +int chosen_strategy = -1; \ No newline at end of file diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a142fe9..5c87630 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -22,3 +22,9 @@ target_link_libraries(bus_test gmock_main pthread ) + +add_custom_command(TARGET bus_test POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${CMAKE_CURRENT_SOURCE_DIR}/dict.dic" + $ + ) \ No newline at end of file diff --git a/test/dict.dic b/test/dict.dic new file mode 100644 index 0000000..b66f92a --- /dev/null +++ b/test/dict.dic @@ -0,0 +1,6 @@ +# The Config file of bus +# Comment +TOTAL_STATION = 10 +DISTANCE = 10 +# STRATEGY = SCAN +STRATEGY = FCFS diff --git a/test/io_test.cpp b/test/io_test.cpp index 413cc25..9beb68d 100644 --- a/test/io_test.cpp +++ b/test/io_test.cpp @@ -10,6 +10,7 @@ extern "C" #endif #include "bus_io.h" #include "define.h" +#include "controller.h" #ifdef __cplusplus } #endif @@ -36,4 +37,31 @@ TEST(bus_io, ReadInput_reading) char str[20] = "target 8\n"; int result = ReadInput(str); EXPECT_EQ(result, IO_READING); +} + +TEST(bus_file, all) +{ + rail_node_t *result = ReadConfigFile(); + // 先测试轨道的参数读取是否正确 + rail_node_t *p = result; + + ASSERT_TRUE(p != nullptr); + for(int i = 1; i <= 10; i++) + { + EXPECT_EQ(p->id, i); + EXPECT_EQ(p->next_node_distance, 5); + EXPECT_EQ(p->last_node_distance, 5); + p = p->next_node; + } + + p = result->last_node; + for(int i = 10; i >= 1; i--) + { + EXPECT_EQ(p->id, i); + EXPECT_EQ(p->next_node_distance, 5); + EXPECT_EQ(p->last_node_distance, 5); + p = p->last_node; + } + + EXPECT_EQ(chosen_strategy, BUS_FCFS); } \ No newline at end of file