3
0

read_package_field.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Utility routines.
  4. *
  5. * Copyright (C) many different people.
  6. * If you wrote this, please 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. #include <stdlib.h>
  24. #include <string.h>
  25. #include "libbb.h"
  26. /*
  27. * Gets the next package field from package_buffer, seperated into the field name
  28. * and field value, it returns the int offset to the first character of the next field
  29. */
  30. int read_package_field(const char *package_buffer, char **field_name, char **field_value)
  31. {
  32. int offset_name_start = 0;
  33. int offset_name_end = 0;
  34. int offset_value_start = 0;
  35. int offset_value_end = 0;
  36. int offset = 0;
  37. int next_offset;
  38. int name_length;
  39. int value_length;
  40. int exit_flag = FALSE;
  41. if (package_buffer == NULL) {
  42. *field_name = NULL;
  43. *field_value = NULL;
  44. return(-1);
  45. }
  46. while (1) {
  47. next_offset = offset + 1;
  48. switch (package_buffer[offset]) {
  49. case('\0'):
  50. exit_flag = TRUE;
  51. break;
  52. case(':'):
  53. if (offset_name_end == 0) {
  54. offset_name_end = offset;
  55. offset_value_start = next_offset;
  56. }
  57. /* TODO: Name might still have trailing spaces if ':' isnt
  58. * immediately after name */
  59. break;
  60. case('\n'):
  61. /* TODO: The char next_offset may be out of bounds */
  62. if (package_buffer[next_offset] != ' ') {
  63. exit_flag = TRUE;
  64. break;
  65. }
  66. case('\t'):
  67. case(' '):
  68. /* increment the value start point if its a just filler */
  69. if (offset_name_start == offset) {
  70. offset_name_start++;
  71. }
  72. if (offset_value_start == offset) {
  73. offset_value_start++;
  74. }
  75. break;
  76. }
  77. if (exit_flag) {
  78. /* Check that the names are valid */
  79. offset_value_end = offset;
  80. name_length = offset_name_end - offset_name_start;
  81. value_length = offset_value_end - offset_value_start;
  82. if (name_length == 0) {
  83. break;
  84. }
  85. if ((name_length > 0) && (value_length > 0)) {
  86. break;
  87. }
  88. /* If not valid, start fresh with next field */
  89. exit_flag = FALSE;
  90. offset_name_start = offset + 1;
  91. offset_name_end = 0;
  92. offset_value_start = offset + 1;
  93. offset_value_end = offset + 1;
  94. offset++;
  95. }
  96. offset++;
  97. }
  98. if (name_length == 0) {
  99. *field_name = NULL;
  100. } else {
  101. *field_name = bb_xstrndup(&package_buffer[offset_name_start], name_length);
  102. }
  103. if (value_length > 0) {
  104. *field_value = bb_xstrndup(&package_buffer[offset_value_start], value_length);
  105. } else {
  106. *field_value = NULL;
  107. }
  108. return(next_offset);
  109. }