home · contact · privacy
Improved and defined more precisely textfile_sizes().
[plomrogue] / src / command_db.c
index cbf6959e87322ff24a9a2d7e392160482450332f..bdcdcd1fa83a49faa343231360f4f72add0b4bcf 100644 (file)
@@ -25,6 +25,41 @@ static void copy_tokenized_string(struct World * world,
 
 
 
+extern uint8_t is_command_id_shortdsc(struct World * world,
+                                      uint8_t id, char * shortdsc)
+{
+    struct Command * cmd_ptr = world->cmd_db->cmds;
+    while (1)
+    {
+        if (id == cmd_ptr->id)
+        {
+            if (strcmp(shortdsc, cmd_ptr->dsc_short))
+            {
+                return 0;
+            }
+            return 1;
+        }
+        cmd_ptr = &cmd_ptr[1];
+    }
+}
+
+
+
+extern uint8_t get_command_id(struct World * world, char * dsc_short)
+{
+    struct Command * cmd_ptr = world->cmd_db->cmds;
+    while (1)
+    {
+        if (0 == strcmp(dsc_short, cmd_ptr->dsc_short))
+        {
+            return cmd_ptr->id;
+        }
+        cmd_ptr = &cmd_ptr[1];
+    }
+}
+
+
+
 extern char * get_command_longdsc(struct World * world, char * dsc_short)
 {
     struct Command * cmd_ptr = world->cmd_db->cmds;
@@ -42,29 +77,33 @@ extern char * get_command_longdsc(struct World * world, char * dsc_short)
 
 extern void init_command_db(struct World * world)
 {
-    char * err = "Trouble in init_cmds() with fopen() on file 'commands'.";
-    FILE * file = fopen("config/commands", "r");
-    exit_err(NULL == file, world, err);
+    char * err_o = "Trouble in init_cmds() with fopen() on file 'commands'.";
+    char * err_s = "Trouble in init_cmds() with textfile_sizes().";
+    char * err_m = "Trouble in init_cmds() with malloc().";
+    char * err_c = "Trouble in init_cmds() with fclose() on file 'commands'.";
+
+    char * path = "config/commands";
+    FILE * file = fopen(path, "r");
+    exit_err(NULL == file, world, err_o);
     uint16_t lines, linemax;
-    textfile_sizes(file, &linemax, &lines);
-    err = "Trouble in init_cmds() with malloc().";
-    char * line = malloc(linemax);
-    exit_err(NULL == line, world, err);
+    exit_err(textfile_sizes(file, &linemax, &lines), world, err_s);
+    char line[linemax + 1];
+
     struct Command * cmds = malloc(lines * sizeof(struct Command));
-    exit_err(NULL == line, world, err);
+    exit_err(NULL == line, world, err_m);
     uint8_t i = 0;
-    while (fgets(line, linemax, file))
+    while (fgets(line, linemax + 1, file))
     {
         cmds[i].id = atoi(strtok(line, " "));
-        copy_tokenized_string(world, &cmds[i].dsc_short, " ", err);
-        copy_tokenized_string(world, &cmds[i].dsc_long, "\n", err);
+        copy_tokenized_string(world, &cmds[i].dsc_short, " ", err_m);
+        copy_tokenized_string(world, &cmds[i].dsc_long, "\n", err_m);
         i++;
     }
+    exit_err(fclose(file), world, err_c);
+
     world->cmd_db = malloc(sizeof(struct CommandDB));
     world->cmd_db->cmds = cmds;
     world->cmd_db->n = lines;
-    err = "Trouble in init_cmds() with fclose() on file 'commands'.";
-    exit_err(fclose(file), world, err);
 }