Major rework of the directory structure and the entire build system.
[oweals/busybox.git] / libbb / read_package_field.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Utility routines.
4  *
5  * Copyright (C) many different people.  If you wrote this, please
6  * acknowledge your work.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  * USA
22  */
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include "libbb.h"
27
28 /*
29  * Gets the next package field from package_buffer, seperated into the field name
30  * and field value, it returns the int offset to the first character of the next field
31  */
32 int read_package_field(const char *package_buffer, char **field_name, char **field_value)
33 {
34         int offset_name_start = 0;
35         int offset_name_end = 0;
36         int offset_value_start = 0;
37         int offset_value_end = 0;
38         int offset = 0;
39         int next_offset;
40         int name_length;
41         int value_length;
42         int exit_flag = FALSE;
43
44         if (package_buffer == NULL) {
45                 *field_name = NULL;
46                 *field_value = NULL;
47                 return(-1);
48         }
49         while (1) {
50                 next_offset = offset + 1;
51                 switch (package_buffer[offset]) {
52                         case('\0'):
53                                 exit_flag = TRUE;
54                                 break;
55                         case(':'):
56                                 if (offset_name_end == 0) {
57                                         offset_name_end = offset;
58                                         offset_value_start = next_offset;
59                                 }
60                                 /* TODO: Name might still have trailing spaces if ':' isnt
61                                  * immediately after name */
62                                 break;
63                         case('\n'):
64                                 /* TODO: The char next_offset may be out of bounds */
65                                 if (package_buffer[next_offset] != ' ') {
66                                         exit_flag = TRUE;
67                                         break;
68                                 }
69                         case('\t'):
70                         case(' '):
71                                 /* increment the value start point if its a just filler */
72                                 if (offset_name_start == offset) {
73                                         offset_name_start++;
74                                 }
75                                 if (offset_value_start == offset) {
76                                         offset_value_start++;
77                                 }
78                                 break;
79                 }
80                 if (exit_flag == TRUE) {
81                         /* Check that the names are valid */
82                         offset_value_end = offset;
83                         name_length = offset_name_end - offset_name_start;
84                         value_length = offset_value_end - offset_value_start;
85                         if (name_length == 0) {
86                                 break;
87                         }
88                         if ((name_length > 0) && (value_length > 0)) {
89                                 break;
90                         }
91
92                         /* If not valid, start fresh with next field */
93                         exit_flag = FALSE;
94                         offset_name_start = offset + 1;
95                         offset_name_end = 0;
96                         offset_value_start = offset + 1;
97                         offset_value_end = offset + 1;
98                         offset++;
99                 }
100                 offset++;
101         }
102         if (name_length == 0) {
103                 *field_name = NULL;
104         } else {
105                 *field_name = xstrndup(&package_buffer[offset_name_start], name_length);
106         }
107         if (value_length > 0) {
108                 *field_value = xstrndup(&package_buffer[offset_value_start], value_length);
109         } else {
110                 *field_value = NULL;
111         }
112         return(next_offset);
113 }
114