• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /************************************************************************
2 Copyright (c) 2015, The Linux Foundation. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are
6 met:
7     * Redistributions of source code must retain the above copyright
8       notice, this list of conditions and the following disclaimer.
9     * Redistributions in binary form must reproduce the above
10       copyright notice, this list of conditions and the following
11       disclaimer in the documentation and/or other materials provided
12       with the distribution.
13     * Neither the name of The Linux Foundation nor the names of its
14       contributors may be used to endorse or promote products derived
15       from this software without specific prior written permission.
16 
17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 ************************************************************************/
29 
30 /**
31  * @file datatop_str.c
32  * @brief Algorithms used for storing and polling data created.
33  *
34  * Methods created which store collected data from files in
35  * dictionaries for many different file formats.
36  */
37 
38 #include <stdio.h>
39 #include <string.h>
40 #include "datatop_str.h"
41 
42 /** @brief Reads an individual line from a file.
43  *
44  * Will read from buf2 until either a '\n' is reached, or the end of buf1
45  * or buf2 is reached. The result is guaranteed to be null terminated.
46  *
47  * @param buf1 Destination buffer to store the read line.
48  * @param len1 Size of destination buffer.
49  * @param buf2 Source buffer to read lines from. Const, will not be
50  *             modified by this function.
51  * @param len2 Size of the source buffer.
52  * @param start Offset (in bytes) to start reading from source buffer.
53  * @return Length of line (of chars).
54  */
dt_read_line(char * buf1,int len1,const char * buf2,int len2,int start)55 int dt_read_line(char *buf1, int len1, const char *buf2, int len2, int start)
56 {
57 	int i, j;
58 
59 	if (len1 < 1 || len2 < 1 || start < 0 || start > len2)
60 		return 0;
61 
62 	if (buf1 == 0 || buf2 == 0)
63 		return 0;
64 
65 	i = 0;
66 	j = start;
67 
68 	while ((i < (len1-1)) && (j < len2)) {
69 		buf1[i] = buf2[j];
70 		if (buf1[i] == '\n')
71 			break;
72 		i++;
73 		j++;
74 	}
75 	buf1[i] = 0;
76 	return i;
77 }
78 
79 /**
80  * @brief Parses files that have Names and Values on separate lines.
81  *
82  * Use this method to parse files that have names on one line, followed by
83  * the corresponding values on the next line. Such as "/proc/net/netstat"
84  *
85  * @param line1 First line that is parsed to store the datapoint names as keys.
86  * @param len1 Length of line1.
87  * @param line2 Second line that is parsed to store the datapoint values as dictionary values.
88  * @param len2 Length of line2.
89  * @param dict Dictionary that keys and values are added to.
90  * @return Number of key/val pairs in the dictionary.
91  */
dt_parse_proc_dictionary(char * line1,int len1,char * line2,int len2,struct dt_procdict * dict)92 int dt_parse_proc_dictionary(char *line1, int len1, char *line2,
93 			     int len2, struct dt_procdict *dict)
94 {
95 	int i, j, k;
96 
97 	if (len1 < 1 || len2 < 1)
98 		return 0;
99 
100 	if (line1 == 0 || line2 == 0 || dict == 0)
101 		return 0;
102 
103 	k = 0;
104 	for (i = 0; i < len1 && k < DTOP_DICT_SIZE; i++) {
105 		if (line1[i] == ' ') {
106 			dict->key[k] = &line1[i+1];
107 			line1[i] = 0;
108 			k++;
109 		}
110 	}
111 	j = k;
112 
113 	k = 0;
114 	for (i = 0; i < len2 && k < DTOP_DICT_SIZE; i++) {
115 		if (line2[i] == ' ') {
116 			dict->val[k] = &line2[i+1];
117 			line2[i] = 0;
118 			k++;
119 		}
120 	}
121 	if (j != k) {
122 		if (k < j)
123 			j = k;
124 		fprintf(stderr, "Warning, list index length mismatch\n");
125 	}
126 	dict->max = j;
127 	return j;
128 }
129 
130 /**
131  * @brief Parses line for prefixes for files that have individual data_point prefixes.
132  *
133  * Use this method for lines that have a prefix before data begins. Such as
134  * "/proc/net/snmp"
135  *
136  * @param line1 Line to parse to find datapoint prefix.
137  * @param len1 Length of line1.
138  * @param dict Dictionary prefix is being added to.
139  */
dt_parse_for_prefix(char * line1,int len1,struct dt_procdict * dict)140 void dt_parse_for_prefix(char *line1, int len1, struct dt_procdict *dict)
141 {
142 	int i, j, k;
143 
144 	if (len1 < 1)
145 		return;
146 
147 	if (line1 == 0 || dict == 0)
148 		return;
149 
150 	k = 0;
151 	for (i = 0; i < len1 && k < DTOP_DICT_SIZE; i++) {
152 		if (line1[i] == ' ') {
153 			dict->key[k] = &line1[i+1];
154 			line1[i] = 0;
155 			k++;
156 		}
157 	}
158 
159 	for (j = 0; j < k; j++)
160 		dict->val[j] = &line1[0];
161 
162 	for (j = 0; j < len1; j++) {
163 		if (line1[j] == ':')
164 			line1[j] = 0;
165 	}
166 }
167 
168 /**
169  * @brief Finds the dictionary index of a data_point name.
170  *
171  * @param str Name of data_point that is to be located in dict.
172  * @param dict Dictionary to look through for dp name.
173  * @return Dictionary index of name if found.
174  * @return -1 if name not found in dictionary keys.
175  */
dt_find_dict_idx(const char * str,struct dt_procdict * dict)176 int dt_find_dict_idx(const char *str, struct dt_procdict *dict)
177 {
178 	int i;
179 	if (str == 0 || dict == 0)
180 		return -1;
181 
182 	for (i = 0; i < dict->max; i++) {
183 		if (dict->key[i] && !strcmp(str, dict->key[i]))
184 			return i;
185 	}
186 	return -1;
187 }
188 
189 /**
190  * @brief Parses files that have Names and Values on same line.
191  *
192  * Use this method to parse lines that have a dp name followed
193  * by a dp value. Such as "/proc/net/snmp6"
194  *
195  * @param line1 Line to parse to find datapoint names and values.
196  * @param len1 Length of line1.
197  * @param l Index in the dictionary the key/val pair is added to.
198  * @param dict Dictionary the keys and values are added to.
199  * @return Number of key/val pairs in the dictionary.
200  */
dt_parse_proc_same_line_key_and_val(char * line1,int len1,int l,struct dt_procdict * dict)201 int dt_parse_proc_same_line_key_and_val(char *line1, int len1,
202 					int l, struct dt_procdict *dict)
203 {
204 	int i, k, n;
205 	if (len1 < 1)
206 		return 0;
207 
208 	if (line1 == 0 || dict == 0)
209 		return 0;
210 
211 	k = l;
212 	for (i = 0; i < len1 && k < DTOP_DICT_SIZE; i++) {
213 		if (line1[i] == ' ') {
214 			dict->key[k] = &line1[0];
215 			line1[i] = 0;
216 			for (n = i+1; n < len1; n++) {
217 				if (line1[n] != ' ') {
218 					dict->val[k] = &line1[n+1];
219 					break;
220 				}
221 			}
222 			break;
223 		}
224 	}
225 	k++;
226 	dict->max = k;
227 	return k;
228 }
229 
230 /**
231  * @brief Parses files that have a single line.
232  *
233  * Parses a single line file for csv, tab-separated, space-separated, and single
234  * value formats and adds values to a dictionary. Such as
235  * "/proc/sys/net/ipv4/ping_group_range"
236  *
237  * Use this method to parse lines that contain only values.
238  *
239  * @param line1 Line to parse.
240  * @param len1 Length of line1.
241  * @param dict Dictionary datapoints are added to.
242  * @return Number of values dictionary holds.
243  */
dt_single_line_parse(char * line1,int len1,struct dt_procdict * dict)244 int dt_single_line_parse(char *line1, int len1, struct dt_procdict *dict)
245 {
246 	int i, k;
247 	k = 0;
248 	dict->val[k] = &line1[0];
249 	k++;
250 
251 	for (i = 0; i < len1; i++) {
252 		if (line1[i] == ' ' || line1[i] == ',' || line1[i] == '	') {
253 			line1[i] = 0;
254 			dict->val[k] = &line1[i+1];
255 			k++;
256 		}
257 	}
258 	dict->max = k;
259 	return k;
260 }
261