SDDSlib
Loading...
Searching...
No Matches
match_string.c File Reference

Provides functions for matching strings with various matching modes. More...

#include "match_string.h"
#include "mdb.h"
#include <ctype.h>

Go to the source code of this file.

Functions

long match_string (char *string, char **option, long n_options, long mode)
 Matches a given string against an array of option strings based on specified modes.
 
int strcmp_case_insensitive (char *s1, char *s2)
 Compares two strings in a case-insensitive manner.
 
int strncmp_case_insensitive (char *s1, char *s2, long n)
 Compares up to a specified number of characters of two strings in a case-insensitive manner.
 

Detailed Description

Provides functions for matching strings with various matching modes.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C. Saunders, R. Soliday

Definition in file match_string.c.

Function Documentation

◆ match_string()

long match_string ( char * string,
char ** option,
long n_options,
long mode )

Matches a given string against an array of option strings based on specified modes.

This function searches for a match of the input string within the provided array of option strings. It supports different matching modes such as wildcard matching, case sensitivity, and whole string matching. Depending on the mode flags, it can return the first match or indicate ambiguity.

Parameters
stringThe string to find a match for.
optionThe array of strings to match against.
n_optionsThe number of option strings in the array.
modeFlags that determine the matching behavior (e.g., WILDCARD_MATCH, MATCH_WHOLE_STRING, CASE_SENSITIVE, RETURN_FIRST_MATCH).
Returns
The index of the matching string in the options array, or -1 if no match is found or if multiple matches exist when ambiguity is not allowed.

Definition at line 34 of file match_string.c.

39 {
40 register long i, i_match, l;
41
42 if (string == NULL)
43 return (-1);
44
45 if (mode & WILDCARD_MATCH) {
46 for (i = 0; i < n_options; i++)
47 if (wild_match(string, option[i]))
48 return i;
49 return -1;
50 }
51
52 if (!(mode & MATCH_WHOLE_STRING)) {
53 l = strlen(string);
54 i_match = -1;
55 if (mode & CASE_SENSITIVE) {
56 for (i = 0; i < n_options; i++) {
57 if (strncmp(string, option[i], l) == 0) {
58 if (mode & RETURN_FIRST_MATCH)
59 return (i);
60 if (i_match != -1)
61 return (-1);
62 i_match = i;
63 }
64 }
65 return (i_match);
66 } else {
67 for (i = 0; i < n_options; i++) {
68 if (strncmp_case_insensitive(string, option[i], MIN(l, (long)strlen(option[i]))) == 0) {
69 if (mode & RETURN_FIRST_MATCH)
70 return (i);
71 if (i_match != -1)
72 return (-1);
73 i_match = i;
74 }
75 }
76 return (i_match);
77 }
78 }
79
80 if (mode & MATCH_WHOLE_STRING) {
81 i_match = -1;
82 if (mode & CASE_SENSITIVE) {
83 for (i = 0; i < n_options; i++) {
84 if (strcmp(string, option[i]) == 0) {
85 if (mode & RETURN_FIRST_MATCH)
86 return (i);
87 if (i_match != -1)
88 return (-1);
89 i_match = i;
90 }
91 }
92 return (i_match);
93 } else {
94 for (i = 0; i < n_options; i++) {
95 if (strcmp_case_insensitive(string, option[i]) == 0) {
96 if (mode & RETURN_FIRST_MATCH)
97 return (i);
98 if (i_match != -1)
99 return (-1);
100 i_match = i;
101 }
102 }
103 return (i_match);
104 }
105 }
106
107 /* unknown set of flags */
108 puts("error: unknown flag combination in match_string()");
109 puts(" contact programmer!");
110 exit(1);
111}
int strncmp_case_insensitive(char *s1, char *s2, long n)
Compares up to a specified number of characters of two strings in a case-insensitive manner.
int strcmp_case_insensitive(char *s1, char *s2)
Compares two strings in a case-insensitive manner.
int wild_match(char *string, char *template)
Determine whether one string is a wildcard match for another.
Definition wild_match.c:49

◆ strcmp_case_insensitive()

int strcmp_case_insensitive ( char * s1,
char * s2 )

Compares two strings in a case-insensitive manner.

This function compares two null-terminated strings without considering the case of the characters. It returns an integer less than, equal to, or greater than zero if the first string is found, respectively, to be less than, to match, or be greater than the second string.

Parameters
s1The first string to compare.
s2The second string to compare.
Returns
An integer indicating the relationship between the strings:
  • Less than zero if s1 is less than s2.
  • Zero if s1 is equal to s2.
  • Greater than zero if s1 is greater than s2.

Definition at line 128 of file match_string.c.

128 {
129 register char *ptr1, *ptr2;
130
131 ptr1 = s1;
132 ptr2 = s2;
133 while (*ptr1 && *ptr2 && tolower(*ptr1) == tolower(*ptr2)) {
134 ptr1++;
135 ptr2++;
136 }
137 return ((int)(*ptr1 - *ptr2));
138}

◆ strncmp_case_insensitive()

int strncmp_case_insensitive ( char * s1,
char * s2,
long n )

Compares up to a specified number of characters of two strings in a case-insensitive manner.

This function compares the first n characters of two null-terminated strings without considering the case of the characters. It returns an integer less than, equal to, or greater than zero if the first n characters of the first string are found, respectively, to be less than, to match, or be greater than the second string.

Parameters
s1The first string to compare.
s2The second string to compare.
nThe maximum number of characters to compare.
Returns
An integer indicating the relationship between the strings up to n characters:
  • Less than zero if s1 is less than s2.
  • Zero if the first n characters of s1 are equal to s2.
  • Greater than zero if s1 is greater than s2.

Definition at line 157 of file match_string.c.

157 {
158 register char *ptr1, *ptr2;
159 register long i;
160
161 ptr1 = s1;
162 ptr2 = s2;
163 i = 0;
164 while (i < n && *ptr1 && *ptr2 && tolower(*ptr1) == tolower(*ptr2)) {
165 ptr1++;
166 ptr2++;
167 i++;
168 }
169
170 if (i == n)
171 return (0);
172
173 return ((int)(*ptr1 - *ptr2));
174}