5 |
6 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/zindex.iml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | zindex
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.cdt.autotools.core.genmakebuilderV2
10 |
11 |
12 |
13 |
14 | org.eclipse.cdt.managedbuilder.core.genmakebuilder
15 | clean,full,incremental,
16 |
17 |
18 |
19 |
20 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder
21 | full,incremental,
22 |
23 |
24 |
25 |
26 |
27 | org.eclipse.cdt.core.cnature
28 | org.eclipse.cdt.core.ccnature
29 | org.eclipse.cdt.managedbuilder.core.managedBuildNature
30 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature
31 | org.eclipse.cdt.autotools.core.autotoolsNatureV2
32 |
33 |
34 |
--------------------------------------------------------------------------------
/.settings/language.settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.settings/org.eclipse.cdt.managedbuilder.core.prefs:
--------------------------------------------------------------------------------
1 | eclipse.preferences.version=1
2 | environment/buildEnvironmentInclude/org.eclipse.linuxtools.cdt.autotools.core.configuration.build.1821571161/CPATH/delimiter=\:
3 | environment/buildEnvironmentInclude/org.eclipse.linuxtools.cdt.autotools.core.configuration.build.1821571161/CPATH/operation=remove
4 | environment/buildEnvironmentInclude/org.eclipse.linuxtools.cdt.autotools.core.configuration.build.1821571161/CPLUS_INCLUDE_PATH/delimiter=\:
5 | environment/buildEnvironmentInclude/org.eclipse.linuxtools.cdt.autotools.core.configuration.build.1821571161/CPLUS_INCLUDE_PATH/operation=remove
6 | environment/buildEnvironmentInclude/org.eclipse.linuxtools.cdt.autotools.core.configuration.build.1821571161/C_INCLUDE_PATH/delimiter=\:
7 | environment/buildEnvironmentInclude/org.eclipse.linuxtools.cdt.autotools.core.configuration.build.1821571161/C_INCLUDE_PATH/operation=remove
8 | environment/buildEnvironmentInclude/org.eclipse.linuxtools.cdt.autotools.core.configuration.build.1821571161/append=true
9 | environment/buildEnvironmentInclude/org.eclipse.linuxtools.cdt.autotools.core.configuration.build.1821571161/appendContributed=true
10 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: false
2 | language: cpp
3 | compiler:
4 | - gcc
5 | - clang
6 | env:
7 | - BUILD_TYPE=Debug COVERAGE=On
8 | - BUILD_TYPE=Release COVERAGE=Off
9 | install:
10 | - if [ "$CXX" = "g++" ]; then export CXX="g++-4.7" CC="gcc-4.7"; fi
11 | script:
12 | - mkdir build
13 | - cd build
14 | - cmake -DCMAKE_BUILD_TYPE=${BUILD_TYPE} -DCoverage=${COVERAGE} ..
15 | - cmake --build . -- -j2
16 | - ctest -D ExperimentalBuild -j2
17 | - ctest -D ExperimentalTest -j2
18 | - if [ "${COVERAGE}" = "On" ]; then bash <(curl -s https://codecov.io/bash); fi
19 | #- ctest -D ExperimentalMemCheck
20 | addons:
21 | apt:
22 | sources:
23 | - ubuntu-toolchain-r-test
24 | - kalakris-cmake
25 | packages:
26 | - gcc-4.7
27 | - g++-4.7
28 | - valgrind
29 | - cmake
30 |
--------------------------------------------------------------------------------
/AUTHORS:
--------------------------------------------------------------------------------
1 | Matt Godbolt
2 | Andrew Clarke
3 |
--------------------------------------------------------------------------------
/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | cmake_minimum_required(VERSION 2.8.4)
2 | project(zindex)
3 |
4 | if (APPLE AND CMAKE_CXX_COMPILER_ID MATCHES "Clang")
5 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -stdlib=libc++ -Wall -Werror -Wextra")
6 | else ()
7 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -Wall -Werror -Wextra")
8 | endif ()
9 |
10 | if (APPLE AND CMAKE_C_COMPILER_ID MATCHES "Clang")
11 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra")
12 | else ()
13 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread -Wall -Wextra")
14 | endif ()
15 |
16 | option(UseLTO "Use link-time optimization" OFF)
17 | option(Static "Statically link" OFF)
18 | option(BuildSqlShell "Build the sqlite shell" OFF)
19 | option(ArchNative "Target the computer being built on (march=native)" OFF)
20 | option(PGO "Set PGO flags" "")
21 | option(Coverage "Enable coverage reporting" OFF)
22 |
23 | if (Coverage)
24 | add_compile_options(--coverage -O0)
25 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
26 | endif (Coverage)
27 |
28 | if (PGO)
29 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${PGO}")
30 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${PGO}")
31 | endif (PGO)
32 |
33 | if (UseLTO)
34 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto -Wno-maybe-uninitialized")
35 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -flto -Wno-maybe-uninitialized")
36 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-linker-plugin")
37 | endif (UseLTO)
38 |
39 | set(COMMON_LIBS "")
40 | if (Static)
41 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static -static-libgcc -static-libstdc++")
42 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DSQLITE_OMIT_LOAD_EXTENSION=1")
43 | set(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
44 | set(BUILD_SHARED_LIBRARIES OFF)
45 | set(CMAKE_EXE_LINK_DYNAMIC_C_FLAGS) # remove -Wl,-Bdynamic
46 | set(CMAKE_EXE_LINK_DYNAMIC_CXX_FLAGS)
47 | else (Static)
48 | set(COMMON_LIBS "dl")
49 | endif (Static)
50 |
51 | if (ArchNative)
52 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=native")
53 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -march=native")
54 | endif (ArchNative)
55 |
56 | include(CTest)
57 |
58 | find_package(ZLIB REQUIRED)
59 | include_directories(BEFORE SYSTEM ext/sqlite)
60 | include_directories(${ZLIB_INCLUDE_DIRS} src ext)
61 |
62 | set(SOURCE_FILES
63 | src/File.h
64 | src/Index.cpp
65 | src/Index.h
66 | src/IndexParser.cpp
67 | src/IndexParser.h
68 | src/LineFinder.cpp
69 | src/LineFinder.h
70 | src/LineSink.h
71 | src/Sqlite.cpp
72 | src/Sqlite.h
73 | src/SqliteError.h
74 | src/RegExp.h
75 | src/RegExp.cpp
76 | src/RegExpIndexer.cpp
77 | src/RegExpIndexer.h
78 | src/LineIndexer.h
79 | src/IndexSink.h
80 | src/Log.h
81 | ext/cJSON/cJSON.h
82 | ext/cJSON/cJSON.c
83 | src/ConsoleLog.h
84 | src/ConsoleLog.cpp
85 | src/StringView.cpp
86 | src/StringView.h
87 | src/PrettyBytes.h
88 | src/PrettyBytes.cpp
89 | src/RangeFetcher.cpp
90 | src/RangeFetcher.h
91 | src/FieldIndexer.cpp
92 | src/FieldIndexer.h
93 | src/ExternalIndexer.cpp
94 | src/ExternalIndexer.h
95 | src/Pipe.cpp
96 | src/Pipe.h
97 | ext/sqlite/sqlite3.c)
98 |
99 | set(TEST_FILES
100 | tests/catch.hpp
101 | tests/LineFinderTest.cpp
102 | tests/test_main.cpp
103 | tests/SqliteTest.cpp
104 | tests/RegExpTest.cpp
105 | tests/RegExpIndexerTest.cpp
106 | tests/TempDir.h
107 | tests/TempDir.cpp
108 | tests/PrettyBytesTest.cpp
109 | tests/IndexTest.cpp
110 | tests/RangeFetcherTest.cpp
111 | tests/FieldIndexerTest.cpp
112 | tests/ExternalIndexerTest.cpp
113 | tests/LogTest.cpp)
114 |
115 | add_library(libzindex ${SOURCE_FILES})
116 | set_target_properties(libzindex PROPERTIES OUTPUT_NAME zindex)
117 |
118 | add_executable(zindex src/zindex.cpp)
119 | target_link_libraries(zindex libzindex ${ZLIB_LIBRARIES} ${COMMON_LIBS})
120 | add_executable(zq src/zq.cpp)
121 | target_link_libraries(zq libzindex ${ZLIB_LIBRARIES} ${COMMON_LIBS})
122 |
123 | add_executable(unit-tests ${TEST_FILES})
124 | target_link_libraries(unit-tests libzindex ${ZLIB_LIBRARIES} ${COMMON_LIBS})
125 |
126 | if (BuildSqlShell)
127 | add_executable(sql-shell ext/sqlite/shell.c ext/sqlite/sqlite3.c)
128 | target_link_libraries(sql-shell ${COMMON_LIBS})
129 | endif (BuildSqlShell)
130 |
131 | find_program(MEMORYCHECK_COMMAND valgrind)
132 | enable_testing()
133 | add_test(NAME unit-tests
134 | COMMAND unit-tests)
135 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2012-2016, Matt Godbolt
2 | All rights reserved.
3 |
4 | Redistribution and use in source and binary forms, with or without
5 | modification, are permitted provided that the following conditions are met:
6 |
7 | * Redistributions of source code must retain the above copyright notice,
8 | this list of conditions and the following disclaimer.
9 | * Redistributions in binary form must reproduce the above copyright
10 | notice, this list of conditions and the following disclaimer in the
11 | documentation and/or other materials provided with the distribution.
12 |
13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23 | POSSIBILITY OF SUCH DAMAGE.
24 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | default: all
2 |
3 | BUILD_TYPE ?= Release
4 | BUILD_DIR := build/${BUILD_TYPE}
5 | ROOT_DIR := $(shell pwd)
6 |
7 | ${BUILD_DIR}/Makefile: CMakeLists.txt
8 | mkdir -p ${BUILD_DIR}
9 | cd ${BUILD_DIR} && cmake ${ROOT_DIR} -DCMAKE_BUILD_TYPE=${BUILD_TYPE}
10 |
11 | all: ${BUILD_DIR}/Makefile
12 | $(MAKE) -C ${BUILD_DIR} all
13 |
14 | test: ${BUILD_DIR}/Makefile all
15 | $(MAKE) -C ${BUILD_DIR} test
16 |
17 | clean:
18 | rm -rf build
19 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | [](https://travis-ci.org/mattgodbolt/zindex) [](https://codecov.io/gh/mattgodbolt/zindex)
2 |
3 |
4 | `zindex` creates and queries an index on a compressed, line-based text file in a
5 | time- and space-efficient way.
6 |
7 | ### The itch I had
8 |
9 | I have many multigigabyte text gzipped log files and I'd like to be able to find data in them by an index.
10 | There's a key on each line that a simple regex can pull out. However, to find a
11 | particular record requires `zgrep`, which takes ages as it has to seek through
12 | gigabytes of previous data to get to each record.
13 |
14 | Enter `zindex` which builds an index and also stores decompression checkpoints along the way
15 | which allows lightning fast random access. Pulling out single lines by either
16 | line number of by an index entry is then almost instant, even for huge files. The indices
17 | themselves are small too, typically ~10% of the compressed file size for a simple unique
18 | numeric index.
19 |
20 | ## Creating an index
21 |
22 | `zindex` needs to be told what part of each line constitutes the index. This can be done by
23 | a regular expression, by field, or by piping each line through an external program.
24 |
25 | By default zindex creates an index of `file.gz.zindex` when asked to index `file.gz`.
26 |
27 | Example: create an index on lines matching a numeric regular expression. The capture group
28 | indicates the part that's to be indexed, and the options show each line has a unique, numeric index.
29 |
30 | ```bash
31 | $ zindex file.gz --regex 'id:([0-9]+)' --numeric --unique
32 | ```
33 |
34 | Example: create an index on the second field of a CSV file:
35 |
36 | ```bash
37 | $ zindex file.gz --delimiter , --field 2
38 | ```
39 |
40 | Example: create an index on a JSON field `orderId.id` in any of the items in the document root's `actions` array (requires [jq](http://stedolan.github.io/jq/)).
41 | The `jq` query creates an array of all the `orderId.id`s, then `join`s them with a space to ensure each individual line piped to jq creates a single line of output,
42 | with multiple matches separated by spaces (which is the default separator).
43 |
44 | ```bash
45 | $ zindex file.gz --pipe "jq --raw-output --unbuffered '[.actions[].orderId.id] | join(\" \")'"
46 | ```
47 |
48 | Multiple indices, and configuration of the index creation by JSON configuration file are supported, see below.
49 |
50 | ## Querying the index
51 |
52 | The `zq` program is used to query an index. It's given the name of the compressed file and a list of queries. For example:
53 |
54 | ```bash
55 | $ zq file.gz 1023 4443 554
56 | ```
57 |
58 | It's also possible to output by line number, so to print lines 1 and 1000 from a file:
59 |
60 | ```bash
61 | $ zq file.gz --line 1 1000
62 | ```
63 |
64 | ## Building from source
65 |
66 | `zindex` uses CMake for its basic building (though has a bootstrapping `Makefile`), and requires a C++11 compatible compiler (GCC 4.8 or above and clang 3.4 and above). It also requires `zlib`. With the relevant compiler available, building ought to be as simple as:
67 |
68 | ```bash
69 | $ git clone https://github.com/mattgodbolt/zindex.git
70 | $ cd zindex
71 | $ make
72 | ```
73 |
74 | Binaries are left in `build/Release`.
75 |
76 | Additionally a static binary can be built if you're happy to dip your toe into CMake:
77 |
78 | ```bash
79 | $ cd path/to/build/directory
80 | $ cmake path/to/zindex/checkout/dir -DStatic:BOOL=On -DCMAKE_BUILD_TYPE=Release
81 | $ make
82 | ```
83 |
84 | ## Multiple indices
85 |
86 | To support more than one index, or for easier configuration than all the command-line flags that might be
87 | needed, there is a JSON configuration format. Pass the `--config .json` option and put something like this in the configuration file:
88 |
89 | {
90 | "indexes": [
91 | {
92 | "type": "field",
93 | "delimiter": "\t",
94 | "fieldNum": 1
95 | },
96 | {
97 | "name": "secondary",
98 | "type": "field",
99 | "delimiter": "\t",
100 | "fieldNum": 2
101 | }
102 | ]
103 | }
104 |
105 | This creates two indices, one on the first field and one on the second field, as delimited by tabs. One can
106 | then specify which index to query with the `-i ` option of `zq`.
107 |
108 | ### Issues and feature requests
109 |
110 | See the [issue tracker](https://github.com/mattgodbolt/zindex/issues) for TODOs and known bugs. Please raise bugs there, and feel free to submit suggestions there also.
111 |
112 | Feel free to [contact me](mailto:matt@godbolt.org) if you prefer email over bug trackers.
113 |
--------------------------------------------------------------------------------
/ext/cJSON/cJSON.h:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright (c) 2009 Dave Gamble
3 |
4 | Permission is hereby granted, free of charge, to any person obtaining a copy
5 | of this software and associated documentation files (the "Software"), to deal
6 | in the Software without restriction, including without limitation the rights
7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 | copies of the Software, and to permit persons to whom the Software is
9 | furnished to do so, subject to the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be included in
12 | all copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 | THE SOFTWARE.
21 | */
22 |
23 | #ifndef cJSON__h
24 | #define cJSON__h
25 |
26 | #ifdef __cplusplus
27 | extern "C"
28 | {
29 | #endif
30 |
31 | /* cJSON Types: */
32 | #define cJSON_False (1 << 0)
33 | #define cJSON_True (1 << 1)
34 | #define cJSON_NULL (1 << 2)
35 | #define cJSON_Number (1 << 3)
36 | #define cJSON_String (1 << 4)
37 | #define cJSON_Array (1 << 5)
38 | #define cJSON_Object (1 << 6)
39 |
40 | #define cJSON_IsReference 256
41 | #define cJSON_StringIsConst 512
42 |
43 | /* The cJSON structure: */
44 | typedef struct cJSON {
45 | struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
46 | struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
47 |
48 | int type; /* The type of the item, as above. */
49 |
50 | char *valuestring; /* The item's string, if type==cJSON_String */
51 | int valueint; /* The item's number, if type==cJSON_Number */
52 | double valuedouble; /* The item's number, if type==cJSON_Number */
53 |
54 | char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
55 | } cJSON;
56 |
57 | typedef struct cJSON_Hooks {
58 | void *(*malloc_fn)(size_t sz);
59 | void (*free_fn)(void *ptr);
60 | } cJSON_Hooks;
61 |
62 | /* Supply malloc, realloc and free functions to cJSON */
63 | extern void cJSON_InitHooks(cJSON_Hooks* hooks);
64 |
65 |
66 | /* Supply a block of JSON, and this returns a cJSON object you can interrogate. Call cJSON_Delete when finished. */
67 | extern cJSON *cJSON_Parse(const char *value);
68 | /* Render a cJSON entity to text for transfer/storage. Free the char* when finished. */
69 | extern char *cJSON_Print(cJSON *item);
70 | /* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */
71 | extern char *cJSON_PrintUnformatted(cJSON *item);
72 | /* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */
73 | extern char *cJSON_PrintBuffered(cJSON *item,int prebuffer,int fmt);
74 | /* Delete a cJSON entity and all subentities. */
75 | extern void cJSON_Delete(cJSON *c);
76 |
77 | /* Returns the number of items in an array (or object). */
78 | extern int cJSON_GetArraySize(cJSON *array);
79 | /* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
80 | extern cJSON *cJSON_GetArrayItem(cJSON *array,int item);
81 | /* Get item "string" from object. Case insensitive. */
82 | extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string);
83 | extern int cJSON_HasObjectItem(cJSON *object,const char *string);
84 | /* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
85 | extern const char *cJSON_GetErrorPtr(void);
86 |
87 | /* These calls create a cJSON item of the appropriate type. */
88 | extern cJSON *cJSON_CreateNull(void);
89 | extern cJSON *cJSON_CreateTrue(void);
90 | extern cJSON *cJSON_CreateFalse(void);
91 | extern cJSON *cJSON_CreateBool(int b);
92 | extern cJSON *cJSON_CreateNumber(double num);
93 | extern cJSON *cJSON_CreateString(const char *string);
94 | extern cJSON *cJSON_CreateArray(void);
95 | extern cJSON *cJSON_CreateObject(void);
96 |
97 | /* These utilities create an Array of count items. */
98 | extern cJSON *cJSON_CreateIntArray(const int *numbers,int count);
99 | extern cJSON *cJSON_CreateFloatArray(const float *numbers,int count);
100 | extern cJSON *cJSON_CreateDoubleArray(const double *numbers,int count);
101 | extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
102 |
103 | /* Append item to the specified array/object. */
104 | extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
105 | extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item);
106 | extern void cJSON_AddItemToObjectCS(cJSON *object,const char *string,cJSON *item); /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object */
107 | /* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
108 | extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
109 | extern void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item);
110 |
111 | /* Remove/Detatch items from Arrays/Objects. */
112 | extern cJSON *cJSON_DetachItemFromArray(cJSON *array,int which);
113 | extern void cJSON_DeleteItemFromArray(cJSON *array,int which);
114 | extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string);
115 | extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string);
116 |
117 | /* Update array items. */
118 | extern void cJSON_InsertItemInArray(cJSON *array,int which,cJSON *newitem); /* Shifts pre-existing items to the right. */
119 | extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem);
120 | extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
121 |
122 | /* Duplicate a cJSON item */
123 | extern cJSON *cJSON_Duplicate(cJSON *item,int recurse);
124 | /* Duplicate will create a new, identical cJSON item to the one you pass, in new memory that will
125 | need to be released. With recurse!=0, it will duplicate any children connected to the item.
126 | The item->next and ->prev pointers are always zero on return from Duplicate. */
127 |
128 | /* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed. */
129 | /* If you supply a ptr in return_parse_end and parsing fails, then return_parse_end will contain a pointer to the error. If not, then cJSON_GetErrorPtr() does the job. */
130 | extern cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated);
131 |
132 | extern void cJSON_Minify(char *json);
133 |
134 | /* Macros for creating things quickly. */
135 | #define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
136 | #define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
137 | #define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
138 | #define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b))
139 | #define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n))
140 | #define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s))
141 |
142 | /* When assigning an integer value, it needs to be propagated to valuedouble too. */
143 | #define cJSON_SetIntValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val))
144 | #define cJSON_SetNumberValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val))
145 |
146 | /* Macro for iterating over an array */
147 | #define cJSON_ArrayForEach(pos, head) for(pos = (head)->child; pos != NULL; pos = pos->next)
148 |
149 | #ifdef __cplusplus
150 | }
151 | #endif
152 |
153 | #endif
154 |
--------------------------------------------------------------------------------
/ext/tclap/ArgException.h:
--------------------------------------------------------------------------------
1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2 |
3 | /******************************************************************************
4 | *
5 | * file: ArgException.h
6 | *
7 | * Copyright (c) 2003, Michael E. Smoot .
8 | * All rights reverved.
9 | *
10 | * See the file COPYING in the top directory of this distribution for
11 | * more information.
12 | *
13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 | * DEALINGS IN THE SOFTWARE.
20 | *
21 | *****************************************************************************/
22 |
23 |
24 | #ifndef TCLAP_ARG_EXCEPTION_H
25 | #define TCLAP_ARG_EXCEPTION_H
26 |
27 | #include
28 | #include
29 |
30 | namespace TCLAP {
31 |
32 | /**
33 | * A simple class that defines and argument exception. Should be caught
34 | * whenever a CmdLine is created and parsed.
35 | */
36 | class ArgException : public std::exception
37 | {
38 | public:
39 |
40 | /**
41 | * Constructor.
42 | * \param text - The text of the exception.
43 | * \param id - The text identifying the argument source.
44 | * \param td - Text describing the type of ArgException it is.
45 | * of the exception.
46 | */
47 | ArgException( const std::string& text = "undefined exception",
48 | const std::string& id = "undefined",
49 | const std::string& td = "Generic ArgException")
50 | : std::exception(),
51 | _errorText(text),
52 | _argId( id ),
53 | _typeDescription(td)
54 | { }
55 |
56 | /**
57 | * Destructor.
58 | */
59 | virtual ~ArgException() throw() { }
60 |
61 | /**
62 | * Returns the error text.
63 | */
64 | std::string error() const { return ( _errorText ); }
65 |
66 | /**
67 | * Returns the argument id.
68 | */
69 | std::string argId() const
70 | {
71 | if ( _argId == "undefined" )
72 | return " ";
73 | else
74 | return ( "Argument: " + _argId );
75 | }
76 |
77 | /**
78 | * Returns the arg id and error text.
79 | */
80 | const char* what() const throw()
81 | {
82 | static std::string ex;
83 | ex = _argId + " -- " + _errorText;
84 | return ex.c_str();
85 | }
86 |
87 | /**
88 | * Returns the type of the exception. Used to explain and distinguish
89 | * between different child exceptions.
90 | */
91 | std::string typeDescription() const
92 | {
93 | return _typeDescription;
94 | }
95 |
96 |
97 | private:
98 |
99 | /**
100 | * The text of the exception message.
101 | */
102 | std::string _errorText;
103 |
104 | /**
105 | * The argument related to this exception.
106 | */
107 | std::string _argId;
108 |
109 | /**
110 | * Describes the type of the exception. Used to distinguish
111 | * between different child exceptions.
112 | */
113 | std::string _typeDescription;
114 |
115 | };
116 |
117 | /**
118 | * Thrown from within the child Arg classes when it fails to properly
119 | * parse the argument it has been passed.
120 | */
121 | class ArgParseException : public ArgException
122 | {
123 | public:
124 | /**
125 | * Constructor.
126 | * \param text - The text of the exception.
127 | * \param id - The text identifying the argument source
128 | * of the exception.
129 | */
130 | ArgParseException( const std::string& text = "undefined exception",
131 | const std::string& id = "undefined" )
132 | : ArgException( text,
133 | id,
134 | std::string( "Exception found while parsing " ) +
135 | std::string( "the value the Arg has been passed." ))
136 | { }
137 | };
138 |
139 | /**
140 | * Thrown from CmdLine when the arguments on the command line are not
141 | * properly specified, e.g. too many arguments, required argument missing, etc.
142 | */
143 | class CmdLineParseException : public ArgException
144 | {
145 | public:
146 | /**
147 | * Constructor.
148 | * \param text - The text of the exception.
149 | * \param id - The text identifying the argument source
150 | * of the exception.
151 | */
152 | CmdLineParseException( const std::string& text = "undefined exception",
153 | const std::string& id = "undefined" )
154 | : ArgException( text,
155 | id,
156 | std::string( "Exception found when the values ") +
157 | std::string( "on the command line do not meet ") +
158 | std::string( "the requirements of the defined ") +
159 | std::string( "Args." ))
160 | { }
161 | };
162 |
163 | /**
164 | * Thrown from Arg and CmdLine when an Arg is improperly specified, e.g.
165 | * same flag as another Arg, same name, etc.
166 | */
167 | class SpecificationException : public ArgException
168 | {
169 | public:
170 | /**
171 | * Constructor.
172 | * \param text - The text of the exception.
173 | * \param id - The text identifying the argument source
174 | * of the exception.
175 | */
176 | SpecificationException( const std::string& text = "undefined exception",
177 | const std::string& id = "undefined" )
178 | : ArgException( text,
179 | id,
180 | std::string("Exception found when an Arg object ")+
181 | std::string("is improperly defined by the ") +
182 | std::string("developer." ))
183 | { }
184 |
185 | };
186 |
187 | class ExitException {
188 | public:
189 | ExitException(int estat) : _estat(estat) {}
190 |
191 | int getExitStatus() const { return _estat; }
192 |
193 | private:
194 | int _estat;
195 | };
196 |
197 | } // namespace TCLAP
198 |
199 | #endif
200 |
201 |
--------------------------------------------------------------------------------
/ext/tclap/ArgTraits.h:
--------------------------------------------------------------------------------
1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2 |
3 | /******************************************************************************
4 | *
5 | * file: ArgTraits.h
6 | *
7 | * Copyright (c) 2007, Daniel Aarno, Michael E. Smoot .
8 | * All rights reverved.
9 | *
10 | * See the file COPYING in the top directory of this distribution for
11 | * more information.
12 | *
13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 | * DEALINGS IN THE SOFTWARE.
20 | *
21 | *****************************************************************************/
22 |
23 | // This is an internal tclap file, you should probably not have to
24 | // include this directly
25 |
26 | #ifndef TCLAP_ARGTRAITS_H
27 | #define TCLAP_ARGTRAITS_H
28 |
29 | namespace TCLAP {
30 |
31 | // We use two empty structs to get compile type specialization
32 | // function to work
33 |
34 | /**
35 | * A value like argument value type is a value that can be set using
36 | * operator>>. This is the default value type.
37 | */
38 | struct ValueLike {
39 | typedef ValueLike ValueCategory;
40 | };
41 |
42 | /**
43 | * A string like argument value type is a value that can be set using
44 | * operator=(string). Usefull if the value type contains spaces which
45 | * will be broken up into individual tokens by operator>>.
46 | */
47 | struct StringLike {};
48 |
49 | /**
50 | * A class can inherit from this object to make it have string like
51 | * traits. This is a compile time thing and does not add any overhead
52 | * to the inherenting class.
53 | */
54 | struct StringLikeTrait {
55 | typedef StringLike ValueCategory;
56 | };
57 |
58 | /**
59 | * A class can inherit from this object to make it have value like
60 | * traits. This is a compile time thing and does not add any overhead
61 | * to the inherenting class.
62 | */
63 | struct ValueLikeTrait {
64 | typedef ValueLike ValueCategory;
65 | };
66 |
67 | /**
68 | * Arg traits are used to get compile type specialization when parsing
69 | * argument values. Using an ArgTraits you can specify the way that
70 | * values gets assigned to any particular type during parsing. The two
71 | * supported types are string like and value like.
72 | */
73 | template
74 | struct ArgTraits {
75 | typedef typename T::ValueCategory ValueCategory;
76 | //typedef ValueLike ValueCategory;
77 | };
78 |
79 | #endif
80 |
81 | } // namespace
82 |
--------------------------------------------------------------------------------
/ext/tclap/CmdLineInterface.h:
--------------------------------------------------------------------------------
1 |
2 | /******************************************************************************
3 | *
4 | * file: CmdLineInterface.h
5 | *
6 | * Copyright (c) 2003, Michael E. Smoot .
7 | * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
8 | * All rights reverved.
9 | *
10 | * See the file COPYING in the top directory of this distribution for
11 | * more information.
12 | *
13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 | * DEALINGS IN THE SOFTWARE.
20 | *
21 | *****************************************************************************/
22 |
23 | #ifndef TCLAP_COMMANDLINE_INTERFACE_H
24 | #define TCLAP_COMMANDLINE_INTERFACE_H
25 |
26 | #include
27 | #include
28 | #include
29 | #include
30 | #include
31 |
32 |
33 | namespace TCLAP {
34 |
35 | class Arg;
36 | class CmdLineOutput;
37 | class XorHandler;
38 |
39 | /**
40 | * The base class that manages the command line definition and passes
41 | * along the parsing to the appropriate Arg classes.
42 | */
43 | class CmdLineInterface
44 | {
45 | public:
46 |
47 | /**
48 | * Destructor
49 | */
50 | virtual ~CmdLineInterface() {}
51 |
52 | /**
53 | * Adds an argument to the list of arguments to be parsed.
54 | * \param a - Argument to be added.
55 | */
56 | virtual void add( Arg& a )=0;
57 |
58 | /**
59 | * An alternative add. Functionally identical.
60 | * \param a - Argument to be added.
61 | */
62 | virtual void add( Arg* a )=0;
63 |
64 | /**
65 | * Add two Args that will be xor'd.
66 | * If this method is used, add does
67 | * not need to be called.
68 | * \param a - Argument to be added and xor'd.
69 | * \param b - Argument to be added and xor'd.
70 | */
71 | virtual void xorAdd( Arg& a, Arg& b )=0;
72 |
73 | /**
74 | * Add a list of Args that will be xor'd. If this method is used,
75 | * add does not need to be called.
76 | * \param xors - List of Args to be added and xor'd.
77 | */
78 | virtual void xorAdd( std::vector& xors )=0;
79 |
80 | /**
81 | * Parses the command line.
82 | * \param argc - Number of arguments.
83 | * \param argv - Array of arguments.
84 | */
85 | virtual void parse(int argc, const char * const * argv)=0;
86 |
87 | /**
88 | * Parses the command line.
89 | * \param args - A vector of strings representing the args.
90 | * args[0] is still the program name.
91 | */
92 | void parse(std::vector& args);
93 |
94 | /**
95 | * Returns the CmdLineOutput object.
96 | */
97 | virtual CmdLineOutput* getOutput()=0;
98 |
99 | /**
100 | * \param co - CmdLineOutput object that we want to use instead.
101 | */
102 | virtual void setOutput(CmdLineOutput* co)=0;
103 |
104 | /**
105 | * Returns the version string.
106 | */
107 | virtual std::string& getVersion()=0;
108 |
109 | /**
110 | * Returns the program name string.
111 | */
112 | virtual std::string& getProgramName()=0;
113 |
114 | /**
115 | * Returns the argList.
116 | */
117 | virtual std::list& getArgList()=0;
118 |
119 | /**
120 | * Returns the XorHandler.
121 | */
122 | virtual XorHandler& getXorHandler()=0;
123 |
124 | /**
125 | * Returns the delimiter string.
126 | */
127 | virtual char getDelimiter()=0;
128 |
129 | /**
130 | * Returns the message string.
131 | */
132 | virtual std::string& getMessage()=0;
133 |
134 | /**
135 | * Indicates whether or not the help and version switches were created
136 | * automatically.
137 | */
138 | virtual bool hasHelpAndVersion()=0;
139 |
140 | /**
141 | * Resets the instance as if it had just been constructed so that the
142 | * instance can be reused.
143 | */
144 | virtual void reset()=0;
145 | };
146 |
147 | } //namespace
148 |
149 |
150 | #endif
151 |
--------------------------------------------------------------------------------
/ext/tclap/CmdLineOutput.h:
--------------------------------------------------------------------------------
1 |
2 |
3 | /******************************************************************************
4 | *
5 | * file: CmdLineOutput.h
6 | *
7 | * Copyright (c) 2004, Michael E. Smoot
8 | * All rights reverved.
9 | *
10 | * See the file COPYING in the top directory of this distribution for
11 | * more information.
12 | *
13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 | * DEALINGS IN THE SOFTWARE.
20 | *
21 | *****************************************************************************/
22 |
23 | #ifndef TCLAP_CMDLINEOUTPUT_H
24 | #define TCLAP_CMDLINEOUTPUT_H
25 |
26 | #include
27 | #include
28 | #include
29 | #include
30 | #include
31 | #include
32 |
33 | namespace TCLAP {
34 |
35 | class CmdLineInterface;
36 | class ArgException;
37 |
38 | /**
39 | * The interface that any output object must implement.
40 | */
41 | class CmdLineOutput
42 | {
43 |
44 | public:
45 |
46 | /**
47 | * Virtual destructor.
48 | */
49 | virtual ~CmdLineOutput() {}
50 |
51 | /**
52 | * Generates some sort of output for the USAGE.
53 | * \param c - The CmdLine object the output is generated for.
54 | */
55 | virtual void usage(CmdLineInterface& c)=0;
56 |
57 | /**
58 | * Generates some sort of output for the version.
59 | * \param c - The CmdLine object the output is generated for.
60 | */
61 | virtual void version(CmdLineInterface& c)=0;
62 |
63 | /**
64 | * Generates some sort of output for a failure.
65 | * \param c - The CmdLine object the output is generated for.
66 | * \param e - The ArgException that caused the failure.
67 | */
68 | virtual void failure( CmdLineInterface& c,
69 | ArgException& e )=0;
70 |
71 | };
72 |
73 | } //namespace TCLAP
74 | #endif
75 |
--------------------------------------------------------------------------------
/ext/tclap/Constraint.h:
--------------------------------------------------------------------------------
1 |
2 | /******************************************************************************
3 | *
4 | * file: Constraint.h
5 | *
6 | * Copyright (c) 2005, Michael E. Smoot
7 | * All rights reverved.
8 | *
9 | * See the file COPYING in the top directory of this distribution for
10 | * more information.
11 | *
12 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
13 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
15 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18 | * DEALINGS IN THE SOFTWARE.
19 | *
20 | *****************************************************************************/
21 |
22 | #ifndef TCLAP_CONSTRAINT_H
23 | #define TCLAP_CONSTRAINT_H
24 |
25 | #include
26 | #include
27 | #include
28 | #include
29 | #include
30 | #include
31 |
32 | namespace TCLAP {
33 |
34 | /**
35 | * The interface that defines the interaction between the Arg and Constraint.
36 | */
37 | template
38 | class Constraint
39 | {
40 |
41 | public:
42 | /**
43 | * Returns a description of the Constraint.
44 | */
45 | virtual std::string description() const =0;
46 |
47 | /**
48 | * Returns the short ID for the Constraint.
49 | */
50 | virtual std::string shortID() const =0;
51 |
52 | /**
53 | * The method used to verify that the value parsed from the command
54 | * line meets the constraint.
55 | * \param value - The value that will be checked.
56 | */
57 | virtual bool check(const T& value) const =0;
58 |
59 | /**
60 | * Destructor.
61 | * Silences warnings about Constraint being a base class with virtual
62 | * functions but without a virtual destructor.
63 | */
64 | virtual ~Constraint() { ; }
65 | };
66 |
67 | } //namespace TCLAP
68 | #endif
69 |
--------------------------------------------------------------------------------
/ext/tclap/DocBookOutput.h:
--------------------------------------------------------------------------------
1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*-
2 |
3 | /******************************************************************************
4 | *
5 | * file: DocBookOutput.h
6 | *
7 | * Copyright (c) 2004, Michael E. Smoot
8 | * All rights reverved.
9 | *
10 | * See the file COPYING in the top directory of this distribution for
11 | * more information.
12 | *
13 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19 | * DEALINGS IN THE SOFTWARE.
20 | *
21 | *****************************************************************************/
22 |
23 | #ifndef TCLAP_DOCBOOKOUTPUT_H
24 | #define TCLAP_DOCBOOKOUTPUT_H
25 |
26 | #include
27 | #include
28 | #include
29 | #include
30 | #include
31 |
32 | #include
33 | #include
34 | #include
35 | #include
36 |
37 | namespace TCLAP {
38 |
39 | /**
40 | * A class that generates DocBook output for usage() method for the
41 | * given CmdLine and its Args.
42 | */
43 | class DocBookOutput : public CmdLineOutput
44 | {
45 |
46 | public:
47 |
48 | /**
49 | * Prints the usage to stdout. Can be overridden to
50 | * produce alternative behavior.
51 | * \param c - The CmdLine object the output is generated for.
52 | */
53 | virtual void usage(CmdLineInterface& c);
54 |
55 | /**
56 | * Prints the version to stdout. Can be overridden
57 | * to produce alternative behavior.
58 | * \param c - The CmdLine object the output is generated for.
59 | */
60 | virtual void version(CmdLineInterface& c);
61 |
62 | /**
63 | * Prints (to stderr) an error message, short usage
64 | * Can be overridden to produce alternative behavior.
65 | * \param c - The CmdLine object the output is generated for.
66 | * \param e - The ArgException that caused the failure.
67 | */
68 | virtual void failure(CmdLineInterface& c,
69 | ArgException& e );
70 |
71 | protected:
72 |
73 | /**
74 | * Substitutes the char r for string x in string s.
75 | * \param s - The string to operate on.
76 | * \param r - The char to replace.
77 | * \param x - What to replace r with.
78 | */
79 | void substituteSpecialChars( std::string& s, char r, std::string& x );
80 | void removeChar( std::string& s, char r);
81 | void basename( std::string& s );
82 |
83 | void printShortArg(Arg* it);
84 | void printLongArg(Arg* it);
85 |
86 | char theDelimiter;
87 | };
88 |
89 |
90 | inline void DocBookOutput::version(CmdLineInterface& _cmd)
91 | {
92 | std::cout << _cmd.getVersion() << std::endl;
93 | }
94 |
95 | inline void DocBookOutput::usage(CmdLineInterface& _cmd )
96 | {
97 | std::list argList = _cmd.getArgList();
98 | std::string progName = _cmd.getProgramName();
99 | std::string version = _cmd.getVersion();
100 | theDelimiter = _cmd.getDelimiter();
101 | XorHandler xorHandler = _cmd.getXorHandler();
102 | std::vector< std::vector > xorList = xorHandler.getXorList();
103 | basename(progName);
104 |
105 | std::cout << "" << std::endl;
106 | std::cout << "" << std::endl << std::endl;
108 |
109 | std::cout << "" << std::endl;
110 |
111 | std::cout << "" << std::endl;
112 | std::cout << "" << progName << "" << std::endl;
113 | std::cout << "1" << std::endl;
114 | std::cout << "" << std::endl;
115 |
116 | std::cout << "" << std::endl;
117 | std::cout << "" << progName << "" << std::endl;
118 | std::cout << "" << _cmd.getMessage() << "" << std::endl;
119 | std::cout << "" << std::endl;
120 |
121 | std::cout << "" << std::endl;
122 | std::cout << "" << std::endl;
123 |
124 | std::cout << "" << progName << "" << std::endl;
125 |
126 | // xor
127 | for ( int i = 0; (unsigned int)i < xorList.size(); i++ )
128 | {
129 | std::cout << "" << std::endl;
130 | for ( ArgVectorIterator it = xorList[i].begin();
131 | it != xorList[i].end(); it++ )
132 | printShortArg((*it));
133 |
134 | std::cout << "" << std::endl;
135 | }
136 |
137 | // rest of args
138 | for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
139 | if ( !xorHandler.contains( (*it) ) )
140 | printShortArg((*it));
141 |
142 | std::cout << "" << std::endl;
143 | std::cout << "" << std::endl;
144 |
145 | std::cout << "" << std::endl;
146 | std::cout << "Description" << std::endl;
147 | std::cout << "" << std::endl;
148 | std::cout << _cmd.getMessage() << std::endl;
149 | std::cout << "" << std::endl;
150 | std::cout << "" << std::endl;
151 |
152 | std::cout << "" << std::endl;
153 | std::cout << "Options" << std::endl;
154 |
155 | std::cout << "" << std::endl;
156 |
157 | for (ArgListIterator it = argList.begin(); it != argList.end(); it++)
158 | printLongArg((*it));
159 |
160 | std::cout << "" << std::endl;
161 | std::cout << "" << std::endl;
162 |
163 | std::cout << "" << std::endl;
164 | std::cout << "Version" << std::endl;
165 | std::cout << "" << std::endl;
166 | std::cout << version << std::endl;
167 | std::cout << "" << std::endl;
168 | std::cout << "" << std::endl;
169 |
170 | std::cout << "" << std::endl;
171 |
172 | }
173 |
174 | inline void DocBookOutput::failure( CmdLineInterface& _cmd,
175 | ArgException& e )
176 | {
177 | static_cast(_cmd); // unused
178 | std::cout << e.what() << std::endl;
179 | throw ExitException(1);
180 | }
181 |
182 | inline void DocBookOutput::substituteSpecialChars( std::string& s,
183 | char r,
184 | std::string& x )
185 | {
186 | size_t p;
187 | while ( (p = s.find_first_of(r)) != std::string::npos )
188 | {
189 | s.erase(p,1);
190 | s.insert(p,x);
191 | }
192 | }
193 |
194 | inline void DocBookOutput::removeChar( std::string& s, char r)
195 | {
196 | size_t p;
197 | while ( (p = s.find_first_of(r)) != std::string::npos )
198 | {
199 | s.erase(p,1);
200 | }
201 | }
202 |
203 | inline void DocBookOutput::basename( std::string& s )
204 | {
205 | size_t p = s.find_last_of('/');
206 | if ( p != std::string::npos )
207 | {
208 | s.erase(0, p + 1);
209 | }
210 | }
211 |
212 | inline void DocBookOutput::printShortArg(Arg* a)
213 | {
214 | std::string lt = "<";
215 | std::string gt = ">";
216 |
217 | std::string id = a->shortID();
218 | substituteSpecialChars(id,'<',lt);
219 | substituteSpecialChars(id,'>',gt);
220 | removeChar(id,'[');
221 | removeChar(id,']');
222 |
223 | std::string choice = "opt";
224 | if ( a->isRequired() )
225 | choice = "plain";
226 |
227 | std::cout << "acceptsMultipleValues() )
229 | std::cout << " rep='repeat'";
230 |
231 |
232 | std::cout << '>';
233 | if ( !a->getFlag().empty() )
234 | std::cout << a->flagStartChar() << a->getFlag();
235 | else
236 | std::cout << a->nameStartString() << a->getName();
237 | if ( a->isValueRequired() )
238 | {
239 | std::string arg = a->shortID();
240 | removeChar(arg,'[');
241 | removeChar(arg,']');
242 | removeChar(arg,'<');
243 | removeChar(arg,'>');
244 | arg.erase(0, arg.find_last_of(theDelimiter) + 1);
245 | std::cout << theDelimiter;
246 | std::cout << "" << arg << "";
247 | }
248 | std::cout << "" << std::endl;
249 |
250 | }
251 |
252 | inline void DocBookOutput::printLongArg(Arg* a)
253 | {
254 | std::string lt = "<";
255 | std::string gt = ">";
256 |
257 | std::string desc = a->getDescription();
258 | substituteSpecialChars(desc,'<',lt);
259 | substituteSpecialChars(desc,'>',gt);
260 |
261 | std::cout << "" << std::endl;
262 |
263 | if ( !a->getFlag().empty() )
264 | {
265 | std::cout << "" << std::endl;
266 | std::cout << "