ucl_util.c (242b24828472137ec4411826b86e753d49bd2c39) ucl_util.c (11dd9ed6647d821e7b43d4f8e64412a2623fbab5)
1/* Copyright (c) 2013, Vsevolod Stakhov
2 * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.

--- 12 unchanged lines hidden (view full) ---

21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "ucl.h"
26#include "ucl_internal.h"
27#include "ucl_chartable.h"
28#include "kvec.h"
1/* Copyright (c) 2013, Vsevolod Stakhov
2 * Copyright (c) 2015 Allan Jude <allanjude@freebsd.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.

--- 12 unchanged lines hidden (view full) ---

21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
22 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 */
24
25#include "ucl.h"
26#include "ucl_internal.h"
27#include "ucl_chartable.h"
28#include "kvec.h"
29#include <limits.h>
29#include <stdarg.h>
30#include <stdio.h> /* for snprintf */
31
32#ifndef _WIN32
33#include <glob.h>
30#include <stdarg.h>
31#include <stdio.h> /* for snprintf */
32
33#ifndef _WIN32
34#include <glob.h>
35#include <sys/param.h>
36#else
37#ifndef NBBY
38#define NBBY 8
34#endif
39#endif
40#endif
35
36#ifdef HAVE_LIBGEN_H
37#include <libgen.h> /* For dirname */
38#endif
39
40typedef kvec_t(ucl_object_t *) ucl_array_t;
41
42#define UCL_ARRAY_GET(ar, obj) ucl_array_t *ar = \

--- 33 unchanged lines hidden (view full) ---

76#endif
77#ifndef MAP_PRIVATE
78#define MAP_PRIVATE 2
79#endif
80#ifndef MAP_FAILED
81#define MAP_FAILED ((void *) -1)
82#endif
83
41
42#ifdef HAVE_LIBGEN_H
43#include <libgen.h> /* For dirname */
44#endif
45
46typedef kvec_t(ucl_object_t *) ucl_array_t;
47
48#define UCL_ARRAY_GET(ar, obj) ucl_array_t *ar = \

--- 33 unchanged lines hidden (view full) ---

82#endif
83#ifndef MAP_PRIVATE
84#define MAP_PRIVATE 2
85#endif
86#ifndef MAP_FAILED
87#define MAP_FAILED ((void *) -1)
88#endif
89
84#ifdef _WIN32
85#include <limits.h>
86#define NBBY CHAR_BIT
87#endif
88
89static void *ucl_mmap(char *addr, size_t length, int prot, int access, int fd, off_t offset)
90{
91 void *map = NULL;
92 HANDLE handle = INVALID_HANDLE_VALUE;
93
94 switch (prot) {
95 default:
96 case PROT_READ:

--- 1752 unchanged lines hidden (view full) ---

1849 return false;
1850 }
1851
1852 return ucl_parser_add_file_full(parser, filename,
1853 parser->default_priority, UCL_DUPLICATE_APPEND,
1854 UCL_PARSE_UCL);
1855}
1856
90static void *ucl_mmap(char *addr, size_t length, int prot, int access, int fd, off_t offset)
91{
92 void *map = NULL;
93 HANDLE handle = INVALID_HANDLE_VALUE;
94
95 switch (prot) {
96 default:
97 case PROT_READ:

--- 1752 unchanged lines hidden (view full) ---

1850 return false;
1851 }
1852
1853 return ucl_parser_add_file_full(parser, filename,
1854 parser->default_priority, UCL_DUPLICATE_APPEND,
1855 UCL_PARSE_UCL);
1856}
1857
1858
1857bool
1859bool
1858ucl_parser_add_fd_priority (struct ucl_parser *parser, int fd,
1859 unsigned priority)
1860ucl_parser_add_fd_full (struct ucl_parser *parser, int fd,
1861 unsigned priority, enum ucl_duplicate_strategy strat,
1862 enum ucl_parse_type parse_type)
1860{
1861 unsigned char *buf;
1862 size_t len;
1863 bool ret;
1864 struct stat st;
1865
1866 if (fstat (fd, &st) == -1) {
1867 ucl_create_err (&parser->err, "cannot stat fd %d: %s",

--- 9 unchanged lines hidden (view full) ---

1877 return false;
1878 }
1879
1880 if (parser->cur_file) {
1881 free (parser->cur_file);
1882 }
1883 parser->cur_file = NULL;
1884 len = st.st_size;
1863{
1864 unsigned char *buf;
1865 size_t len;
1866 bool ret;
1867 struct stat st;
1868
1869 if (fstat (fd, &st) == -1) {
1870 ucl_create_err (&parser->err, "cannot stat fd %d: %s",

--- 9 unchanged lines hidden (view full) ---

1880 return false;
1881 }
1882
1883 if (parser->cur_file) {
1884 free (parser->cur_file);
1885 }
1886 parser->cur_file = NULL;
1887 len = st.st_size;
1885 ret = ucl_parser_add_chunk_priority (parser, buf, len, priority);
1888 ret = ucl_parser_add_chunk_full (parser, buf, len, priority, strat,
1889 parse_type);
1886
1887 if (len > 0) {
1888 ucl_munmap (buf, len);
1889 }
1890
1891 return ret;
1892}
1893
1894bool
1890
1891 if (len > 0) {
1892 ucl_munmap (buf, len);
1893 }
1894
1895 return ret;
1896}
1897
1898bool
1899ucl_parser_add_fd_priority (struct ucl_parser *parser, int fd,
1900 unsigned priority)
1901{
1902 if (parser == NULL) {
1903 return false;
1904 }
1905
1906 return ucl_parser_add_fd_full(parser, fd, parser->default_priority,
1907 UCL_DUPLICATE_APPEND, UCL_PARSE_UCL);
1908}
1909
1910bool
1895ucl_parser_add_fd (struct ucl_parser *parser, int fd)
1896{
1897 if (parser == NULL) {
1898 return false;
1899 }
1900
1901 return ucl_parser_add_fd_priority(parser, fd, parser->default_priority);
1902}

--- 580 unchanged lines hidden (view full) ---

2483
2484ucl_object_iter_t
2485ucl_object_iterate_reset (ucl_object_iter_t it, const ucl_object_t *obj)
2486{
2487 struct ucl_object_safe_iter *rit = UCL_SAFE_ITER (it);
2488
2489 UCL_SAFE_ITER_CHECK (rit);
2490
1911ucl_parser_add_fd (struct ucl_parser *parser, int fd)
1912{
1913 if (parser == NULL) {
1914 return false;
1915 }
1916
1917 return ucl_parser_add_fd_priority(parser, fd, parser->default_priority);
1918}

--- 580 unchanged lines hidden (view full) ---

2499
2500ucl_object_iter_t
2501ucl_object_iterate_reset (ucl_object_iter_t it, const ucl_object_t *obj)
2502{
2503 struct ucl_object_safe_iter *rit = UCL_SAFE_ITER (it);
2504
2505 UCL_SAFE_ITER_CHECK (rit);
2506
2507 if (rit->expl_it != NULL) {
2508 UCL_FREE (sizeof (*rit->expl_it), rit->expl_it);
2509 }
2510
2491 rit->impl_it = obj;
2492 rit->expl_it = NULL;
2493
2494 return it;
2495}
2496
2497const ucl_object_t*
2498ucl_object_iterate_safe (ucl_object_iter_t it, bool expand_values)
2499{
2511 rit->impl_it = obj;
2512 rit->expl_it = NULL;
2513
2514 return it;
2515}
2516
2517const ucl_object_t*
2518ucl_object_iterate_safe (ucl_object_iter_t it, bool expand_values)
2519{
2520 return ucl_object_iterate_full (it, expand_values ? UCL_ITERATE_BOTH :
2521 UCL_ITERATE_IMPLICIT);
2522}
2523
2524const ucl_object_t*
2525ucl_object_iterate_full (ucl_object_iter_t it, enum ucl_iterate_type type)
2526{
2500 struct ucl_object_safe_iter *rit = UCL_SAFE_ITER (it);
2501 const ucl_object_t *ret = NULL;
2502
2503 UCL_SAFE_ITER_CHECK (rit);
2504
2505 if (rit->impl_it == NULL) {
2506 return NULL;
2507 }
2508
2509 if (rit->impl_it->type == UCL_OBJECT || rit->impl_it->type == UCL_ARRAY) {
2510 ret = ucl_object_iterate (rit->impl_it, &rit->expl_it, true);
2511
2527 struct ucl_object_safe_iter *rit = UCL_SAFE_ITER (it);
2528 const ucl_object_t *ret = NULL;
2529
2530 UCL_SAFE_ITER_CHECK (rit);
2531
2532 if (rit->impl_it == NULL) {
2533 return NULL;
2534 }
2535
2536 if (rit->impl_it->type == UCL_OBJECT || rit->impl_it->type == UCL_ARRAY) {
2537 ret = ucl_object_iterate (rit->impl_it, &rit->expl_it, true);
2538
2512 if (ret == NULL) {
2539 if (ret == NULL && (type & UCL_ITERATE_IMPLICIT)) {
2513 /* Need to switch to another implicit object in chain */
2514 rit->impl_it = rit->impl_it->next;
2515 rit->expl_it = NULL;
2540 /* Need to switch to another implicit object in chain */
2541 rit->impl_it = rit->impl_it->next;
2542 rit->expl_it = NULL;
2516 return ucl_object_iterate_safe (it, expand_values);
2543
2544 return ucl_object_iterate_safe (it, type);
2517 }
2518 }
2519 else {
2520 /* Just iterate over the implicit array */
2521 ret = rit->impl_it;
2522 rit->impl_it = rit->impl_it->next;
2545 }
2546 }
2547 else {
2548 /* Just iterate over the implicit array */
2549 ret = rit->impl_it;
2550 rit->impl_it = rit->impl_it->next;
2523 if (expand_values) {
2551
2552 if (type & UCL_ITERATE_EXPLICIT) {
2524 /* We flatten objects if need to expand values */
2525 if (ret->type == UCL_OBJECT || ret->type == UCL_ARRAY) {
2553 /* We flatten objects if need to expand values */
2554 if (ret->type == UCL_OBJECT || ret->type == UCL_ARRAY) {
2526 return ucl_object_iterate_safe (it, expand_values);
2555 return ucl_object_iterate_safe (it, type);
2527 }
2528 }
2529 }
2530
2531 return ret;
2532}
2533
2534void
2535ucl_object_iterate_free (ucl_object_iter_t it)
2536{
2537 struct ucl_object_safe_iter *rit = UCL_SAFE_ITER (it);
2538
2539 UCL_SAFE_ITER_CHECK (rit);
2540
2556 }
2557 }
2558 }
2559
2560 return ret;
2561}
2562
2563void
2564ucl_object_iterate_free (ucl_object_iter_t it)
2565{
2566 struct ucl_object_safe_iter *rit = UCL_SAFE_ITER (it);
2567
2568 UCL_SAFE_ITER_CHECK (rit);
2569
2570 if (rit->expl_it != NULL) {
2571 UCL_FREE (sizeof (*rit->expl_it), rit->expl_it);
2572 }
2573
2541 UCL_FREE (sizeof (*rit), it);
2542}
2543
2544const ucl_object_t *
2545ucl_object_lookup_path (const ucl_object_t *top, const char *path_in) {
2546 return ucl_object_lookup_path_char (top, path_in, '.');
2547}
2548

--- 962 unchanged lines hidden ---
2574 UCL_FREE (sizeof (*rit), it);
2575}
2576
2577const ucl_object_t *
2578ucl_object_lookup_path (const ucl_object_t *top, const char *path_in) {
2579 return ucl_object_lookup_path_char (top, path_in, '.');
2580}
2581

--- 962 unchanged lines hidden ---