xyntax/xyntax.c

169 lines
5.3 KiB
C
Raw Normal View History

2023-08-25 17:00:12 -04:00
/*
2023-10-06 13:41:24 -04:00
Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
Xyntax is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
And when you do redistribute it or modify it, it will use either version 3 of the License, or (at yours truly opinion) any later version.
It is distributed in the hope that it will be useful or harmful, it really depends... But no warranty what so ever, seriously. See GNU/GPLv3.
*/
2023-08-25 17:00:12 -04:00
#ifndef XYNTAX_SOURCE
#define XYNTAX_SOURCE
#include <xolatile/xtandard.c>
2023-08-25 17:00:12 -04:00
#include <xolatile/xyntax.h>
2023-11-18 17:33:41 -05:00
static int syntax_count = 0;
static int syntax_active = FALSE;
static int * syntax_enrange = NULL;
static int * syntax_derange = NULL;
static char * * syntax_begin = NULL;
static char * * syntax_end = NULL;
static char * syntax_escape = NULL;
static int * syntax_colour = NULL;
static int * syntax_effect = NULL;
static void syntax_delete (void) {
int offset;
if (syntax_active == FALSE) {
return;
}
for (offset = 0; offset < syntax_count; ++offset) {
syntax_begin [offset] = deallocate (syntax_begin [offset]);
syntax_end [offset] = deallocate (syntax_end [offset]);
}
syntax_enrange = deallocate (syntax_enrange);
syntax_derange = deallocate (syntax_derange);
syntax_begin = deallocate (syntax_begin);
syntax_end = deallocate (syntax_end);
syntax_escape = deallocate (syntax_escape);
syntax_colour = deallocate (syntax_colour);
syntax_effect = deallocate (syntax_effect);
syntax_active = FALSE;
syntax_count = 0;
}
2023-08-25 17:00:12 -04:00
2023-10-31 09:40:22 -04:00
int syntax_define (int enrange, int derange, char * begin, char * end, char escape, int colour, int effect) {
2023-11-18 17:33:41 -05:00
if (syntax_active == FALSE) {
syntax_active = TRUE;
atexit (syntax_delete);
}
2023-09-12 05:03:51 -04:00
fatal_failure (begin == NULL, "syntax_define: Begin string is null pointer.");
fatal_failure (end == NULL, "syntax_define: End string is null pointer.");
2023-08-25 17:00:12 -04:00
2023-09-12 05:03:51 -04:00
++syntax_count;
2023-08-25 17:00:12 -04:00
2023-09-12 05:03:51 -04:00
syntax_enrange = reallocate (syntax_enrange, syntax_count * (int) sizeof (* syntax_enrange));
syntax_derange = reallocate (syntax_derange, syntax_count * (int) sizeof (* syntax_derange));
syntax_begin = reallocate (syntax_begin, syntax_count * (int) sizeof (* syntax_begin));
syntax_end = reallocate (syntax_end, syntax_count * (int) sizeof (* syntax_end));
syntax_escape = reallocate (syntax_escape, syntax_count * (int) sizeof (* syntax_escape));
syntax_colour = reallocate (syntax_colour, syntax_count * (int) sizeof (* syntax_colour));
syntax_effect = reallocate (syntax_effect, syntax_count * (int) sizeof (* syntax_effect));
2023-08-25 17:00:12 -04:00
2023-09-12 05:03:51 -04:00
syntax_begin [syntax_count - 1] = allocate ((string_length (begin) + 1) * (int) sizeof (* * syntax_begin));
syntax_end [syntax_count - 1] = allocate ((string_length (end) + 1) * (int) sizeof (* * syntax_end));
2023-08-25 17:00:12 -04:00
2023-09-12 05:03:51 -04:00
syntax_enrange [syntax_count - 1] = enrange;
syntax_derange [syntax_count - 1] = derange;
syntax_escape [syntax_count - 1] = escape;
syntax_colour [syntax_count - 1] = colour;
syntax_effect [syntax_count - 1] = effect;
2023-08-28 09:03:06 -04:00
2023-09-12 05:03:51 -04:00
string_copy (syntax_begin [syntax_count - 1], begin);
string_copy (syntax_end [syntax_count - 1], end);
return (syntax_count - 1);
2023-08-25 17:00:12 -04:00
}
2023-09-12 05:03:51 -04:00
int syntax_select (char * string, int * length) {
2023-11-18 17:33:41 -05:00
int offset, subset, select;
fatal_failure (syntax_active == FALSE, "syntax_select: Syntax is not active.");
fatal_failure (string == NULL, "syntax_select: String is null.");
fatal_failure (length == NULL, "syntax_select: Length is null.");
for (select = offset = 0; select != syntax_count; ++select) {
if (syntax_enrange [select] == FALSE) {
if (string_compare_limit (string, syntax_begin [select], string_length (syntax_begin [select])) == TRUE) {
break;
2023-08-25 17:00:12 -04:00
}
} else {
2023-11-18 17:33:41 -05:00
for (subset = 0; subset != string_length (syntax_begin [select]); ++subset) {
2023-08-25 17:00:12 -04:00
if (string [offset] == syntax_begin [select] [subset]) {
goto selected;
}
2023-11-18 17:33:41 -05:00
}
2023-08-25 17:00:12 -04:00
}
2023-11-18 17:33:41 -05:00
}
2023-08-25 17:00:12 -04:00
selected:
2023-11-18 17:33:41 -05:00
if (select >= syntax_count) {
2023-09-12 09:58:36 -04:00
* length = 1;
2023-11-18 17:33:41 -05:00
return (syntax_count);
2023-09-12 09:58:36 -04:00
}
2023-11-18 17:33:41 -05:00
for (offset = 1; string [offset - 1] != '\0'; ++offset) {
if (string [offset] == syntax_escape [select]) {
++offset;
continue;
}
if (syntax_derange [select] == FALSE) {
if (string_compare_limit (& string [offset], syntax_end [select], string_length (syntax_end [select])) == TRUE) {
* length = offset + string_length (syntax_end [select]);
return (select);
}
} else {
subset = 0;
if (string_compare (syntax_end [select], "") == FALSE) {
break;
} do {
if (string [offset] == syntax_end [select] [subset]) {
* length = offset;
goto finished;
}
} while (++subset != string_length (syntax_end [select]));
}
}
finished:
/*do {
2023-08-25 17:00:12 -04:00
++offset;
if (string [offset] == syntax_escape [select]) {
++offset;
continue;
}
if (syntax_derange [select] == 0) {
if (string_compare_limit (& string [offset], syntax_end [select], string_length (syntax_end [select]))) {
2023-08-28 09:03:06 -04:00
* length = offset + string_length (syntax_end [select]);
2023-09-12 05:03:51 -04:00
return (select);
2023-08-25 17:00:12 -04:00
}
} else {
int subset = 0;
2023-08-28 09:03:06 -04:00
if (string_compare (syntax_end [select], "") == 0) {
2023-08-25 17:00:12 -04:00
break;
} do {
if (string [offset] == syntax_end [select] [subset]) {
2023-08-28 09:03:06 -04:00
* length = offset;
2023-09-12 05:03:51 -04:00
return (select);
2023-08-25 17:00:12 -04:00
}
} while (++subset != (int) string_length (syntax_end [select]));
}
2023-11-18 17:33:41 -05:00
} while (string [offset - 1] != '\0');*/
2023-09-12 05:03:51 -04:00
return (select);
2023-08-25 17:00:12 -04:00
}
#endif