File: strstr_icase_blank.c

package info (click to toggle)
mudita24 1.0.3%2Bsvn13-6
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 604 kB
  • ctags: 626
  • sloc: ansic: 8,577; makefile: 5
file content (144 lines) | stat: -rw-r--r-- 3,877 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 *  Function to search string in string with ignoring case sensitivity
 *  and times of blank
 *  Copyright (c) by Dirk Kalis<dirk.kalis@t-online.de>
 *
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 */

#include <string.h>
#include <ctype.h>

#ifndef MAX_SEARCH_FILED_LENGTH
#define MAX_SEARCH_FIELD_LENGTH 1024
#endif
#ifndef SEP_CHAR
#define SEP_CHAR ' '
#endif
#ifndef NOTFOUND
#define NOTFOUND -1
#endif

/*
 * Search string in string with ignoring case sensitivity and times of blank
 * and comment lines.
 * Blanks will be replaced with SEP_CHAR before compare.
 * Comment lines are lines beginning with first non blank character '#'.
 * Return value is the position in string1 relative to the begin of string1.
 */
int strstr_icase_blank(const char * const string1, const char * const string2)
{
	int position, i, j;
	char line[MAX_SEARCH_FIELD_LENGTH];
	char cmp_line[MAX_SEARCH_FIELD_LENGTH];
	char search_string[MAX_SEARCH_FIELD_LENGTH];
	char *pstr;
	int pos_first_non_blank;

	strncpy(search_string, string2, MAX_SEARCH_FIELD_LENGTH);
	search_string[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';

	pos_first_non_blank = -1;
	// convert search string in upper case
	for (i = 0; i < strlen(search_string); i++)
	{
		if ((pos_first_non_blank < 0) && (!isblank(search_string[i])))
			pos_first_non_blank = i;
		search_string[i] = (char)toupper(search_string[i]);
	}

	// replace blanks in search string with SEP_CHAR to compare without blanks
	i = pos_first_non_blank;
	j = 0;
	while(i < strlen(search_string))
	{
		if (j > 0) {
			cmp_line[j] = SEP_CHAR;
			j++;
		}
		sscanf(&search_string[i], "%s", cmp_line + j);
		i += strlen(cmp_line + j) + 1;
		j += strlen(cmp_line + j);
		for(; i < strlen(search_string); i++)
		{
			if (isblank(search_string[i])) {
				continue
				;
			} else {
				break
				;
			}
		}
	}
	strncpy(search_string, cmp_line, strlen(search_string));

	position = 0;
	while (position < strlen(string1))
	{
		strncpy(line, (string1 + (position * sizeof(char))), MAX_SEARCH_FIELD_LENGTH);
		line[MAX_SEARCH_FIELD_LENGTH - 1] = '\0';
		pos_first_non_blank = -1;
		for (i = 0; i < strlen(line); i++)
		{
			if ((pos_first_non_blank < 0) && (!isblank(line[i])))
				pos_first_non_blank = i;
			line[i] = (char)toupper(line[i]);
			if (line[i] == '\n') {
				line[i] = '\0';
				break
				;
			}
		}
		// no compare with comment lines or empty lines
		if ((line[pos_first_non_blank] != '#') && strlen(line) > 0) {
			// replace blanks between entities in input line with SEP_CHAR to compare without blanks
			i = pos_first_non_blank;
			j = 0;
			while(i < strlen(line))
			{
				if (j > 0) {
					cmp_line[j] = SEP_CHAR;
					j++;
				}
				sscanf(&line[i], "%s", cmp_line + j);
				i += strlen(cmp_line + j) + 1;
				j += strlen(cmp_line + j);
				for(; i < strlen(line); i++)
				{
					if (isblank(line[i])) {
						continue
						;
					} else {
						break
						;
					}
				}
			}
			if ((pstr = strstr(cmp_line, search_string)) != NULL) {
				position += (pstr - cmp_line)/sizeof(char) + pos_first_non_blank;
				break
				;
			}
		}
		position += strlen(line) + 1;
	}
	if (position >= strlen(string1)) {
		position = NOTFOUND;
	}

	return position;
}