vita-toolchain

git clone https://git.neptards.moe/neptards/vita-toolchain.git
Log | Files | Refs | README | LICENSE

yamltreeutil.c (3353B)


      1 /*
      2 
      3 Copyright (C) 2016, David "Davee" Morgan
      4 
      5 Permission is hereby granted, free of charge, to any person obtaining a
      6 copy of this software and associated documentation files (the "Software"),
      7 to deal in the Software without restriction, including without limitation
      8 the rights to use, copy, modify, merge, publish, distribute, sublicense,
      9 and/or sell copies of the Software, and to permit persons to whom the
     10 Software is furnished to do so, subject to the following conditions:
     11 
     12 The above copyright notice and this permission notice shall be included in
     13 all copies or substantial portions of the Software.
     14 
     15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
     18 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     20 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
     21 DEALINGS IN THE SOFTWARE.
     22 
     23 */
     24 
     25 #include "yamltreeutil.h"
     26 
     27 #include <stdlib.h>
     28 #include <stdbool.h>
     29 #include <string.h>
     30 #include <limits.h>
     31 
     32 int yaml_iterate_mapping(yaml_node *node, mapping_functor functor, void *userdata)
     33 {
     34 	// check we have a scalar
     35 	if (node->type != NODE_MAPPING)
     36 		return -1;
     37 	
     38 	yaml_mapping *module = &node->data.mapping;
     39 	
     40 	for (int i = 0; i < module->count; ++i)
     41 	{
     42 		if (functor(module->pairs[i]->lhs, module->pairs[i]->rhs, userdata) < 0)
     43 			return -2;
     44 	}
     45 	
     46 	return 0;
     47 }
     48 
     49 int yaml_iterate_sequence(yaml_node *node, sequence_functor functor, void *userdata)
     50 {
     51 	// check we have a scalar
     52 	if (node->type != NODE_SEQUENCE)
     53 		return -1;
     54 	
     55 	yaml_sequence *module = &node->data.sequence;
     56 	
     57 	for (int i = 0; i < module->count; ++i)
     58 	{
     59 		if (functor(module->nodes[i], userdata) < 0)
     60 			return -2;
     61 	}
     62 	
     63 	return 0;
     64 }
     65 
     66 int process_32bit_integer(yaml_node *node, uint32_t *nid) 
     67 {	
     68 	// check we have a scalar
     69 	if (node->type != NODE_SCALAR)
     70 		return -1;
     71 	
     72 	yaml_scalar *scalar = &node->data.scalar;
     73 	
     74 	char *endptr = NULL;
     75 	uint32_t res = strtoul(scalar->value, &endptr, 0);
     76 	
     77 	if (*endptr) 
     78 	{
     79 		return (res == ULONG_MAX) ? (-2) : (-3);
     80 	}
     81 	
     82 	*nid = res;
     83 	return 0;
     84 }
     85 
     86 int process_boolean(yaml_node *node, uint32_t *boolean) 
     87 {	
     88 	// check we have a scalar
     89 	if (node->type != NODE_SCALAR)
     90 		return -1;
     91 	
     92 	yaml_scalar *scalar = &node->data.scalar;
     93 	
     94 	if (strcmp(scalar->value, "true") == 0)
     95 	{
     96 		*boolean = 1;
     97 	}
     98 	else if (strcmp(scalar->value, "false") == 0)
     99 	{
    100 		*boolean = 0;
    101 	}
    102 	else
    103 	{
    104 		// invalid input
    105 		return -2;
    106 	}
    107 	
    108 	return 0;
    109 }
    110 
    111 int process_bool(yaml_node *node, bool *boolean) 
    112 {	
    113 	uint32_t cast = 0;
    114 	int res = process_boolean(node,&cast);
    115 	
    116 	if(!res){
    117 		*boolean = cast == 1;
    118 	}
    119 	
    120 	return res;
    121 }
    122 
    123 int process_string(yaml_node *node, const char **str) 
    124 {
    125 	// check we have a scalar
    126 	if (node->type != NODE_SCALAR)
    127 		return -1;
    128 	
    129 	yaml_scalar *scalar = &node->data.scalar;
    130 	*str = scalar->value;
    131 	return 0;
    132 }
    133 
    134 int is_scalar(yaml_node *node)
    135 {
    136 	return (node->type == NODE_SCALAR);
    137 }
    138 
    139 int is_mapping(yaml_node *node)
    140 {
    141 	return (node->type == NODE_MAPPING);
    142 }
    143 
    144 int is_sequence(yaml_node *node)
    145 {
    146 	return (node->type == NODE_SEQUENCE);
    147 }