Skip to content

Commit 8954906

Browse files
Merge pull request #16 from dreamer-coding/apply_soap_awareness
Apply soap awareness
2 parents 115453a + d40d0ae commit 8954906

File tree

9 files changed

+1087
-405
lines changed

9 files changed

+1087
-405
lines changed

code/logic/error.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ void fossil_io_error(const char *format, ...) {
3030
char buffer[FOSSIL_IO_BUFFER_SIZE];
3131
vsnprintf(buffer, sizeof(buffer), format, args);
3232

33-
// Sanitize the buffer
34-
fossil_soap_sanitize(buffer);
35-
3633
// Print the sanitized error message
3734
fprintf(stderr, "ERROR: %s\n", buffer);
3835

code/logic/fossil/io/soap.h

Lines changed: 95 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,54 @@
1515
#define FOSSIL_IO_SOAP_H
1616

1717
#include <stdint.h>
18+
#include <stddef.h>
1819

1920
#ifdef __cplusplus
2021
extern "C" {
2122
#endif
2223

2324
/**
24-
* Sanitize a string by replacing curse words with asterisks.
25-
* This function scans the input string for offensive words and replaces them with asterisks,
26-
* thereby making the string suitable for use in contexts where offensive language is not allowed.
27-
* The input string is modified in place.
25+
* @brief Sanitize input text by removing or replacing "rot-brain" and meme-based language.
2826
*
29-
* @param input The input string to be sanitized in-place.
27+
* @param text The input text to sanitize.
28+
* @return A dynamically allocated sanitized string (must be freed by the caller).
3029
*/
31-
void fossil_soap_sanitize(char *input);
30+
char *fossil_io_soap_sanitize(const char *text);
3231

3332
/**
34-
* Check if a word is an offensive word or phrase.
35-
* Returns EXIT_FAILURE if the word is considered offensive, EXIT_SUCCESS otherwise.
33+
* @brief Suggest proper alternatives for rot-brain words or grammar fixes.
34+
*
35+
* @param text The input text.
36+
* @return A dynamically allocated string with suggestions (must be freed by the caller).
3637
*/
37-
int32_t fossil_soap_is_offensive(const char *word);
38+
char *fossil_io_soap_suggest(const char *text);
3839

3940
/**
40-
* Check if a word is meme speak.
41-
* Returns EXIT_FAILURE if the word is considered meme speak, EXIT_SUCCESS otherwise.
41+
* @brief Add a custom word or phrase to the filter.
42+
*
43+
* @param phrase The phrase to add.
44+
* @return 0 on success, nonzero on failure.
4245
*/
43-
int32_t fossil_soap_is_rotbrain(const char *word);
46+
int fossil_io_soap_add_custom_filter(const char *phrase);
4447

4548
/**
46-
* Count offensive words in a string.
47-
* Returns the number of offensive words found in the input string.
49+
* @brief Clear all custom filters.
4850
*/
49-
int32_t fossil_soap_count_offensive(const char *input);
51+
void fossil_io_soap_clear_custom_filters(void);
5052

5153
/**
52-
* Count meme speak words in a string.
53-
* Returns the number of meme speak words found in the input string.
54+
* @brief Detect the tone of a sentence.
55+
*
56+
* @param text The input text.
57+
* @return A string representing the detected tone ("formal", "casual", "sarcastic", etc.).
5458
*/
55-
int32_t fossil_soap_count_rotbrain(const char *input);
59+
const char *fossil_io_soap_detect_tone(const char *text);
5660

5761
#ifdef __cplusplus
5862
}
5963

64+
#include <string>
65+
6066
/**
6167
* C++ wrapper for the SOAP API.
6268
*/
@@ -72,50 +78,98 @@ namespace fossil {
7278
class Soap {
7379
public:
7480
/**
75-
* Sanitize a string by replacing curse words with asterisks.
76-
* This function scans the input string for offensive words and replaces them with asterisks,
77-
* thereby making the string suitable for use in contexts where offensive language is not allowed.
78-
* The input string is modified in place.
81+
* Sanitize input text by removing or replacing "rot-brain" and meme-based language.
82+
*
83+
* @param text The input text to sanitize.
84+
* @return A dynamically allocated sanitized string (must be freed by the caller).
85+
*/
86+
static std::string sanitize(const std::string &text) {
87+
return fossil_io_soap_sanitize(text.c_str());
88+
}
89+
90+
/**
91+
* Suggest proper alternatives for rot-brain words or grammar fixes.
92+
*
93+
* @param text The input text.
94+
* @param format_type Custom format for output (e.g., "*" or "#").
95+
* @return A dynamically allocated string with suggestions (must be freed by the caller).
96+
*/
97+
static std::string suggest(const std::string &text) {
98+
return fossil_io_soap_suggest(text.c_str());
99+
}
100+
101+
/**
102+
* Add a custom word or phrase to the filter.
103+
*
104+
* @param phrase The phrase to add.
105+
* @return 0 on success, nonzero on failure.
106+
*/
107+
static int add_custom_filter(const std::string &phrase) {
108+
return fossil_io_soap_add_custom_filter(phrase.c_str());
109+
}
110+
111+
/**
112+
* Clear all custom filters.
113+
*/
114+
static void clear_custom_filters() {
115+
fossil_io_soap_clear_custom_filters();
116+
}
117+
118+
/**
119+
* Detect the tone of a sentence.
79120
*
80-
* @param input The input string to be sanitized in-place.
121+
* @param text The input text.
122+
* @return A string representing the detected tone ("formal", "casual", "sarcastic", etc.).
81123
*/
82-
static void sanitize(char *input) {
83-
fossil_soap_sanitize(input);
124+
static std::string detect_tone(const std::string &text) {
125+
return fossil_io_soap_detect_tone(text.c_str());
84126
}
85127

86128
/**
87-
* Check if a word is an offensive word or phrase.
88-
* Returns EXIT_FAILURE if the word is considered offensive, EXIT_SUCCESS otherwise.
129+
* Sanitize input text by removing or replacing "rot-brain" and meme-based language.
130+
*
131+
* @param text The input text to sanitize.
132+
* @return A dynamically allocated sanitized string (must be freed by the caller).
89133
*/
90-
static int32_t is_offensive(const char *word) {
91-
return fossil_soap_is_offensive(word);
134+
static char* sanitize(const char* text) {
135+
return fossil_io_soap_sanitize(text);
92136
}
93137

94138
/**
95-
* Check if a word is meme speak.
96-
* Returns EXIT_FAILURE if the word is considered meme speak, EXIT_SUCCESS otherwise.
139+
* Suggest proper alternatives for rot-brain words or grammar fixes.
140+
*
141+
* @param text The input text.
142+
* @param format_type Custom format for output (e.g., "*" or "#").
143+
* @return A dynamically allocated string with suggestions (must be freed by the caller).
97144
*/
98-
static int32_t is_rotbrain(const char *word) {
99-
return fossil_soap_is_rotbrain(word);
145+
static char* suggest(const char* text) {
146+
return fossil_io_soap_suggest(text);
100147
}
101148

102149
/**
103-
* Count offensive words in a string.
104-
* Returns the number of offensive words found in the input string.
150+
* Add a custom word or phrase to the filter.
151+
*
152+
* @param phrase The phrase to add.
153+
* @return 0 on success, nonzero on failure.
105154
*/
106-
static int32_t count_offensive(const char *input) {
107-
return fossil_soap_count_offensive(input);
155+
static int add_custom_filter(const char* phrase) {
156+
return fossil_io_soap_add_custom_filter(phrase);
108157
}
109158

110159
/**
111-
* Count meme speak words in a string.
112-
* Returns the number of meme speak words found in the input string.
160+
* Detect the tone of a sentence.
161+
*
162+
* @param text The input text.
163+
* @return A string representing the detected tone ("formal", "casual", "sarcastic", etc.).
113164
*/
114-
static int32_t count_rotbrain(const char *input) {
115-
return fossil_soap_count_rotbrain(input);
165+
static const char* detect_tone(const char* text) {
166+
return fossil_io_soap_detect_tone(text);
116167
}
168+
117169
};
170+
118171
}
172+
119173
}
120174

121175
#endif

code/logic/input.c

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* -----------------------------------------------------------------------------
1313
*/
1414
#include "fossil/io/input.h"
15-
#include "fossil/io/soap.h"
1615
#include "fossil/io/output.h"
1716

1817
#include <ctype.h>
@@ -54,7 +53,7 @@ void fossil_io_trim(char *str) {
5453
// Function to get a sanitized line of input from a provided stream (or stdin by default)
5554
char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream) {
5655
if (buf == NULL || size == 0 || input_stream == NULL) {
57-
fprintf(stderr, "Error: Invalid buffer or stream\n");
56+
fprintf(stderr, "Error: Invalid buffer or stream.\n");
5857
return NULL;
5958
}
6059

@@ -63,7 +62,7 @@ char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream) {
6362
if (feof(input_stream)) {
6463
return NULL; // End of file reached
6564
}
66-
fprintf(stderr, "Error: Failed to read from input stream\n");
65+
fprintf(stderr, "Error: Failed to read from input stream.\n");
6766
return NULL;
6867
}
6968

@@ -73,9 +72,6 @@ char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream) {
7372
buf[len - 1] = '\0'; // Remove the newline character
7473
}
7574

76-
// Sanitize the input buffer
77-
fossil_soap_sanitize(buf);
78-
7975
// Trim any leading or trailing whitespace
8076
fossil_io_trim(buf);
8177

@@ -84,7 +80,7 @@ char *fossil_io_gets_from_stream(char *buf, size_t size, FILE *input_stream) {
8480

8581
char *fossil_io_gets_from_stream_ex(char *buf, size_t size, FILE *input_stream, int *error_code) {
8682
if (buf == NULL || size == 0 || input_stream == NULL || error_code == NULL) {
87-
fprintf(stderr, "Error: Invalid buffer, stream, or error code\n");
83+
fprintf(stderr, "Error: Invalid buffer, stream, or error code.\n");
8884
return NULL;
8985
}
9086

@@ -95,7 +91,7 @@ char *fossil_io_gets_from_stream_ex(char *buf, size_t size, FILE *input_stream,
9591
return NULL; // End of file reached
9692
}
9793
*error_code = ferror(input_stream);
98-
fprintf(stderr, "Error: Failed to read from input stream\n");
94+
fprintf(stderr, "Error: Failed to read from input stream.\n");
9995
return NULL;
10096
}
10197

@@ -105,9 +101,6 @@ char *fossil_io_gets_from_stream_ex(char *buf, size_t size, FILE *input_stream,
105101
buf[len - 1] = '\0'; // Remove the newline character
106102
}
107103

108-
// Sanitize the input buffer
109-
fossil_soap_sanitize(buf);
110-
111104
// Trim any leading or trailing whitespace
112105
fossil_io_trim(buf);
113106

@@ -132,7 +125,7 @@ int fossil_io_fscanf(FILE *input_stream, const char *format, ...) {
132125

133126
int fossil_io_validate_input_buffer(const char *buf, size_t size) {
134127
if (buf == NULL || size == 0) {
135-
fprintf(stderr, "Error: Invalid buffer or size\n");
128+
fprintf(stderr, "Error: Invalid buffer or size.\n");
136129
return 0;
137130
}
138131
return 1;
@@ -148,7 +141,7 @@ char *fossil_io_gets_utf8(char *buf, size_t size, FILE *input_stream) {
148141
if (feof(input_stream)) {
149142
return NULL; // End of file reached
150143
}
151-
fprintf(stderr, "Error: Failed to read from input stream\n");
144+
fprintf(stderr, "Error: Failed to read from input stream.\n");
152145
return NULL;
153146
}
154147

@@ -158,9 +151,6 @@ char *fossil_io_gets_utf8(char *buf, size_t size, FILE *input_stream) {
158151
buf[len - 1] = '\0'; // Remove the newline character
159152
}
160153

161-
// Sanitize the input buffer
162-
fossil_soap_sanitize(buf);
163-
164154
// Trim any leading or trailing whitespace
165155
fossil_io_trim(buf);
166156

@@ -250,9 +240,6 @@ int fossil_io_validate_sanitize_string(const char *input, char *output, size_t o
250240
// Copy the input string to the output buffer
251241
strncpy(output, input, output_size);
252242

253-
// Sanitize the output buffer
254-
fossil_soap_sanitize(output);
255-
256243
return 1;
257244
}
258245

code/logic/output.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
* -----------------------------------------------------------------------------
1313
*/
1414
#include "fossil/io/output.h"
15-
#include "fossil/io/soap.h"
1615

1716
#include <stdio.h>
1817
#include <stdarg.h>
@@ -136,7 +135,6 @@ void fossil_io_puts(const char *str) {
136135
char sanitized_str[FOSSIL_IO_BUFFER_SIZE];
137136
strncpy(sanitized_str, str, sizeof(sanitized_str));
138137
sanitized_str[sizeof(sanitized_str) - 1] = '\0'; // Ensure null termination
139-
fossil_soap_sanitize(sanitized_str);
140138

141139
// Print the sanitized string with attributes
142140
fossil_io_print_with_attributes(sanitized_str);
@@ -164,9 +162,6 @@ void fossil_io_printf(const char *format, ...) {
164162
char buffer[FOSSIL_IO_BUFFER_SIZE];
165163
vsnprintf(buffer, sizeof(buffer), format, args);
166164

167-
// Sanitize the buffer
168-
fossil_soap_sanitize(buffer);
169-
170165
// Print the sanitized output with attributes
171166
fossil_io_print_with_attributes(buffer);
172167

0 commit comments

Comments
 (0)