Skip to content

Commit 61e6c3d

Browse files
Merge pull request #10 from dreamer-coding/cli_parser
Command-line parser
2 parents b880673 + 18b840e commit 61e6c3d

File tree

8 files changed

+789
-3
lines changed

8 files changed

+789
-3
lines changed

code/logic/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ file(GLOB HEADER_FILES ${CMAKE_CURRENT_SOURCE_DIR}/fossil/*.h)
88

99
# List the source files
1010
set(TEST_CODE
11+
parser.c
1112
error.c
1213
input.c
1314
output.c
@@ -19,6 +20,9 @@ set(TEST_CODE
1920
# Create the library target
2021
add_library(fossil-io STATIC ${TEST_CODE} ${HEADER_FILES})
2122

23+
# Link the math library
24+
target_link_libraries(fossil-io PUBLIC m)
25+
2226
# Set the library to be installed
2327
install(TARGETS fossil-io
2428
ARCHIVE DESTINATION lib

code/logic/fossil/io/framework.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "error.h"
2222
#include "stream.h"
2323
#include "soap.h"
24+
#include "parser.h"
2425

2526
enum {
2627
FOSSIL_IO_SUCCESS = 0,

code/logic/fossil/io/parser.h

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
/*
2+
* -----------------------------------------------------------------------------
3+
* Project: Fossil Logic
4+
*
5+
* This file is part of the Fossil Logic project, which aims to develop high-
6+
* performance, cross-platform applications and libraries. The code contained
7+
* herein is subject to the terms and conditions defined in the project license.
8+
*
9+
* Author: Michael Gene Brockus (Dreamer)
10+
*
11+
* Copyright (C) 2024 Fossil Logic. All rights reserved.
12+
* -----------------------------------------------------------------------------
13+
*/
14+
#ifndef FOSSIL_IO_PARSER_H
15+
#define FOSSIL_IO_PARSER_H
16+
17+
#include <stdint.h>
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
// Types of command argument
24+
typedef enum {
25+
FOSSIL_IO_PARSER_BOOL, // Boolean (enable/disable)
26+
FOSSIL_IO_PARSER_STRING, // String argument
27+
FOSSIL_IO_PARSER_INT, // Integer argument
28+
FOSSIL_IO_PARSER_COMBO // Combo of predefined values
29+
} fossil_io_parser_arg_type_t;
30+
31+
// Structure to represent each argument in the command
32+
typedef struct fossil_io_parser_argument_s {
33+
char *name; // Argument name
34+
fossil_io_parser_arg_type_t type; // Argument type
35+
char *value; // Parsed value
36+
char **combo_options; // Valid options for COMBO type
37+
int combo_count; // Number of valid options
38+
struct fossil_io_parser_argument_s *next; // Next argument in the list
39+
} fossil_io_parser_argument_t;
40+
41+
// Structure for a command
42+
typedef struct fossil_io_parser_command_s {
43+
char *name; // Command name
44+
char *description; // Command description
45+
fossil_io_parser_argument_t *arguments; // List of arguments
46+
struct fossil_io_parser_command_s *prev; // Previous command in the list
47+
struct fossil_io_parser_command_s *next; // Next command in the list
48+
} fossil_io_parser_command_t;
49+
50+
// Structure for the command palette
51+
typedef struct fossil_io_parser_palette_s {
52+
char *name; // Palette name
53+
char *description; // Palette description
54+
fossil_io_parser_command_t *commands; // List of commands
55+
} fossil_io_parser_palette_t;
56+
57+
// ==================================================================
58+
// Functions
59+
// ==================================================================
60+
61+
/**
62+
* @brief Creates a new parser palette.
63+
*
64+
* @param name The name of the palette.
65+
* @param description A description of the palette.
66+
* @return A pointer to the newly created parser palette.
67+
*/
68+
fossil_io_parser_palette_t *fossil_io_parser_create_palette(const char *name, const char *description);
69+
70+
/**
71+
* @brief Adds a command to the parser palette.
72+
*
73+
* @param palette The parser palette to which the command will be added.
74+
* @param command_name The name of the command.
75+
* @param description A description of the command.
76+
* @return A pointer to the newly added command.
77+
*/
78+
fossil_io_parser_command_t *fossil_io_parser_add_command(fossil_io_parser_palette_t *palette, const char *command_name, const char *description);
79+
80+
/**
81+
* @brief Adds an argument to a command.
82+
*
83+
* @param command The command to which the argument will be added.
84+
* @param arg_name The name of the argument.
85+
* @param arg_type The type of the argument.
86+
* @param combo_options (Optional) Array of valid options for COMBO type.
87+
* @param combo_count (Optional) Number of options for COMBO type.
88+
* @return A pointer to the newly added argument.
89+
*/
90+
fossil_io_parser_argument_t *fossil_io_parser_add_argument(fossil_io_parser_command_t *command, const char *arg_name, fossil_io_parser_arg_type_t arg_type, char **combo_options, int combo_count);
91+
92+
/**
93+
* @brief Parses the command-line arguments using the parser palette.
94+
*
95+
* @param palette The parser palette to use for parsing.
96+
* @param argc The number of command-line arguments.
97+
* @param argv The command-line arguments.
98+
*/
99+
void fossil_io_parser_parse(fossil_io_parser_palette_t *palette, int argc, char **argv);
100+
101+
/**
102+
* @brief Frees the memory allocated for the parser palette.
103+
*
104+
* @param palette The parser palette to be freed.
105+
*/
106+
void fossil_io_parser_free(fossil_io_parser_palette_t *palette);
107+
108+
#ifdef __cplusplus
109+
}
110+
111+
/**
112+
* C++ wrapper for the Parser API.
113+
*/
114+
namespace fossil {
115+
116+
/**
117+
* Namespace for I/O operations.
118+
*/
119+
namespace io {
120+
/**
121+
* Parser API for sanitizing strings.
122+
*/
123+
class Parser {
124+
public:
125+
/**
126+
* Creates a new parser palette.
127+
*
128+
* @param name The name of the palette.
129+
* @param description A description of the palette.
130+
* @return A pointer to the newly created parser palette.
131+
*/
132+
static fossil_io_parser_palette_t *create_palette(const char *name, const char *description) {
133+
return fossil_io_parser_create_palette(name, description);
134+
}
135+
136+
/**
137+
* Adds a command to the parser palette.
138+
*
139+
* @param palette The parser palette to which the command will be added.
140+
* @param command_name The name of the command.
141+
* @param description A description of the command.
142+
* @return A pointer to the newly added command.
143+
*/
144+
static fossil_io_parser_command_t *add_command(fossil_io_parser_palette_t *palette, const char *command_name, const char *description) {
145+
return fossil_io_parser_add_command(palette, command_name, description);
146+
}
147+
148+
/**
149+
* Adds an argument to a command.
150+
*
151+
* @param command The command to which the argument will be added.
152+
* @param arg_name The name of the argument.
153+
* @param arg_type The type of the argument.
154+
* @param combo_options (Optional) Array of valid options for COMBO type.
155+
* @param combo_count (Optional) Number of options for COMBO type.
156+
* @return A pointer to the newly added argument.
157+
*/
158+
static fossil_io_parser_argument_t *add_argument(fossil_io_parser_command_t *command, const char *arg_name, fossil_io_parser_arg_type_t arg_type, char **combo_options, int combo_count) {
159+
return fossil_io_parser_add_argument(command, arg_name, arg_type, combo_options, combo_count);
160+
}
161+
162+
/**
163+
* Parses the command-line arguments using the parser palette.
164+
*
165+
* @param palette The parser palette to use for parsing.
166+
* @param argc The number of command-line arguments.
167+
* @param argv The command-line arguments.
168+
*/
169+
static void parse(fossil_io_parser_palette_t *palette, int argc, char **argv) {
170+
fossil_io_parser_parse(palette, argc, argv);
171+
}
172+
173+
/**
174+
* Frees the memory allocated for the parser palette.
175+
*
176+
* @param palette The parser palette to be freed.
177+
*/
178+
static void free(fossil_io_parser_palette_t *palette) {
179+
fossil_io_parser_free(palette);
180+
}
181+
182+
};
183+
}
184+
}
185+
186+
#endif
187+
188+
#endif /* FOSSIL_IO_FRAMEWORK_H */

code/logic/meson.build

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
dir = include_directories('.')
2-
2+
cc = meson.get_compiler('c')
33

44
fossil_io_lib = library('fossil-io',
5-
files('input.c', 'output.c', 'error.c', 'soap.c', 'stream.c', 'keyboard.c'),
5+
files('parser.c', 'input.c', 'output.c', 'error.c', 'soap.c', 'stream.c', 'keyboard.c'),
66
install: true,
7+
dependencies: cc.find_library('m', required : false),
78
include_directories: dir)
89

910
fossil_io_dep = declare_dependency(

0 commit comments

Comments
 (0)