Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 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 | |
Glenn L McGrath | ee0d93e | 2001-04-21 10:26:15 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
Glenn L McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 25 | #include <string.h> |
| 26 | #include "libbb.h" |
| 27 | |
| 28 | /* |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 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 |
Glenn L McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 31 | */ |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 32 | int read_package_field(const char *package_buffer, char **field_name, char **field_value) |
Glenn L McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 33 | { |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 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; |
Glenn L McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 43 | |
Glenn L McGrath | 481d19b | 2001-07-11 15:43:03 +0000 | [diff] [blame] | 44 | if (package_buffer == NULL) { |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 45 | *field_name = NULL; |
| 46 | *field_value = NULL; |
| 47 | return(-1); |
Glenn L McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 48 | } |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 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; |
Glenn L McGrath | 481d19b | 2001-07-11 15:43:03 +0000 | [diff] [blame] | 79 | } |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 80 | if (exit_flag) { |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 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++; |
Glenn L McGrath | 481d19b | 2001-07-11 15:43:03 +0000 | [diff] [blame] | 101 | } |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 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); |
Glenn L McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 113 | } |
| 114 | |