diff options
| author | Filip Wandzio <contact@philw.dev> | 2026-03-01 01:03:39 +0100 |
|---|---|---|
| committer | Filip Wandzio <contact@philw.dev> | 2026-03-01 01:03:39 +0100 |
| commit | bf0d77d7d448e964e9716d5af67c48f3d014f090 (patch) | |
| tree | e55f1e91a8c20cd737dfb01dc12a954c25711e01 /src/syntax_essentials.c | |
| download | embedded_guardian-bf0d77d7d448e964e9716d5af67c48f3d014f090.tar.gz embedded_guardian-bf0d77d7d448e964e9716d5af67c48f3d014f090.zip | |
Scaffold basic project tree, implement benchmarking logic
Implement unit testing guardian
Diffstat (limited to '')
| -rw-r--r-- | src/syntax_essentials.c | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/src/syntax_essentials.c b/src/syntax_essentials.c new file mode 100644 index 0000000..419996a --- /dev/null +++ b/src/syntax_essentials.c | |||
| @@ -0,0 +1,176 @@ | |||
| 1 | #include "syntax_essentials.h" | ||
| 2 | #include <stdio.h> | ||
| 3 | |||
| 4 | /** | ||
| 5 | * | ||
| 6 | * TASK 1: validate_first_name | ||
| 7 | * Check if the first name is valid. | ||
| 8 | * - Cannot be NULL or empty | ||
| 9 | * - Only letters (a-z, A-Z) | ||
| 10 | * - Minimum length: 2 | ||
| 11 | * | ||
| 12 | * @param first_name | ||
| 13 | * @return | ||
| 14 | */ | ||
| 15 | int validate_first_name(const char* first_name) | ||
| 16 | { | ||
| 17 | if (!first_name) | ||
| 18 | return 0; | ||
| 19 | |||
| 20 | int length = 0; | ||
| 21 | for (const char* p = first_name; *p; p++) { | ||
| 22 | if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z')) | ||
| 23 | length++; | ||
| 24 | else | ||
| 25 | return 0; | ||
| 26 | } | ||
| 27 | return length >= 2; | ||
| 28 | } | ||
| 29 | |||
| 30 | /** | ||
| 31 | * | ||
| 32 | * TASK 2: validate_last_name | ||
| 33 | * Check if the last name is valid. | ||
| 34 | * - Cannot be NULL or empty | ||
| 35 | * - Only letters (a-z, A-Z) | ||
| 36 | * - Minimum length: 2 | ||
| 37 | * | ||
| 38 | * @param last_name | ||
| 39 | * @return | ||
| 40 | */ | ||
| 41 | int validate_last_name(const char* last_name) | ||
| 42 | { | ||
| 43 | if (!last_name) | ||
| 44 | return 0; | ||
| 45 | |||
| 46 | int length = 0; | ||
| 47 | |||
| 48 | for (const char* p = last_name; *p; p++) { | ||
| 49 | if ((*p >= 'A' && *p <= 'Z') || (*p >= 'a' && *p <= 'z')) | ||
| 50 | length++; | ||
| 51 | else | ||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | return length >= 2; | ||
| 56 | } | ||
| 57 | |||
| 58 | /** | ||
| 59 | * | ||
| 60 | * TASK 3: validate_full_name | ||
| 61 | * Check if a full name "First Last" is valid. | ||
| 62 | * Use TASK 1 and TASK 2 internally. | ||
| 63 | * @param full_name | ||
| 64 | * @return | ||
| 65 | */ | ||
| 66 | int validate_full_name(const char* full_name) | ||
| 67 | { | ||
| 68 | if (!full_name) | ||
| 69 | return 0; | ||
| 70 | |||
| 71 | char first_name[64], last_name[64]; | ||
| 72 | if (sscanf(full_name, "%63s %63s", first_name, last_name) != 2) | ||
| 73 | return 0; | ||
| 74 | |||
| 75 | return validate_first_name(first_name) && validate_last_name(last_name); | ||
| 76 | } | ||
| 77 | |||
| 78 | /** | ||
| 79 | * | ||
| 80 | * @param index | ||
| 81 | * @return | ||
| 82 | */ | ||
| 83 | int validate_index(const char* index) | ||
| 84 | { | ||
| 85 | if (!index) | ||
| 86 | return 0; | ||
| 87 | |||
| 88 | int length = 0; | ||
| 89 | for (const char* p = index; *p; p++) { | ||
| 90 | if (*p >= '0' && *p <= '9') | ||
| 91 | length++; | ||
| 92 | else | ||
| 93 | return 0; | ||
| 94 | } | ||
| 95 | |||
| 96 | return length == 6; | ||
| 97 | } | ||
| 98 | |||
| 99 | /** | ||
| 100 | * | ||
| 101 | * @param full_name | ||
| 102 | * @param index | ||
| 103 | * @return | ||
| 104 | */ | ||
| 105 | int register_student(const char* full_name, const char* index) | ||
| 106 | { | ||
| 107 | if (!validate_full_name(full_name)) | ||
| 108 | return 1; | ||
| 109 | if (!validate_index(index)) | ||
| 110 | return 2; | ||
| 111 | return 0; | ||
| 112 | } | ||
| 113 | |||
| 114 | /** | ||
| 115 | * | ||
| 116 | * @param name | ||
| 117 | * @return | ||
| 118 | */ | ||
| 119 | int name_length(const char* name) | ||
| 120 | { | ||
| 121 | if (!name) | ||
| 122 | return 0; | ||
| 123 | |||
| 124 | int length = 0; | ||
| 125 | while (*name++) | ||
| 126 | length++; | ||
| 127 | return length; | ||
| 128 | } | ||
| 129 | |||
| 130 | /** | ||
| 131 | * | ||
| 132 | * @param array | ||
| 133 | * @param max_size | ||
| 134 | * @param name | ||
| 135 | * @param index | ||
| 136 | * @return | ||
| 137 | */ | ||
| 138 | int add_student(Student* array, | ||
| 139 | const int max_size, | ||
| 140 | const char* name, | ||
| 141 | const char* index) | ||
| 142 | { | ||
| 143 | if (!name || !index || !array) | ||
| 144 | return 2; | ||
| 145 | |||
| 146 | int len = 0; | ||
| 147 | while (len < max_size && array[len].full_name[0] != '\0') | ||
| 148 | len++; | ||
| 149 | |||
| 150 | if (len >= max_size) | ||
| 151 | return 1; | ||
| 152 | |||
| 153 | snprintf( | ||
| 154 | array[len].full_name, sizeof(array[len].full_name), "%s", name); | ||
| 155 | snprintf(array[len].index, sizeof(array[len].index), "%s", index); | ||
| 156 | return 0; | ||
| 157 | } | ||
| 158 | |||
| 159 | /** | ||
| 160 | * | ||
| 161 | * @param code | ||
| 162 | * @return | ||
| 163 | */ | ||
| 164 | const char* get_error_message(const int code) | ||
| 165 | { | ||
| 166 | switch (code) { | ||
| 167 | case 0: | ||
| 168 | return "Success"; | ||
| 169 | case 1: | ||
| 170 | return "Invalid name"; | ||
| 171 | case 2: | ||
| 172 | return "Invalid index"; | ||
| 173 | default: | ||
| 174 | return "Unknown error"; | ||
| 175 | } | ||
| 176 | } | ||
