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

Functions for parsing tokens from strings with support for delimiters and quotations. More...

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

Go to the source code of this file.

Functions

char * seek_level (char *s, char qs, char qe)
 
int in_charset (char c, char *s)
 
char * get_token_t (char *s, char *t)
 Extracts a token from a string based on delimiter characters.
 
char * get_token_tq (char *s, char *ts, char *te, char *qs, char *qe)
 Extracts a token from a string with support for multiple delimiter and quotation sets.
 
void interpret_escaped_quotes (char *s)
 Processes a string to interpret and replace escaped quotation marks.
 

Detailed Description

Functions for parsing tokens from strings with support for delimiters and quotations.

This file provides implementations for extracting tokens from character strings based on specified delimiter sets and quotation marks. It includes functions to handle nested quotations and escaped characters, facilitating robust tokenization of complex strings.

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 get_token_tq.c.

Function Documentation

◆ get_token_t()

char * get_token_t ( char * s,
char * t )

Extracts a token from a string based on delimiter characters.

The get_token_t function retrieves a token from the input string s, where a token is defined as a sequence of characters bounded by any of the delimiter characters specified in the string t. It intelligently handles quoted sections, allowing delimiters within quotes to be part of the token.

Parameters
sThe input string to parse.
tA string containing delimiter characters.
Returns
A dynamically allocated string containing the extracted token, or NULL if no token is found.

Definition at line 40 of file get_token_tq.c.

40 {
41 char *ptr0, *ptr1, *ptr;
42
43 /* skip all leading characters of s found in string t */
44 ptr0 = s;
45 while (in_charset(*s, t) && *s)
46 s++;
47 if (*s == 0)
48 return (NULL);
49 ptr1 = s;
50
51 /* skip to next character of s found in t, skipping over quoted */
52 /* portions */
53 do {
54 if (*s == '"' && !(s != ptr0 && *(s - 1) == '\\')) {
55 s++;
56 while (*s && !(*s == '"' && *(s - 1) != '\\'))
57 s++;
58 if (*s == '"')
59 s++;
60 } else
61 s++;
62 } while (*s && !in_charset(*s, t));
63
64 ptr = tmalloc(sizeof(*ptr) * (s - ptr1 + 1));
65 strncpy(ptr, ptr1, s - ptr1);
66 ptr[s - ptr1] = 0;
67
68 strcpy_ss(ptr0, s);
69
71 return (ptr);
72}
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59
void interpret_escaped_quotes(char *s)
Processes a string to interpret and replace escaped quotation marks.
char * strcpy_ss(char *dest, const char *src)
Safely copies a string, handling memory overlap.
Definition str_copy.c:34

◆ get_token_tq()

char * get_token_tq ( char * s,
char * ts,
char * te,
char * qs,
char * qe )

Extracts a token from a string with support for multiple delimiter and quotation sets.

The get_token_tq function retrieves a token from the input string s based on two sets of delimiter characters (ts for token start and te for token end) and two sets of quotation characters (qs for quotation start and qe for quotation end). It ensures that delimiters within quoted sections are ignored, allowing for nested or paired quotations.

Parameters
sThe input string to parse.
tsA string containing token start delimiter characters.
teA string containing token end delimiter characters.
qsA string containing quotation start characters.
qeA string containing quotation end characters.
Returns
A dynamically allocated string containing the extracted token, or NULL if no token is found.

Definition at line 108 of file get_token_tq.c.

111{
112 register char *ptr0, *ptr1, *ptr;
113 register int in_quotes;
114
115 /* skip all leading characters of s found in string t */
116 ptr0 = s;
117 while (*s && in_charset(*s, ts) && !in_charset(*s, qs)) {
118 s++;
119 }
120 if (*s == 0)
121 return (NULL);
122 ptr1 = s;
123 if ((in_quotes = in_charset(*s, qs)))
124 s++;
125
126 /* skip to next character of s found in t */
127 do {
128 if (in_quotes) {
129 if ((ptr = seek_level(s, *(qs + in_quotes - 1), *(qe + in_quotes - 1)))) {
130 s = ptr;
131 in_quotes = 0;
132 } else {
133 s += strlen(s);
134 in_quotes = 0;
135 }
136 } else {
137 in_quotes = in_charset(*s, qs);
138 s++;
139 }
140 } while (*s && (in_quotes || !in_charset(*s, te)));
141
142 ptr = tmalloc((unsigned)sizeof(*ptr) * (s - ptr1 + 1));
143 strncpy(ptr, ptr1, s - ptr1);
144 ptr[s - ptr1] = 0;
145
146 if (*s)
147 strcpy_ss(ptr0, s + 1);
148 else
149 *ptr0 = *s;
150
152 return (ptr);
153}

◆ in_charset()

int in_charset ( char c,
char * s )

Definition at line 80 of file get_token_tq.c.

80 {
81 register int i;
82
83 i = 1;
84 while (*set) {
85 if (*set == c)
86 return (i);
87 set++;
88 i++;
89 }
90 return (0);
91}

◆ interpret_escaped_quotes()

void interpret_escaped_quotes ( char * s)

Processes a string to interpret and replace escaped quotation marks.

The interpret_escaped_quotes function scans the input string s and replaces any escaped quotation marks (e.g., \"</tt>) with actual quotation marks (<tt>"), effectively removing the escape character and preserving the intended quote in the string.

Parameters
sThe string in which to interpret escaped quotes.

Definition at line 192 of file get_token_tq.c.

192 {
193 char *ptr;
194
195 ptr = s;
196 while (*ptr) {
197 if (*ptr == '\\' && *(ptr + 1) == '"')
198 strcpy_ss(ptr, ptr + 1);
199 else
200 ptr++;
201 }
202}

◆ seek_level()

char * seek_level ( char * s,
char qs,
char qe )

Definition at line 165 of file get_token_tq.c.

165 {
166 register int qlevel;
167 char *ptr0;
168
169 ptr0 = s;
170 qlevel = 1;
171 while (*s && qlevel) {
172 if (*s == qe && !(s != ptr0 && *(s - 1) == '\\'))
173 qlevel--;
174 else if (*s == qs && !(s != ptr0 && *(s - 1) == '\\'))
175 qlevel++;
176 s++;
177 }
178 if (qlevel == 0)
179 return (s);
180 return (NULL);
181}