Skip to content

Commit

Permalink
supress warning with smart quit
Browse files Browse the repository at this point in the history
  • Loading branch information
timbergeron committed Dec 7, 2024
1 parent 5c3f913 commit 39ce361
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
6 changes: 6 additions & 0 deletions Quake/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -1474,6 +1474,9 @@ const char *Cmd_CompleteCommand (const char *partial)

qboolean Cmd_IsQuitMistype (const char* input) // woods -- #smartquit
{
if (!input)
return false;

if (Cvar_FindVar(input) || Cmd_Exists2(input) || Cmd_AliasExists(input))
return false;

Expand All @@ -1485,6 +1488,9 @@ qboolean Cmd_IsQuitMistype (const char* input) // woods -- #smartquit
int threshold = 2; // Define a threshold for mistypes (e.g., distance <= 2)
int distance = LevenshteinDistance(input, correct_cmd); // Calculate the Levenshtein distance

if (distance == -1)
return false;

return distance > 0 && distance <= threshold; // Return true if within the threshold, else false
}

Expand Down
28 changes: 23 additions & 5 deletions Quake/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -4710,14 +4710,32 @@ void SetChatInfo (int flags) // woods #chatinfo

int LevenshteinDistance (const char* s, const char* t) // woods -- #smartquit -- function to calculate the Levenshtein Distance
{
// Check for null pointers
if (!s || !t) return -1;

int len_s = strlen(s);
int len_t = strlen(t);

// Handle empty strings
if (len_s == 0) return len_t;
if (len_t == 0) return len_s;

// Allocate a matrix dynamically
int** matrix = malloc((len_s + 1) * sizeof(int*));
for (int i = 0; i <= len_s; i++)
{
matrix[i] = malloc((len_t + 1) * sizeof(int));
int** matrix = (int**)calloc(len_s + 1, sizeof(int*));
if (!matrix) return -1; // Handle allocation failure

int distance = -1;

for (int i = 0; i <= len_s; i++) {
matrix[i] = (int*)calloc(len_t + 1, sizeof(int));
if (!matrix[i]) {
// Clean up previously allocated memory if allocation fails
for (int j = 0; j < i; j++) {
free(matrix[j]);
}
free(matrix);
return -1;
}
}

// Initialize the matrix
Expand All @@ -4740,7 +4758,7 @@ int LevenshteinDistance (const char* s, const char* t) // woods -- #smartquit --
}
}

int distance = matrix[len_s][len_t];
distance = matrix[len_s][len_t];

// Free the matrix
for (int i = 0; i <= len_s; i++) {
Expand Down

0 comments on commit 39ce361

Please sign in to comment.