├── .gitignore ├── .gitmodules ├── .travis.yml ├── CMakeLists.txt ├── License.md ├── Readme.md ├── TEXB_stream_format.txt ├── a.txt ├── include └── TEXB │ └── TEXBLib.h ├── src ├── CompilerName.h ├── Info.rc.in ├── Itsudemo.cpp ├── TEXB.h ├── TEXBFetch.cpp ├── TEXBLib.cpp ├── TEXBLoad.cpp ├── TEXBModify.cpp ├── TEXBPixel.cpp ├── TEXBPixel.h ├── TEXBSave.cpp ├── TEXBVersion.h.in ├── TIMG.cpp └── xy2uv.h └── tclap-1.2.1 └── tclap ├── Arg.h ├── ArgException.h ├── ArgTraits.h ├── CmdLine.h ├── CmdLineInterface.h ├── CmdLineOutput.h ├── Constraint.h ├── DocBookOutput.h ├── HelpVisitor.h ├── IgnoreRestVisitor.h ├── MultiArg.h ├── MultiSwitchArg.h ├── OptionalUnlabeledTracker.h ├── StandardTraits.h ├── StdOutput.h ├── SwitchArg.h ├── UnlabeledMultiArg.h ├── UnlabeledValueArg.h ├── ValueArg.h ├── ValuesConstraint.h ├── VersionVisitor.h ├── Visitor.h ├── XorHandler.h └── ZshCompletionOutput.h /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | build* 3 | *.texb 4 | *.png.imag 5 | installdir* 6 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "zlib"] 2 | path = zlib 3 | url = https://github.com/madler/zlib 4 | [submodule "lodepng"] 5 | path = lodepng 6 | url = https://github.com/lvandeve/lodepng 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: cpp 2 | dist: xenial 3 | 4 | compiler: 5 | - gcc 6 | - clang 7 | 8 | script: 9 | - cmake -Bbuild -H. 10 | - cmake --build build --config Release 11 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.13) 2 | 3 | project(Itsudemo) 4 | 5 | set(LIBTEXB_VERSION "2.0.0") 6 | set(LIBTEXB_VERSION_MAJOR 2) 7 | set(LIBTEXB_VERSION_MINOR 0) 8 | set(LIBTEXB_VERSION_PATCH 0) 9 | 10 | # zlib 11 | find_package(ZLIB) 12 | if(ZLIB_FOUND) 13 | message(STATUS "using system zlib") 14 | set(ZLIB_LIBRARY_TARGET ZLIB::ZLIB) 15 | set(ZLIB_INCLUDE_DIRECTORIES "") 16 | elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/zlib") 17 | message(STATUS "using provided zlib") 18 | add_subdirectory(zlib "${CMAKE_CURRENT_BINARY_DIR}/zlib_build") 19 | set(ZLIB_LIBRARY_TARGET zlibstatic) 20 | set(ZLIB_INCLUDE_DIRECTORIES 21 | zlib 22 | "${CMAKE_CURRENT_BINARY_DIR}/zlib_build" 23 | ) 24 | else() 25 | message(FATAL_ERROR "cannot find zlib") 26 | endif() 27 | 28 | # LibTEXB 29 | configure_file(src/TEXBVersion.h.in "${CMAKE_CURRENT_BINARY_DIR}/include/TEXB/TEXBVersion.h") 30 | add_library(libtexb 31 | include/TEXB/TEXBLib.h 32 | src/TEXB.h 33 | src/TEXBFetch.cpp 34 | src/TEXBLib.cpp 35 | src/TEXBLoad.cpp 36 | src/TEXBModify.cpp 37 | src/TEXBPixel.cpp 38 | src/TEXBPixel.h 39 | src/TEXBSave.cpp 40 | src/TIMG.cpp 41 | src/xy2uv.h 42 | "${CMAKE_CURRENT_BINARY_DIR}/include/TEXB/TEXBVersion.h" 43 | ) 44 | target_include_directories(libtexb PUBLIC include "${CMAKE_CURRENT_BINARY_DIR}/include" ${ZLIB_INCLUDE_DIRECTORIES}) 45 | target_link_libraries(libtexb PRIVATE ${ZLIB_LIBRARY_TARGET}) 46 | target_compile_definitions(libtexb PRIVATE LIBTEXB_DURING_COMPILATION) 47 | set_target_properties(libtexb PROPERTIES 48 | CXX_STANDARD 11 49 | CXX_STANDARD_REQUIRED TRUE 50 | OUTPUT_NAME "TEXB" 51 | ) 52 | if(MSVC) 53 | target_compile_definitions(libtexb PRIVATE _CRT_SECURE_NO_WARNINGS) 54 | endif() 55 | 56 | install(TARGETS) 57 | 58 | # Itsudemo program 59 | set(ITSUDEMO_BUILD_PROGRAM_DEFAULT ON) 60 | get_directory_property(HAS_PARENT PARENT_DIRECTORY) 61 | if(HAS_PARENT) 62 | set(ITSUDEMO_BUILD_PROGRAM_DEFAULT OFF) 63 | endif() 64 | 65 | option(ITSUDEMO_BUILD_PROGRAM "Build Itsudemo front-end program?" ${ITSUDEMO_BUILD_PROGRAM_DEFAULT}) 66 | if(ITSUDEMO_BUILD_PROGRAM) 67 | set(ITSUDEMO_VERSION "1.1.0.0") 68 | set(ITSUDEMO_MAJOR 1) 69 | set(ITSUDEMO_MINOR 1) 70 | set(ITSUDEMO_REV 0) 71 | set(ITSUDEMO_PATCH 0) # deprecated 72 | 73 | # lodepng 74 | add_library(lodepng STATIC 75 | lodepng/lodepng.h 76 | lodepng/lodepng.cpp 77 | ) 78 | target_include_directories(lodepng PUBLIC lodepng) 79 | 80 | configure_file(src/Info.rc.in "${CMAKE_CURRENT_BINARY_DIR}/Info.rc") 81 | add_executable(Itsudemo 82 | src/Itsudemo.cpp 83 | "${CMAKE_CURRENT_BINARY_DIR}/Info.rc" 84 | ) 85 | target_link_libraries(Itsudemo PRIVATE lodepng libtexb) 86 | endif() 87 | -------------------------------------------------------------------------------- /License.md: -------------------------------------------------------------------------------- 1 | License 2 | ======= 3 | 4 | Itsudemo and libtexb license (this project) 5 | ---------- 6 | 7 | Copyright © 2040 Dark Energy Processor Corporation 8 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and 10 | associated documentation files (the "Software"), to deal in the Software without restriction, 11 | including without limitation the rights to use, copy, modify, merge, publish, distribute, 12 | sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 13 | furnished to do so, subject to the following conditions: 14 | 15 | The above copyright notice and this permission notice shall be included in all copies or substantial 16 | portions of the Software. 17 | 18 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT 19 | NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 20 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 21 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | 24 | zLib/libz license 25 | ---------- 26 | 27 | Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler 28 | 29 | This software is provided 'as-is', without any express or implied 30 | warranty. In no event will the authors be held liable for any damages 31 | arising from the use of this software. 32 | 33 | Permission is granted to anyone to use this software for any purpose, 34 | including commercial applications, and to alter it and redistribute it 35 | freely, subject to the following restrictions: 36 | 37 | 1. The origin of this software must not be misrepresented; you must not 38 | claim that you wrote the original software. If you use this software 39 | in a product, an acknowledgment in the product documentation would be 40 | appreciated but is not required. 41 | 42 | 2. Altered source versions must be plainly marked as such, and must not be 43 | misrepresented as being the original software. 44 | 45 | 3. This notice may not be removed or altered from any source distribution. 46 | 47 | Jean-loup Gailly Mark Adler 48 | jloup@gzip.org madler@alumni.caltech.edu 49 | 50 | 51 | The data format used by the zlib library is described by RFCs (Request for 52 | Comments) 1950 to 1952 in the files http://tools.ietf.org/html/rfc1950 53 | (zlib format), rfc1951 (deflate format) and rfc1952 (gzip format). 54 | 55 | Only zLib distributed. Other libraries that come with zlib, like minizip is not redistributed because it's unused. 56 | 57 | LodePNG license 58 | ---------- 59 | 60 | LodePNG version 20160118 61 | 62 | Copyright (c) 2005-2016 Lode Vandevenne 63 | 64 | This software is provided 'as-is', without any express or implied 65 | warranty. In no event will the authors be held liable for any damages 66 | arising from the use of this software. 67 | 68 | Permission is granted to anyone to use this software for any purpose, 69 | including commercial applications, and to alter it and redistribute it 70 | freely, subject to the following restrictions: 71 | 72 | 1. The origin of this software must not be misrepresented; you must not 73 | claim that you wrote the original software. If you use this software 74 | in a product, an acknowledgment in the product documentation would be 75 | appreciated but is not required. 76 | 77 | 2. Altered source versions must be plainly marked as such, and must not be 78 | misrepresented as being the original software. 79 | 80 | 3. This notice may not be removed or altered from any source 81 | distribution. 82 | 83 | TCLAP license 84 | ---------- 85 | 86 | Copyright (c) 2003 Michael E. Smoot 87 | 88 | Permission is hereby granted, free of charge, to any person 89 | obtaining a copy of this software and associated documentation 90 | files (the "Software"), to deal in the Software without restriction, 91 | including without limitation the rights to use, copy, modify, merge, 92 | publish, distribute, sublicense, and/or sell copies of the Software, 93 | and to permit persons to whom the Software is furnished to do so, 94 | subject to the following conditions: 95 | 96 | The above copyright notice and this permission notice shall be 97 | included in all copies or substantial portions of the Software. 98 | 99 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 100 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 101 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 102 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 103 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 104 | AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 105 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 106 | THE SOFTWARE. 107 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | Itsudemo 2 | ======== 3 | 4 | Rewrite of [The Holy Constituency of summertriangle's hatedelay](https://github.com/summertriangle-dev/hatedelay) so it compatible with older compilers and uses CRT instead of POSIX calls. 5 | 6 | Please see **License.md** for license of this project and the associated files. -------------------------------------------------------------------------------- /TEXB_stream_format.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------- 2 | TEXB File format 3 | 4 | TEXB file format describe a texture. 5 | 6 | Texture can embbed multiple "images" which are not only rectangular but 7 | is defined as a triangle soup. 8 | 9 | 2011/12/06 V0.1 : First Draft 10 | 2011/12/09 V0.15: Added information about "Type" field, compressed texture is not complete still. 11 | 2012/01/12 V0.2 : Updated information about C String format for ressource name 12 | 2012/01/26 V0.3 : Added member width, height into image asset 13 | -------------------------------------------------------------------------- 14 | [TEXB](u32) 15 | Size (u32) 16 | String Length (u16) 17 | Length include 0 and padding alignment 18 | C String (0 included, + padding align 2) 19 | T + full resource path name 20 | example : Tpackage.package2.filename.texb 21 | 22 | Width (u16) 23 | Height (u16) 24 | Type (u16) 25 | Bit 0..2 : [0] ALPHA | [1] LUMA | [2] LUMALPHA | [3] RGB | [4] RGBA 26 | Bit 3 : [0] False, [1] True --> Compressed 27 | Bit 4 : [0] False, [1] True --> Mipmap 28 | Bit 5 : [0] False, [1] True --> Double Buffered 29 | Bit 6..7 : Pixel Format 30 | 0 : 565 RGB 31 | 1 : 5551 RGBA 32 | 2 : 4444 RGBA 33 | 3 : Byte 34 | 35 | TODO RP : Add Bit definition for compressed texture format type (ETC1 variation, etc...) 36 | Can reuse pixel format bits if necessary 37 | 38 | TotalVertexCount (u16) 39 | TotalIndexCount (u16) 40 | ImageCount (u16) 41 | 42 | (multiply by Image Count) 43 | [TIMG] (u32) 44 | Size (u16) 45 | String Length (u16) 46 | Length include 0 and padding alignment 47 | C String (0 included, + padding align 2) 48 | I + full resource path name 49 | Ipackage.package2.filenameB.imag 50 | VertexCount (u8) 51 | IndexCount (u8) 52 | Width (u16) 53 | Height (u16) 54 | CenterX (u16) 55 | CenterY (u16) 56 | 57 | (multiply by VertexCount) 58 | X (u32 fixed 16.16) 59 | Y (u32 fixed 16.16) 60 | U (u32 fixed 16.16) 61 | V (u32 fixed 16.16) 62 | 63 | (multiply by IndexCount) 64 | Index (u8) 65 | 66 | [... Texture Bitmap Data...] 67 | ------------------------------------------------------------------------ 68 | -------------------------------------------------------------------------------- /include/TEXB/TEXBLib.h: -------------------------------------------------------------------------------- 1 | #ifndef _TEXBLIB_H_ 2 | #define _TEXBLIB_H_ 3 | 4 | #include 5 | 6 | #include "TEXB/TEXBVersion.h" 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | #ifdef LIBTEXB_DURING_COMPILATION 13 | typedef struct TextureBank TEXB_TextureBank; 14 | typedef struct TextureImage TEXB_TextureImage; 15 | #else 16 | typedef struct TEXB_TextureBank TEXB_TextureBank; 17 | typedef struct TEXB_TextureImage TEXB_TextureImage; 18 | #endif 19 | 20 | #ifndef TEXBLIB 21 | # define TEXBLIB 22 | #endif 23 | 24 | typedef enum 25 | { 26 | TEXB_FLAG_COMPRESSED = 8, 27 | TEXB_FLAG_MIPMAP = 16, 28 | TEXB_FLAG_DOUBLEBUFFERED = 32, 29 | } TEXB_FLAGS; 30 | 31 | typedef enum 32 | { 33 | TEXB_CHANNEL_KIND_ALPHA = 0, 34 | TEXB_CHANNEL_KIND_LUMINANCE = 1, 35 | TEXB_CHANNEL_KIND_LUMINANCE_ALPHA = 2, 36 | TEXB_CHANNEL_KIND_RGB = 3, 37 | TEXB_CHANNEL_KIND_RGBA = 4, 38 | TEXB_CHANNEL_KIND_MAX_ENUM 39 | } TEXB_CHANNEL_KIND; 40 | #define TEXB_GET_CHANNEL_KIND(flags) ((TEXB_CHANNEL_KIND) (flags & 7)) 41 | 42 | typedef enum 43 | { 44 | TEXB_PIXEL_FORMAT_RGB565 = 0, 45 | TEXB_PIXEL_FORMAT_RGBA5551 = 1, 46 | TEXB_PIXEL_FORMAT_RGBA4444 = 2, 47 | TEXB_PIXEL_FORMAT_BYTE = 3 48 | } TEXB_PIXEL_FORMAT; 49 | #define TEXB_GET_PIXEL_FORMAT(flags) ((TEXB_PIXEL_FORMAT) ((flags >> 6) & 3)) 50 | 51 | /** 52 | * @brief Retrieve the libTEXB runtime version. This may be newer than `LIBTEXB_VERSION_NUM`. 53 | * @return libTEXB version runtime version. 54 | * @note This is runtime version, `LIBTEXB_VERSION_NUM` is compile-time version. 55 | **/ 56 | TEXBLIB size_t TEXB_version(); 57 | 58 | /** 59 | * @brief Get readable string of the current libTEXB version. 60 | * @return String of the current libTEXB version. 61 | **/ 62 | TEXBLIB const char *TEXB_version_string(); 63 | 64 | /** 65 | * @brief Get last error message of TEXB_* function calls. 66 | * 67 | * Note that: 68 | * * Successful TEXB_* calls does NOT change the return value of this function. 69 | * * The returned pointer may be invalidated on next failure calls on TEXB_* functions. 70 | * * If you want to store the error message longer, you need to copy it yourself. 71 | * * Error message is thread-local. 72 | * @return Reason why last TEXB_* call fails. This function never return NULL. 73 | **/ 74 | TEXBLIB const char *TEXB_get_last_error(); 75 | 76 | /** 77 | * @brief Create new texture bank atlas with the specified width and height. 78 | * @param width Atlas width. 79 | * @param height Atlas height. 80 | * @return New texture bank, or NULL on failure. 81 | **/ 82 | TEXBLIB TEXB_TextureBank *TEXB_create(uint32_t width, uint32_t height); 83 | 84 | /** 85 | * @brief Open existing texture bank atlas. 86 | * @param path Path to texb file. 87 | * @return New texture bank, or NULL on failure. 88 | **/ 89 | TEXBLIB TEXB_TextureBank *TEXB_open_from_file(const char *path); 90 | 91 | /** 92 | * @brief Open existing texture bank using custom reader callback and userdata. 93 | * @param handle Userdata 94 | * @param reader Function with signature similar to fread. 95 | * @return New texture bank, or NULL on failure. 96 | **/ 97 | TEXBLIB TEXB_TextureBank *TEXB_open_from_callback(void *handle, size_t(*reader)(void*, size_t, size_t, void*)); 98 | 99 | /** 100 | * @brief Open existing texture bank in-memory. 101 | * @param data Pointer to texture bank file data. 102 | * @param size Length of the texture bank file data. 103 | * @return New texture bank, or NULL on failure. 104 | **/ 105 | TEXBLIB TEXB_TextureBank *TEXB_open_from_memory(const void *data, size_t size); 106 | 107 | /** 108 | * @brief Duplicate current texture bank. 109 | * @param texb Existing texture bank. 110 | * @return New texture bank, or NULL on failure. 111 | **/ 112 | TEXBLIB TEXB_TextureBank *TEXB_clone(TEXB_TextureBank *texb); 113 | 114 | /** 115 | * @brief Free a texture bank and the associated texture image memory. 116 | * @param t1 Pointer to texture bank. Can be NULL. 117 | * @param t2 Pointer to pointer to texture bank. Can be NULL. Set the pointer value to NULL. 118 | **/ 119 | TEXBLIB void TEXB_free(TEXB_TextureBank *t1, TEXB_TextureBank **t2); 120 | 121 | /** 122 | * @brief Get texture bank name as defined in the file. 123 | * @param texb Existing texture bank. 124 | * @return Name of the texture bank, or empty string. 125 | * @note Don't assume the lifetime of the pointer. Copy it if necessary! 126 | **/ 127 | TEXBLIB const char *TEXB_get_name(TEXB_TextureBank *texb); 128 | 129 | /** 130 | * @brief Set the texture bank name. 131 | * @param texb Existing texture bank. 132 | * @param newname New name for this texture bank. 133 | * @return 1 on success, 0 on failure. 134 | **/ 135 | TEXBLIB int TEXB_set_name(TEXB_TextureBank *texb, const char *newname); 136 | 137 | /** 138 | * @brief Get texture atlas width. 139 | * @param texb Existing texture bank. 140 | * @return Texture atlas width 141 | **/ 142 | TEXBLIB uint32_t TEXB_get_atlas_width(TEXB_TextureBank *texb); 143 | 144 | /** 145 | * @brief Get texture atlas height. 146 | * @param texb Existing texture bank. 147 | * @return Texture atlas height 148 | **/ 149 | TEXBLIB uint32_t TEXB_get_atlas_height(TEXB_TextureBank *texb); 150 | 151 | /** 152 | * @brief Get texture atlas raw buffer. 153 | * @param texb Existing texture bank. 154 | * @return Texture atlas raw buffer. 155 | * @note `TEXB_flush()` can modify certain areas of the raw buffer. Use with caution! 156 | **/ 157 | TEXBLIB void *TEXB_get_atlas_contents(TEXB_TextureBank *texb); 158 | 159 | /** 160 | * @brief Get texture atlas flags. The flags also contains channel kind and pixel format. 161 | * @param texb Existing texture bank. 162 | * @return Texture bank flags. 163 | **/ 164 | TEXBLIB uint16_t TEXB_get_flags(TEXB_TextureBank *texb); 165 | 166 | /** 167 | * @brief Get amount of images in the texture atlas. 168 | * @param texb Existing texture bank. 169 | * @return Amount of images in the texture atlas. 170 | **/ 171 | TEXBLIB size_t TEXB_get_image_count(TEXB_TextureBank *texb); 172 | 173 | TEXBLIB TEXB_TextureImage *TEXB_get_image(TEXB_TextureBank *texb, size_t index, const char *name); 174 | TEXBLIB TEXB_TextureImage *TEXB_insert_image(TEXB_TextureBank *texb, TEXB_TextureImage *timg, uint32_t top, uint32_t left, uint32_t bottom, uint32_t right, size_t *index); 175 | TEXBLIB TEXB_TextureImage *TEXB_create_image_in_atlas(TEXB_TextureBank *texb, const char *name, uint32_t top, uint32_t left, uint32_t bottom, uint32_t right, uint32_t width, uint32_t height, size_t *index); 176 | TEXBLIB TEXB_TextureImage *TEXB_create_new_image(const char *name, TEXB_CHANNEL_KIND channelkind, TEXB_PIXEL_FORMAT pixelformat, uint32_t width, uint32_t height); 177 | TEXBLIB const char *TEXB_get_image_name(TEXB_TextureImage *timg); 178 | TEXBLIB int TEXB_set_image_name(TEXB_TextureImage *timg, const char *newname); 179 | TEXBLIB void *TEXB_get_image_contents(TEXB_TextureImage *timg); 180 | 181 | #ifdef __cplusplus 182 | } /* extern "C" */ 183 | #endif 184 | 185 | #endif 186 | -------------------------------------------------------------------------------- /src/CompilerName.h: -------------------------------------------------------------------------------- 1 | /** 2 | * CompilerName.h 3 | * Returns the compiler name 4 | **/ 5 | 6 | #ifdef __cplusplus 7 | #include 8 | #include 9 | #else 10 | #include 11 | #include 12 | #endif 13 | 14 | char name[256]; 15 | bool already_loaded=false; 16 | 17 | const char* CompilerName() 18 | { 19 | if(already_loaded) return name; 20 | memset(name,0,256); 21 | #if defined(__clang__) || defined(__ICC) || defined(__INTEL_COMPILER) 22 | /* Clang/LLVM or Intel Compiler. It support __VERSION__ ----- */ 23 | strcpy(name,__VERSION__); 24 | #elif defined(__GNUC__) || defined(__GNUG__) 25 | /* GCC. -----------------------------------------------------*/ 26 | strcpy(name,"gcc-" __VERSION__); 27 | #elif defined(__HP_cc) || defined(__HP_aCC) 28 | /* Hewlett-Packard C/aC++. ---------------------------------- */ 29 | #ifdef __cplusplus 30 | const static int v=__HP_aCC; 31 | #else 32 | const static int v=__HP_cc; 33 | #endif 34 | sprintf(name,"HP C/aC++ Version A.%02d.%02d.%02d",v/10000,(v/100)%100,v%100); 35 | #elif defined(__IBMC__) || defined(__IBMCPP__) 36 | /* IBM XL C/C++. -------------------------------------------- */ 37 | strcpy(name,"IBM XL C/C++ Version " __xlc__); 38 | #elif defined(_MSC_VER) 39 | /* Microsoft Visual Studio. --------------------------------- */ 40 | sprintf(name,"Visual Studio Version %02d.%02d.%05d.%02d",int(_MSC_VER/100),int(_MSC_VER)%100,int(_MSC_FULL_VER)%100000,_MSC_BUILD); 41 | #elif defined(__PGI) 42 | /* Portland Group PGCC/PGCPP. ------------------------------- */ 43 | sprintf(name,"PGCC/PGCPP Version %02d.%d.%d",__PGIC__,__PGIC_MINOR,__PGIC_PATCHLEVEL__); 44 | #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC) 45 | /* Oracle Solaris Studio. ----------------------------------- */ 46 | #ifdef __cplusplus 47 | const static int v=__SUNPRO_CC; 48 | #else 49 | const static int v=__SUNPRO_C; 50 | #endif 51 | sprintf(name,"Solaris Studio Version %x.%x.%x",v/0x1000,(v/0x10)%0x100,v%0x10); 52 | #else 53 | strcpy(name,"Unknown compiler"); 54 | #endif 55 | 56 | already_loaded=true; 57 | return name; 58 | } -------------------------------------------------------------------------------- /src/Info.rc.in: -------------------------------------------------------------------------------- 1 | #define ITSUDEMO_VERSION "@ITSUDEMO_VERSION@" 2 | 3 | #ifdef RC_INVOKED 4 | 5 | 1 VERSIONINFO 6 | FILEVERSION @ITSUDEMO_MAJOR@,@ITSUDEMO_MINOR@,@ITSUDEMO_REV@,@ITSUDEMO_PATCH@ 7 | PRODUCTVERSION @ITSUDEMO_MAJOR@,@ITSUDEMO_MINOR@,@ITSUDEMO_REV@,@ITSUDEMO_PATCH@ 8 | FILEFLAGSMASK 0x17L 9 | FILEOS 0x40004L 10 | FILETYPE 0x1L 11 | FILESUBTYPE 0x0L 12 | BEGIN 13 | BLOCK "StringFileInfo" 14 | BEGIN 15 | BLOCK "00000000" 16 | BEGIN 17 | VALUE "CompanyName", "Dark Energy Processor Corporation" 18 | VALUE "FileDescription", "TEXB Manipulation Tool" 19 | VALUE "FileVersion", ITSUDEMO_VERSION 20 | VALUE "InternalName", "Itsudemo.exe" 21 | VALUE "LegalCopyright", "Copyright © 2039 Dark Energy Processor Corporation" 22 | VALUE "OriginalFilename", "Itsudemo.exe" 23 | VALUE "ProductName", "Itsudemo" 24 | VALUE "ProductVersion", ITSUDEMO_VERSION 25 | END 26 | END 27 | BLOCK "VarFileInfo" 28 | BEGIN 29 | VALUE "Translation", 0x0, 0 30 | END 31 | END 32 | 33 | #endif 34 | -------------------------------------------------------------------------------- /src/Itsudemo.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Itsudemo.cpp 3 | * The main program. 4 | **/ 5 | 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | #include "TEXB.h" 18 | #include "CompilerName.h" 19 | #include "Info.rc" 20 | #include "tclap/CmdLine.h" 21 | #include "lodepng.h" 22 | #include "zlib.h" 23 | 24 | struct AppendStringVisitor:public TCLAP::Visitor 25 | { 26 | std::string append; 27 | std::string* append_where; 28 | AppendStringVisitor(std::string append_which,std::string* where_append):Visitor() 29 | { 30 | append=append_which; 31 | append_where=where_append; 32 | } 33 | void visit() 34 | { 35 | append_where->append(append); 36 | } 37 | }; 38 | 39 | uint16_t g_TexbFlags=0; 40 | //int main_interactive(); 41 | std::string create_texb_attribute(uint16_t texb_flags); 42 | 43 | std::vector getRawImageFromTEXB(TextureBank* texb,std::string& name,uint32_t& w,uint32_t& h) 44 | { 45 | if(texb->Name==name) 46 | { 47 | w=texb->Width; 48 | h=texb->Height; 49 | return texb->FetchRaw(); 50 | } 51 | TextureImage* timg=texb->FetchImage(name); 52 | w=timg->Width; 53 | h=timg->Height; 54 | return std::vector(timg->RawImage,timg->RawImage+w*h*4); 55 | } 56 | 57 | 58 | inline const char* getBasename(const char* path) 59 | { 60 | const char* a=strrchr(path,'/'); 61 | const char* b=strrchr(path,'\\'); 62 | return a == b ? path : std::max(a,b) + 1; 63 | } 64 | 65 | std::string getDirectory(const char* path) 66 | { 67 | const char* basename=getBasename(path); 68 | if(basename>path) 69 | return std::string(path,0,basename-path); 70 | 71 | return std::string(); 72 | } 73 | 74 | void show_information_once(TextureBank* texb,const std::string path) 75 | { 76 | static bool ever_here=false; 77 | if(ever_here) return; 78 | 79 | std::vector timg_list=texb->FetchAll(); 80 | TextureImage* temp_timg=NULL; 81 | 82 | std::cerr << "File: " << path << std::endl << "Size: " << texb->Width << "x" << texb->Height << " pixels" << std::endl; 83 | std::cerr << "Name: " << texb->Name << std::endl; 84 | std::cerr << "Attributes: " << create_texb_attribute(texb->Flags) << std::endl; 85 | std::cerr << "Image(s): " << timg_list.size() << std::endl; 86 | for(std::vector::iterator i=timg_list.begin();i!=timg_list.end();i++) 87 | { 88 | temp_timg=*i; 89 | std::cerr << " " << temp_timg->Name << ": " << temp_timg->Width << "x" << temp_timg->Height << " pixels" << std::endl; 90 | } 91 | 92 | ever_here=true; 93 | } 94 | 95 | void dumpTEXB(TextureBank* texb,const std::string path) 96 | { 97 | uint32_t temp_int=0; 98 | std::vector timg_list=texb->FetchAll(); 99 | TextureImage* temp_timg=NULL; 100 | std::string temp_string; 101 | const char* charPath=path.c_str(); 102 | const char* basename=getBasename(charPath); 103 | std::string fixedPath=getDirectory(charPath); 104 | 105 | show_information_once(texb,path); 106 | temp_string=std::string(getBasename(texb->Name.c_str()))+".png"; 107 | 108 | std::cerr << std::endl; 109 | std::cerr << "Writing: " << temp_string << std::endl; 110 | temp_int=lodepng::encode(fixedPath+temp_string,texb->FetchRaw(),texb->Width,texb->Height); 111 | if(temp_int!=0) 112 | std::cerr << " " << lodepng_error_text(temp_int) << std::endl; 113 | 114 | for(std::vector::iterator i=timg_list.begin();i!=timg_list.end();i++) 115 | { 116 | temp_timg=*i; 117 | temp_string=getBasename(temp_timg->Name.c_str())+std::string(".png"); 118 | std::cerr << "Writing: " << temp_string << std::endl; 119 | temp_int=lodepng::encode( 120 | fixedPath+temp_string, 121 | std::vector( 122 | reinterpret_cast(temp_timg->RawImage), 123 | reinterpret_cast(temp_timg->RawImage)+temp_timg->Width*temp_timg->Height*4 124 | ), 125 | temp_timg->Width, 126 | temp_timg->Height 127 | ); 128 | if(temp_int!=0) 129 | std::cerr << " " << lodepng_error_text(temp_int) << std::endl; 130 | } 131 | } 132 | 133 | bool parse_timg_path(const std::string& from,std::string* to,bool path_necessary=false,const std::string& default_suffix=".png") 134 | { 135 | const char* a=from.c_str(); 136 | const char* b=strchr(a,':'); 137 | 138 | if(b) 139 | { 140 | to[0]=std::string(a,uint32_t(b-a)); 141 | to[1]=std::string(b+1); 142 | } 143 | else 144 | { 145 | if(path_necessary) return false; 146 | const char* c=strrchr(a,'/'); 147 | c=c==NULL?a:c+1; 148 | to[0]=a; 149 | to[1]=std::string(c)+default_suffix; 150 | } 151 | return true; 152 | } 153 | 154 | std::string texb_image_image_info(uint16_t texb_flags) { 155 | uint8_t pix_format=uint8_t(texb_flags)>>6; 156 | uint8_t img_format=texb_flags&7; 157 | if (pix_format==3) { 158 | switch (img_format) { 159 | case 1: 160 | return "LUMA 1 Byte/Pixel"; 161 | case 0: 162 | return "ALPHA 1 Byte/Pixel"; 163 | case 2: 164 | return "LUMALPHA 2 Bytes/Pixel"; 165 | case 3: 166 | return "RGB 3 Bytes/Pixel"; 167 | default: 168 | return "RGBA 4 Bytes/Pixel"; 169 | } 170 | } else { 171 | switch(pix_format) { 172 | case 0: 173 | return "RGB565 2 Bytes/Pixel"; 174 | case 2: 175 | return "RGBA4444 2 Bytes/Pixel"; 176 | case 1: 177 | return "RGBA5551 2 Bytes/Pixel"; 178 | default: 179 | return "Unknown"; 180 | } 181 | } 182 | } 183 | 184 | std::string create_texb_attribute(uint16_t texb_flags) 185 | { 186 | std::string img=texb_image_image_info(texb_flags); 187 | std::string temp; 188 | if(texb_flags&8) temp.append("compressed "); 189 | else temp.append("uncompressed "); 190 | if(texb_flags&16) temp.append("mipmap "); 191 | if(texb_flags&32) temp.append("double-buffered "); 192 | temp.append(img); 193 | 194 | return temp; 195 | } 196 | 197 | int main(int argc,char* argv[]) 198 | { 199 | if(argc<2) 200 | { 201 | // TODO: Interactive mode 202 | //std::cerr << "Currently interactive mode is not supported." << std::endl; 203 | //return main_interactive(); 204 | std::cerr << " Type --help for command-line usage.\n" << std::endl; 205 | std::cerr << " Itsudemo. TEXB Manipulation tool\n\n Copyright (c) 2039 Dark Energy Processor Corporation\n" << std::endl; 206 | return 0; 207 | } 208 | std::string VersionString(ITSUDEMO_VERSION "\nCopyright (c) 2039 Dark Energy Processor Corporation\nCompiled with "); 209 | VersionString.append(CompilerName()); 210 | #ifdef _DEBUG 211 | VersionString.append(" (debug)"); 212 | #endif 213 | using namespace TCLAP; 214 | std::string CmdLineOrder; 215 | AppendStringVisitor AppendE("e",&CmdLineOrder); 216 | AppendStringVisitor AppendN("n",&CmdLineOrder); 217 | AppendStringVisitor AppendR("r",&CmdLineOrder); 218 | CmdLine CommandLine("Itsudemo. TEXB Manipulation tool\nCopyright (c) 2039 Dark Energy Processor Corporation",' ',VersionString); 219 | SwitchArg SwitchA("a","file-info","Prints TEXB information to stdout",CommandLine,false); 220 | ValueArg SwitchC("c","compress-level","Sets compress level when writing TEXB. 0 - No compression, 9 - Best compression",false,6,"0-9",CommandLine); 221 | SwitchArg SwitchD("d","dump-all","Dump all images, including the texture to PNG in the TEXB directory",CommandLine,false); 222 | MultiArg SwitchE("e","extract-image","Extract specificed TIMG image in TEXB.",false,"timg name>: SwitchN("n","rename","Rename internal name of TIMG in TEXB.",false,"timg name>: SwitchO("o","output","Specify output of the TEXB file. Defaults to input file which means overwrite it",false,"","output texb",CommandLine); 227 | MultiArg SwitchR("r","replace","Replace TIMG with PNG in TEXB.",false,"timg name>: TEXBInput("input","Input TEXB File location",true,"","input texb",CommandLine); 230 | 231 | try 232 | { 233 | CommandLine.parse(argc,argv); 234 | } 235 | catch(ArgException& e) 236 | { 237 | std::cerr << "Error: " << e.error() << " for arg " << e.argId() << std::endl; 238 | return -1; 239 | } 240 | 241 | TextureBank* texb=NULL; 242 | TextureImage* timg=NULL; 243 | uint32_t temp_int=0; 244 | char isTexbModified=0; 245 | std::string inputPath=TEXBInput.getValue(); 246 | 247 | try 248 | { 249 | texb=TextureBank::FromFile(inputPath); 250 | } 251 | catch(int err) 252 | { 253 | std::cerr << "Cannot open " << inputPath.c_str() << ": " << strerror(err) << std::endl; 254 | return err; 255 | } 256 | 257 | if(SwitchA.getValue()) 258 | { 259 | std::vector timg_list=texb->FetchAll(); 260 | TextureImage* temp_timg=NULL; 261 | 262 | show_information_once(texb,inputPath); 263 | std::cerr << std::endl; 264 | } 265 | 266 | if(SwitchG.getValue()) 267 | { 268 | std::vector timg_list=texb->FetchAll(); 269 | TextureImage* temp_timg=NULL; 270 | FILE* tempf=NULL; 271 | std::string link_path=getDirectory(inputPath.c_str()); 272 | uint8_t prebuf[4]; 273 | 274 | show_information_once(texb,inputPath); 275 | 276 | for(std::vector::iterator i=timg_list.begin();i!=timg_list.end();i++) 277 | { 278 | temp_timg=*i; 279 | std::string filename=getBasename(temp_timg->Name.c_str())+std::string(".png.imag"); 280 | uint32_t temp=texb->Name.length()+6; 281 | prebuf[0]=temp>>24; 282 | prebuf[1]=(temp>>16)&255; 283 | prebuf[2]=(temp>>8)&255; 284 | prebuf[3]=temp&255; 285 | 286 | std::cerr << "Writing: " << filename << std::endl; 287 | tempf=fopen((link_path+filename).c_str(),"wb"); 288 | fwrite("LINK",4,1,tempf); 289 | fwrite(prebuf,4,1,tempf); 290 | fwrite((texb->Name+".texb").c_str(),1,temp,tempf); 291 | fclose(tempf); 292 | } 293 | 294 | std::cerr << std::endl; 295 | } 296 | 297 | if(SwitchD.getValue()) 298 | { 299 | dumpTEXB(texb,inputPath); 300 | std::cerr << std::endl; 301 | return 0; 302 | } 303 | else if(SwitchI.getValue()) 304 | { 305 | show_information_once(texb,inputPath); 306 | 307 | std::string temp_string; 308 | std::vector timg_list=texb->FetchAll(); 309 | std::string fixedPath=getDirectory(inputPath.c_str()); 310 | 311 | for(std::vector::iterator i=timg_list.begin();i!=timg_list.end();i++) 312 | { 313 | TextureImage* ti = *i; 314 | temp_string=getBasename(ti->Name.c_str())+std::string(".png"); 315 | std::cerr << "Writing: " << temp_string << std::endl; 316 | temp_int=lodepng::encode( 317 | fixedPath+temp_string, 318 | std::vector( 319 | reinterpret_cast(ti->RawImage), 320 | reinterpret_cast(ti->RawImage)+ti->Width*ti->Height*4 321 | ), 322 | ti->Width, 323 | ti->Height 324 | ); 325 | if(temp_int!=0) 326 | std::cerr << " " << lodepng_error_text(temp_int) << std::endl; 327 | } 328 | std::cerr << std::endl; 329 | 330 | return 0; 331 | } 332 | else if(SwitchT.getValue()) 333 | { 334 | show_information_once(texb,inputPath); 335 | 336 | std::string temp_string=std::string(getBasename(texb->Name.c_str()))+".png"; 337 | std::string fixedPath=getDirectory(inputPath.c_str()); 338 | std::cerr << "Writing: " << temp_string << std::endl; 339 | temp_int=lodepng::encode(fixedPath+temp_string,texb->FetchRaw(),texb->Width,texb->Height); 340 | if(temp_int!=0) 341 | std::cerr << " " << lodepng_error_text(temp_int) << std::endl; 342 | std::cerr << std::endl; 343 | 344 | return 0; 345 | } 346 | 347 | std::string TEXBOutput; 348 | const std::vector ExtractList=SwitchE.getValue(); 349 | const std::vector RenameList=SwitchN.getValue(); 350 | const std::vector ReplaceList=SwitchR.getValue(); 351 | int indexE=(-1); 352 | int indexN=(-1); 353 | int indexR=(-1); 354 | uint32_t temp_int2=0; 355 | uint32_t temp_int3=0; 356 | std::vector rawImage; 357 | 358 | if(SwitchO.isSet()) 359 | TEXBOutput=SwitchO.getValue(); 360 | else 361 | TEXBOutput=inputPath; 362 | 363 | for(uint32_t i=0;iName << std::endl; 380 | continue; 381 | } 382 | std::cerr << "Extract: Writing " << timg_n_path[0] << " to " << timg_n_path[1] << std::endl; 383 | temp_int=lodepng::encode(timg_n_path[1],rawImage,temp_int2,temp_int3); 384 | if(temp_int!=0) 385 | std::cerr << "Extract: Cannot write " << timg_n_path[1] << ": " << lodepng_error_text(temp_int) << std::endl; 386 | rawImage=std::vector(); 387 | } 388 | else if(type=='n') 389 | { 390 | indexN++; 391 | if(parse_timg_path(RenameList[indexN],timg_n_path,true)==false) 392 | { 393 | std::cerr << "Rename: Cannot rename '" << RenameList[indexN] << "': missing 'timg new name'" << std::endl; 394 | continue; 395 | } 396 | 397 | if(texb->Name==timg_n_path[0]) 398 | texb->Name=timg_n_path[1]; 399 | else 400 | { 401 | try 402 | { 403 | timg=texb->FetchImage(timg_n_path[0]); 404 | } 405 | catch(int) 406 | { 407 | std::cerr << "Rename: Cannot find " << timg_n_path[0] << " in " << texb->Name << std::endl; 408 | continue; 409 | } 410 | 411 | std::cerr << "Rename: Renaming " << timg_n_path[0] << " to " << timg_n_path[1] << std::endl; 412 | timg->Name=std::string(timg_n_path[1]); 413 | } 414 | isTexbModified|=1; 415 | } 416 | else if(type=='r') 417 | { 418 | indexR++; 419 | parse_timg_path(ReplaceList[indexR],timg_n_path); 420 | 421 | if(texb->Name==timg_n_path[0]) 422 | { 423 | uint8_t* rw=texb->RawImage; 424 | temp_int=lodepng::decode(rawImage,temp_int2,temp_int3,timg_n_path[1]); 425 | if(temp_int!=0) 426 | { 427 | std::cerr << "Replace: Cannot read " << timg_n_path[1] << ": " << lodepng_error_text(temp_int) << std::endl; 428 | rawImage=std::vector(); 429 | continue; 430 | } 431 | if(texb->Width!=temp_int2 || texb->Height!=temp_int3) 432 | { 433 | std::cerr << "Replace: Cannot write " << timg_n_path[0] << ": Image size mismatch." << std::endl << 434 | " Expected " << texb->Width << "x" << texb->Height << " pixels, got " << 435 | temp_int2 << "x" << temp_int3 << " pixels." << std::endl; 436 | rawImage=std::vector(); 437 | continue; 438 | } 439 | std::cerr << "Replace: Replacing " << timg_n_path[0] << " image from " << timg_n_path[1] << std::endl; 440 | memcpy(texb->RawImage,&rawImage[0],temp_int2*temp_int3*4); 441 | isTexbModified|=4; 442 | } 443 | else 444 | { 445 | try 446 | { 447 | timg=texb->FetchImage(timg_n_path[0]); 448 | } 449 | catch(int) 450 | { 451 | std::cerr << "Replace: Cannot find " << timg_n_path[0] << " in " << texb->Name << std::endl; 452 | continue; 453 | } 454 | 455 | temp_int=lodepng::decode(rawImage,temp_int2,temp_int3,timg_n_path[1]); 456 | if(temp_int!=0) 457 | { 458 | std::cerr << "Replace: Cannot read " << timg_n_path[1] << ": " << lodepng_error_text(temp_int) << std::endl; 459 | rawImage=std::vector(); 460 | continue; 461 | } 462 | if(timg->Width!=temp_int2 || timg->Height!=temp_int3) 463 | { 464 | std::cerr << "Replace: Cannot write " << timg_n_path[0] << ": Image size mismatch." << std::endl << 465 | " Expected " << timg->Width << "x" << timg->Height << " pixels, got " << 466 | temp_int2 << "x" << temp_int3 << " pixels." << std::endl; 467 | rawImage=std::vector(); 468 | continue; 469 | } 470 | 471 | std::cerr << "Replace: Replacing " << timg_n_path[0] << " image from " << timg_n_path[1] << std::endl; 472 | memcpy(timg->RawImage,&rawImage[0],temp_int2*temp_int3*4); 473 | } 474 | rawImage=std::vector(); 475 | isTexbModified|=2; 476 | } 477 | else continue; // We should never reach here 478 | } 479 | 480 | // Reflect changes. 481 | if(isTexbModified>1 && (isTexbModified&4)==0) 482 | texb->ReflectChanges(); 483 | if(isTexbModified) 484 | texb->SaveToFile(TEXBOutput,SwitchC.getValue()); 485 | 486 | delete texb; 487 | return 0; 488 | } 489 | -------------------------------------------------------------------------------- /src/TEXB.h: -------------------------------------------------------------------------------- 1 | /** 2 | * TEXB.h 3 | * Definition about most common function 4 | **/ 5 | 6 | #ifndef TEXB_H_ 7 | #define TEXB_H_ 8 | 9 | // In case you want to use malloc and free, just add it to your compiler preprocessor 10 | // #define LIBTEXB_ALLOC(type,size) malloc(sizeof(type)*(size)) 11 | // #define LIBTEXB_FREE(ptr) free(ptr) 12 | 13 | // Defaults to C++ new and delete 14 | #ifndef LIBTEXB_ALLOC 15 | // Deprecated 16 | #define LIBTEXB_ALLOC(type,size) new type[size] 17 | #endif 18 | 19 | #ifndef LIBTEXB_FREE 20 | // Deprecated 21 | #define LIBTEXB_FREE(ptr) delete[] ptr 22 | #endif 23 | 24 | // Deprecated 25 | #ifdef LIBTEXB_NO_EXCEPTION 26 | #define LIBTEXB_FAILEXIT return NULL 27 | #define LIBTEXB_FAILWITH(errcode) {errno=errcode;return NULL;} 28 | #else 29 | #define LIBTEXB_FAILEXIT throw int(errno) 30 | #define LIBTEXB_FAILWITH(errcode) throw errcode 31 | #endif 32 | 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | 39 | #include 40 | #include 41 | 42 | #include "TEXB/TEXBLib.h" 43 | 44 | #if __cplusplus >= 201402L 45 | #define TEXB_DEPRECATED [[deprecated]] 46 | #elif defined(_MSC_VER) 47 | #define TEXB_DEPRECATED __declspec(deprecated) 48 | #elif defined(__GNUC__) || defined(__clang__) 49 | #define TEXB_DEPRECATED __attribute__((deprecated)) 50 | #else 51 | #define TEXB_DEPRECATED 52 | #endif 53 | 54 | struct Point 55 | { 56 | uint32_t X; 57 | uint32_t Y; 58 | }; 59 | 60 | struct UVPoint 61 | { 62 | double U; 63 | double V; 64 | }; 65 | 66 | struct VertexFormat 67 | { 68 | uint32_t X, Y, U, V; 69 | }; 70 | 71 | #pragma pack(push, 1) 72 | struct FixedVertexIndexFormat 73 | { 74 | std::array Vertices; 75 | std::array Indices; 76 | }; 77 | #pragma pack(pop) 78 | 79 | static_assert(sizeof(FixedVertexIndexFormat) == 70, "invalid fixed vertex index format size"); 80 | 81 | // Class of Image in TEXB 82 | struct TextureImage; 83 | 84 | // Class of Playground texture bank 85 | struct TextureBank 86 | { 87 | protected: 88 | uint32_t RawImageWidth; 89 | uint32_t RawImageHeight; 90 | uint16_t _Flags; 91 | std::vector ImageList_Id; 92 | std::map ImageList_Names; 93 | std::vector VertexIndexUVs; 94 | 95 | TextureBank():Width(RawImageWidth),Height(RawImageHeight),Flags(_Flags) {} 96 | public: 97 | ~TextureBank(); 98 | // Texture bank name defined in file. 99 | std::string Name; 100 | // Texture bank width 101 | const uint32_t& Width; 102 | // Texture bank height 103 | const uint32_t& Height; 104 | // Texture bank raw image in RGBA 105 | uint8_t* RawImage; 106 | // Texture bank flags when decoded with FromFile or FromMemory 107 | const uint16_t& Flags; 108 | // Creates TextureBank from specificed width and height. 109 | // It's recommended that the width and the height is power of 2, but width and height doesn't need to be equal 110 | // Example: 1024x512 is acceptable width and height 111 | TextureBank(uint32_t width,uint32_t height); 112 | // Loads TEXB from file 113 | // Throws int or returns NULL on error 114 | static TextureBank* FromFile(const std::string &filename); 115 | // Loads TEXB from memory 116 | // Throws int or returns NULL on error 117 | static TextureBank* FromMemory(const uint8_t* memory,size_t memsize); 118 | // Loads TEXB from callback 119 | // Throws std::exception on error 120 | static TextureBank* FromCallback(void *handle, size_t(*reader)(void*, size_t, size_t, void*)); 121 | // Clone this TextureBank to a new instance. 122 | TextureBank* Clone(); 123 | 124 | size_t GetImageCount() const noexcept; 125 | // List all images name in TEXB as std::vector 126 | // Images name doesn't contain .png.imag suffix 127 | std::vector ListImages(); 128 | // Returns the raw image data from image name in TextureBank in form of RGBA 129 | // Returns NULL or throws exception on failure 130 | TextureImage* FetchImage(const std::string &image_name); 131 | // Returns the raw image data from index in TextureBank in form of RGBA 132 | // Returns NULL or throws exception on failure 133 | TextureImage* FetchImage(size_t index); 134 | // Returns all TextureImage 135 | std::vector FetchAll(); 136 | // Returns raw image of this texture bank 137 | std::vector FetchRaw(); 138 | // Same as FetchImage(std::string) 139 | inline TextureImage* operator[](std::string image_name) 140 | { 141 | return FetchImage(image_name); 142 | } 143 | // Same as FetchImage(uint32_t) 144 | inline TextureImage* operator[](uint32_t index) 145 | { 146 | return FetchImage(index); 147 | } 148 | // Same as FetchImage(std::string) except that it accepts const char* 149 | inline TextureImage* operator[](const char* image_name) 150 | { 151 | return FetchImage(std::string(image_name)); 152 | } 153 | 154 | // Replaces image inside texture bank with the specificed TextureImage 155 | // Returns 0 on success 156 | TEXB_DEPRECATED 157 | int32_t ReplaceImage(TextureImage* image); 158 | // Replaces image with specificed index inside texture bank with the specificed TextureImage 159 | // Returns 0 on success 160 | TEXB_DEPRECATED 161 | int32_t ReplaceImage(TextureImage* image,size_t index); 162 | // Replaces image with specified index inside texture bank with the speificed TextureImage 163 | // Throws exception on failure 164 | // dummy can be anything 165 | void ReplaceImage(TextureImage *image, size_t index, bool dummy); 166 | // Define new texture image location 167 | // Returns 0 and the texture image index on success 168 | TEXB_DEPRECATED 169 | int32_t DefineImage(const Point* texture_vertexes,const UVPoint* texture_uvs,std::string name,uint32_t* index); 170 | // Defines new texture image location with their UVs automatically generated. 171 | // Returns 0 and the texture image index on success 172 | TEXB_DEPRECATED 173 | int32_t DefineImage(const Point* texture_where_width_height,std::string name,uint32_t* index); 174 | // Define new texture image location 175 | // Returns the texture image index on success 176 | size_t DefineImage(const std::string &name, uint32_t top, uint32_t left, uint32_t bottom, uint32_t right, uint32_t width, uint32_t height); 177 | // Saves current TextureBank to file 178 | // Returns 0 on success 179 | int32_t SaveToFile(std::string filename,uint32_t compression_level=9); 180 | // Saves current TextureBank to memory. The memory is allocated by the function 181 | // memory_size can't be NULL 182 | // Returns 0 on success 183 | int32_t SaveToMemory(uint8_t*& memory,size_t* memory_size,uint32_t compression_level=9); 184 | int32_t SaveToMemory(uint8_t** memory,size_t* memory_size,uint32_t compression_level=9); 185 | // Reflect all changes made in the TextureImage by writing to TEXB raw buffer 186 | // Warning: can be slow if called repeatedly 187 | void ReflectChanges(); 188 | 189 | friend struct TextureImage; 190 | }; 191 | 192 | struct TextureImage 193 | { 194 | protected: 195 | TextureBank* from_texb; 196 | // Pre-calculated when constructed. 197 | //uint8_t* Indexes; // Most likely to be 6 in size 198 | //uint32_t* Vertexes; // Most likely to be 4 in size 199 | TextureImage() {} 200 | public: 201 | ~TextureImage(); 202 | // Creates TextureImage with specificed Width, Height, and RawImage data. 203 | // raw image is assumed to have format of RGBA. 204 | // raw image is copied so it's safe to free the raw image memory specificed in raw_image afterwards. 205 | // if raw image is null, the raw image filled with white. 206 | TextureImage(uint32_t width, uint32_t height, uint8_t* raw_image = nullptr); 207 | // Creates TextureImage with specified Width, Height, Channel Kind, Pixel Format, and optionally Pixel Data. 208 | // Pixel Data has format of channelkind and pixfmt. 209 | // Pixel Data is copied so it's safe to free the Pixel Data afterwards. 210 | // If Pixel Data is null, the image is filled with white. 211 | TextureImage(uint32_t width, uint32_t height, TEXB_CHANNEL_KIND channelkind, TEXB_PIXEL_FORMAT pixfmt, uint8_t *pixels = nullptr); 212 | // Copy this TextureImage to a new instance 213 | TextureImage* Clone(); 214 | // Image width 215 | uint32_t Width; 216 | // Image height 217 | uint32_t Height; 218 | // Image pixel data in form of RGBA. 4-bytes/pixel 219 | uint8_t* RawImage; 220 | // Name of this image without .png.imag 221 | std::string Name; 222 | // Channel kind 223 | TEXB_CHANNEL_KIND ChannelKind; 224 | // Pixel format 225 | TEXB_PIXEL_FORMAT PixelFormat; 226 | 227 | friend struct TextureBank; 228 | }; 229 | 230 | typedef TextureBank TEXB; 231 | typedef TextureImage TIMG; 232 | 233 | #endif 234 | -------------------------------------------------------------------------------- /src/TEXBFetch.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * TEXBFetch.cpp 3 | * TextureBank::Fetch* implementation 4 | **/ 5 | 6 | #include "TEXB.h" 7 | 8 | #include 9 | #include 10 | 11 | #include 12 | 13 | #include 14 | 15 | TextureImage* TextureBank::FetchImage(const std::string &ImageName) 16 | { 17 | std::map::iterator i=ImageList_Names.find(ImageName); 18 | if(i==this->ImageList_Names.end()) 19 | LIBTEXB_FAILWITH(ENOENT); 20 | 21 | return ImageList_Id[i->second]; 22 | } 23 | 24 | TextureImage* TextureBank::FetchImage(size_t Index) 25 | { 26 | if(IndexImageList_Id.size()) 27 | return (this->ImageList_Id)[Index]; 28 | else 29 | LIBTEXB_FAILWITH(ERANGE); 30 | } 31 | 32 | std::vector TextureBank::FetchAll() 33 | { 34 | std::vector i=this->ImageList_Id; 35 | return i; 36 | } 37 | 38 | std::vector TextureBank::FetchRaw() 39 | { 40 | return std::vector(this->RawImage,this->RawImage+this->RawImageWidth*this->RawImageHeight*4); 41 | } 42 | -------------------------------------------------------------------------------- /src/TEXBLib.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #if defined(_MSC_VER) && defined(_DLL) 7 | # define TEXBLIB __declspec(dllexport) 8 | #elif defined(__GNUC__) || defined(__clang__) 9 | # define TEXBLIB __attribute__((visibility("default"))) 10 | #else 11 | # define TEXBLIB 12 | #endif 13 | 14 | #include "TEXB/TEXBLib.h" 15 | #include "TEXB.h" 16 | #include "TEXBPixel.h" 17 | 18 | thread_local std::string lastError = LIBTEXB_VERSION; 19 | 20 | extern "C" { 21 | 22 | TEXBLIB size_t TEXB_version() 23 | { 24 | return LIBTEXB_VERSION_NUM; 25 | } 26 | 27 | TEXBLIB const char *TEXB_version_string() 28 | { 29 | return LIBTEXB_VERSION; 30 | } 31 | 32 | TEXBLIB const char *TEXB_get_last_error() 33 | { 34 | return lastError.c_str(); 35 | } 36 | 37 | TEXBLIB TEXB_TextureBank *TEXB_create(uint32_t width, uint32_t height) 38 | { 39 | try 40 | { 41 | return new TextureBank(width, height); 42 | } 43 | catch (std::exception &e) 44 | { 45 | lastError = e.what(); 46 | return nullptr; 47 | } 48 | catch (...) 49 | { 50 | std::terminate(); 51 | } 52 | } 53 | 54 | TEXBLIB TEXB_TextureBank *TEXB_open_from_file(const char *path) 55 | { 56 | try 57 | { 58 | return TextureBank::FromFile(path); 59 | } 60 | catch (std::exception &e) 61 | { 62 | lastError = e.what(); 63 | } 64 | catch (int e) 65 | { 66 | lastError = strerror(e); 67 | } 68 | catch (...) 69 | { 70 | std::terminate(); 71 | } 72 | 73 | return nullptr; 74 | } 75 | 76 | TEXBLIB TEXB_TextureBank *TEXB_open_from_callback(void *handle, size_t(*reader)(void *, size_t, size_t, void *)) 77 | { 78 | try 79 | { 80 | return TextureBank::FromCallback(handle, reader); 81 | } 82 | catch (std::exception &e) 83 | { 84 | lastError = e.what(); 85 | } 86 | catch (...) 87 | { 88 | std::terminate(); 89 | } 90 | 91 | return nullptr; 92 | } 93 | 94 | TEXBLIB TEXB_TextureBank *TEXB_open_from_memory(const void *data, size_t size) 95 | { 96 | try 97 | { 98 | return TextureBank::FromMemory((const uint8_t *) data, size); 99 | } 100 | catch (std::exception &e) 101 | { 102 | lastError = e.what(); 103 | } 104 | catch (int e) 105 | { 106 | lastError = strerror(e); 107 | } 108 | catch (...) 109 | { 110 | std::terminate(); 111 | } 112 | 113 | return nullptr; 114 | } 115 | 116 | TEXBLIB TEXB_TextureBank *TEXB_clone(TEXB_TextureBank *texb) 117 | { 118 | try 119 | { 120 | return texb->Clone(); 121 | } 122 | catch (std::exception &e) 123 | { 124 | lastError = e.what(); 125 | } 126 | catch (...) 127 | { 128 | std::terminate(); 129 | } 130 | 131 | return nullptr; 132 | } 133 | 134 | TEXBLIB void TEXB_free(TEXB_TextureBank *t1, TEXB_TextureBank **t2) 135 | { 136 | delete t1; 137 | 138 | if (t2) 139 | { 140 | delete *t2; 141 | *t2 = nullptr; 142 | } 143 | } 144 | 145 | TEXBLIB const char *TEXB_get_name(TEXB_TextureBank *texb) 146 | { 147 | return texb->Name.c_str(); 148 | } 149 | 150 | TEXBLIB int TEXB_set_name(TEXB_TextureBank *texb, const char *newname) 151 | { 152 | try 153 | { 154 | texb->Name = newname; 155 | return 1; 156 | } 157 | catch (std::exception &e) 158 | { 159 | lastError = e.what(); 160 | } 161 | 162 | return 0; 163 | } 164 | 165 | TEXBLIB uint32_t TEXB_get_atlas_width(TEXB_TextureBank *texb) 166 | { 167 | return texb->Width; 168 | } 169 | 170 | TEXBLIB uint32_t TEXB_get_atlas_height(TEXB_TextureBank *texb) 171 | { 172 | return texb->Height; 173 | } 174 | 175 | TEXBLIB void *TEXB_get_atlas_contents(TEXB_TextureBank *texb) 176 | { 177 | return texb->RawImage; 178 | } 179 | 180 | TEXBLIB uint16_t TEXB_get_flags(TEXB_TextureBank *texb) 181 | { 182 | return texb->Flags; 183 | } 184 | 185 | TEXBLIB size_t TEXB_get_image_count(TEXB_TextureBank *texb) 186 | { 187 | return texb->GetImageCount(); 188 | } 189 | 190 | TEXBLIB TEXB_TextureImage *TEXB_get_image(TEXB_TextureBank *texb, size_t index, const char *name) 191 | { 192 | try 193 | { 194 | if (name) 195 | return texb->FetchImage(name); 196 | 197 | return texb->FetchImage(index); 198 | } 199 | catch (std::exception &e) 200 | { 201 | lastError = e.what(); 202 | } 203 | catch (int e) 204 | { 205 | lastError = strerror(e); 206 | } 207 | catch (...) 208 | { 209 | std::terminate(); 210 | } 211 | 212 | return nullptr; 213 | } 214 | 215 | TEXBLIB TEXB_TextureImage *TEXB_insert_image( 216 | TEXB_TextureBank *texb, 217 | TEXB_TextureImage *timg, 218 | uint32_t top, 219 | uint32_t left, 220 | uint32_t bottom, 221 | uint32_t right, 222 | size_t *index 223 | ) 224 | { 225 | try 226 | { 227 | size_t idx = texb->DefineImage(timg->Name, top, left, bottom, right, timg->Width, timg->Height); 228 | TEXB_TextureImage *newTimg = texb->FetchImage(idx); 229 | memcpy(newTimg->RawImage, timg->RawImage, timg->Width * timg->Height * GetBytePerPixel(timg->ChannelKind, timg->PixelFormat)); 230 | 231 | if (index) 232 | *index = idx; 233 | 234 | return newTimg; 235 | } 236 | catch (std::exception &e) 237 | { 238 | lastError = e.what(); 239 | } 240 | catch (...) 241 | { 242 | std::terminate(); 243 | } 244 | 245 | return nullptr; 246 | } 247 | 248 | TEXBLIB TEXB_TextureImage *TEXB_create_image_in_atlas( 249 | TEXB_TextureBank *texb, 250 | const char *name, 251 | uint32_t top, 252 | uint32_t left, 253 | uint32_t bottom, 254 | uint32_t right, 255 | uint32_t width, 256 | uint32_t height, 257 | size_t *index 258 | ) 259 | { 260 | try 261 | { 262 | size_t idx = texb->DefineImage(name, top, left, bottom, right, width, height); 263 | TEXB_TextureImage *newTimg = texb->FetchImage(idx); 264 | 265 | if (index) 266 | *index = idx; 267 | 268 | return newTimg; 269 | } 270 | catch (std::exception &e) 271 | { 272 | lastError = e.what(); 273 | } 274 | catch (...) 275 | { 276 | std::terminate(); 277 | } 278 | 279 | return nullptr; 280 | } 281 | 282 | TEXBLIB TEXB_TextureImage *TEXB_create_new_image( 283 | const char *name, 284 | TEXB_CHANNEL_KIND channelkind, 285 | TEXB_PIXEL_FORMAT pixelformat, 286 | uint32_t width, 287 | uint32_t height 288 | ) 289 | { 290 | try 291 | { 292 | return new TEXB_TextureImage(width, height, channelkind, pixelformat); 293 | } 294 | catch (std::exception &e) 295 | { 296 | lastError = e.what(); 297 | } 298 | catch (...) 299 | { 300 | std::terminate(); 301 | } 302 | 303 | return nullptr; 304 | } 305 | 306 | } // extern "C" 307 | -------------------------------------------------------------------------------- /src/TEXBLoad.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * TEXBLoad.cpp 3 | * Load and parse Texture Bank file 4 | **/ 5 | 6 | #include "TEXB.h" 7 | #include "TEXBPixel.h" 8 | #include "xy2uv.h" 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | 23 | #include 24 | 25 | #include "zlib.h" 26 | 27 | struct MemoryReader 28 | { 29 | const void *buffer; 30 | size_t size, pos; 31 | 32 | static size_t read(void *dest, size_t elemSize, size_t elemCount, void *mreader) 33 | { 34 | MemoryReader *reader = (MemoryReader *) mreader; 35 | size_t elemRead = std::min(elemCount, (reader->size - reader->pos) / elemSize); 36 | 37 | memcpy(dest, reader->buffer, elemSize * elemRead); 38 | reader->pos += elemRead * elemSize; 39 | return elemRead; 40 | } 41 | }; 42 | 43 | TextureBank* TextureBank::FromMemory(const uint8_t* _mem,size_t _n) 44 | { 45 | MemoryReader reader = {_mem, _n, 0}; 46 | return FromCallback(&reader, MemoryReader::read); 47 | } 48 | 49 | TextureBank* TextureBank::FromFile(const std::string &Filename) 50 | { 51 | TextureBank* texb = nullptr; 52 | FILE* file = fopen(Filename.c_str(), "rb"); 53 | 54 | if (file == nullptr) 55 | throw int(errno); 56 | 57 | try 58 | { 59 | texb = FromCallback(file, (size_t(*)(void*, size_t, size_t, void*)) fread); 60 | } 61 | catch (std::bad_alloc &) 62 | { 63 | throw int(ENOMEM); 64 | } 65 | catch (std::exception &) 66 | { 67 | throw int(EBADF); 68 | } 69 | 70 | fclose(file); 71 | return texb; 72 | } 73 | 74 | TextureBank *TextureBank::FromCallback(void *handle, size_t(*reader)(void*, size_t, size_t, void*)) 75 | { 76 | std::unique_ptr texb = nullptr; 77 | std::vector scratchpadVector(40); 78 | uint8_t *scratchpad = scratchpadVector.data(); 79 | 80 | auto readData = [&scratchpadVector, &scratchpad, handle, reader](size_t size, void *dest = nullptr) 81 | { 82 | if (dest == nullptr) 83 | { 84 | if (size > scratchpadVector.size()) 85 | { 86 | scratchpadVector.resize(size); 87 | scratchpad = scratchpadVector.data(); 88 | } 89 | 90 | dest = scratchpad; 91 | } 92 | 93 | if (reader(dest, 1, size, handle) < 4) 94 | throw std::runtime_error("read error"); 95 | }; 96 | 97 | // Find header 98 | readData(4); 99 | if(memcmp(scratchpad, "TEXB", 4) != 0) 100 | throw std::runtime_error("not TEXB"); 101 | 102 | readData(4); 103 | ptrdiff_t texbSize = (scratchpad[0] << 24) | (scratchpad[1] << 16) | (scratchpad[2] << 8) | scratchpad[3]; 104 | texb.reset(new TextureBank); 105 | 106 | // TEXB Name without T prefix and extension 107 | { 108 | readData(2); texbSize -= 2; 109 | size_t texbNameLen = (scratchpad[0] << 8U) | scratchpad[1]; 110 | readData(texbNameLen); texbSize -= texbNameLen; 111 | std::string texbName((char *) scratchpad); 112 | texb->Name = texbName.substr(1, texbName.find(".texb") - 1); // Discard .texb 113 | } 114 | 115 | // Width & Height 116 | uint16_t tWidth = 0, tHeight = 0; 117 | readData(4); texbSize -= 4; 118 | texb->RawImageWidth = (scratchpad[0] << 8) | scratchpad[1]; 119 | texb->RawImageHeight = (scratchpad[2] << 8) | scratchpad[3]; 120 | 121 | // Flags 122 | readData(2); texbSize -= 2; 123 | uint16_t TexbFlags = (scratchpad[0] << 8) | scratchpad[1]; 124 | texb->_Flags = TexbFlags; 125 | 126 | // Vertex & Index count 127 | readData(6); texbSize -= 6; 128 | /* 129 | uint16_t VertexCount = (scratchpad[0] << 8) | scratchpad[1]; 130 | uint16_t IndexCount = (scratchpad[2] << 8) | scratchpad[3]; 131 | */ 132 | uint16_t ImageCount = (scratchpad[4] << 8) | scratchpad[5]; 133 | 134 | for(size_t i = 0; i < ImageCount; i++) 135 | { 136 | readData(6); texbSize -= 6; 137 | ptrdiff_t imageDefLen = (scratchpad[4] << 8U) | scratchpad[5]; 138 | 139 | if (memcmp(scratchpad, "TIMG", 4) == 0) 140 | { 141 | std::unique_ptr timg(new TextureImage); 142 | timg->from_texb = texb.get(); 143 | 144 | // Texture Image name 145 | readData(2); imageDefLen -= 2; texbSize -= 2; 146 | size_t timgNameLen = (scratchpad[0] << 8) | scratchpad[1]; 147 | readData(timgNameLen); imageDefLen -= timgNameLen; 148 | std::string timgName((char*) scratchpad); 149 | timg->Name = timgName.substr(1, timgName.find(".png.imag") - 1); // Discard .png.imag 150 | 151 | // Subimages 152 | readData(2); imageDefLen -= 2; texbSize -= 2; 153 | uint16_t Subimgs = (scratchpad[0] << 8) | scratchpad[1]; 154 | bool cancel = false; 155 | 156 | if(Subimgs == 65535) 157 | { 158 | readData(2); imageDefLen -= 2; texbSize -= 2; 159 | size_t attributes = (scratchpad[0] << 8) | scratchpad[1]; 160 | 161 | for(size_t j = 0; j < attributes; j++) 162 | { 163 | // Based from extb.c, it just skips the attribute. 164 | uint8_t keyAndType[2]; 165 | readData(2, keyAndType); imageDefLen -= 2; texbSize -= 2; 166 | 167 | switch(keyAndType[1]) 168 | { 169 | case 0: 170 | case 1: 171 | { 172 | readData(4); imageDefLen -= 4; texbSize -= 4; 173 | break; 174 | } 175 | case 2: 176 | { 177 | readData(2); imageDefLen -= 2; texbSize -= 2; 178 | size_t skipLen = (scratchpad[0] << 8) | scratchpad[1]; 179 | readData(skipLen); imageDefLen -= skipLen; texbSize -= skipLen; 180 | break; 181 | } 182 | default: 183 | { 184 | // Based from extb.c, this is unknown and the program just throws assert failed. Delete all memory and exit 185 | // But let's ignore this whole section instead. 186 | cancel = true; 187 | break; 188 | } 189 | } 190 | 191 | if (cancel) 192 | break; 193 | } 194 | } 195 | 196 | if (!cancel) 197 | { 198 | if (Subimgs == 65535) 199 | { 200 | readData(2); imageDefLen -= 2; texbSize -= 2; 201 | Subimgs = (scratchpad[0] << 8) | scratchpad[1]; 202 | } 203 | 204 | if(Subimgs > 1) 205 | { 206 | // Currently not supported. It breaks the TextureImage structure 207 | cancel = true; 208 | } 209 | } 210 | 211 | uint8_t verts = 0; 212 | uint8_t idxs = 0; 213 | 214 | if (!cancel) 215 | { 216 | readData(6); imageDefLen -= 6; texbSize -= 6; 217 | uint8_t verts = scratchpad[0]; 218 | uint8_t idxs = scratchpad[1]; 219 | timg->Width = (scratchpad[2] << 8) | scratchpad[3]; 220 | timg->Height = (scratchpad[4] << 8) | scratchpad[5]; 221 | 222 | if (verts != 4 || idxs != 6) 223 | { 224 | // TODO non-rectangular 225 | cancel = true; 226 | } 227 | } 228 | 229 | std::unique_ptr vibuf(nullptr); 230 | 231 | if (!cancel) 232 | { 233 | // CenterX, CenterY 234 | readData(4); imageDefLen -= 4; texbSize -= 4; 235 | 236 | // Vertices 237 | uint32_t vsize = verts * sizeof(uint32_t) * 4; 238 | vibuf.reset(new FixedVertexIndexFormat); 239 | 240 | for(size_t l = 0; l < 4; l++) 241 | { 242 | readData(16); imageDefLen -= 16; texbSize -= 16; 243 | uint32_t x = (scratchpad[0] << 24) | (scratchpad[1] << 16) | (scratchpad[2] << 8) | scratchpad[3]; 244 | uint32_t y = (scratchpad[4] << 24) | (scratchpad[5] << 16) | (scratchpad[6] << 8) | scratchpad[7]; 245 | uint32_t u = (scratchpad[8] << 24) | (scratchpad[9] << 16) | (scratchpad[10] << 8) | scratchpad[11]; 246 | uint32_t v = (scratchpad[12] << 24) | (scratchpad[13] << 16) | (scratchpad[14] << 8) | scratchpad[15]; 247 | vibuf->Vertices[l] = {x, y, u, v}; 248 | } 249 | 250 | // Indices 251 | readData(6); imageDefLen -= 6; texbSize -= 6; 252 | std::copy(scratchpad, scratchpad + 6, vibuf->Indices.begin()); 253 | 254 | } 255 | 256 | if ((!cancel && imageDefLen != 0) || (cancel && imageDefLen < 0)) 257 | throw std::runtime_error("image definition size mismatch"); 258 | 259 | if (cancel) 260 | { 261 | readData(imageDefLen); 262 | texbSize -= imageDefLen; 263 | } 264 | else 265 | { 266 | texb->ImageList_Id.push_back(timg.get()); 267 | texb->VertexIndexUVs.push_back(vibuf.get()); 268 | timg.release(); 269 | vibuf.release(); 270 | } 271 | } 272 | } 273 | 274 | size_t dataSize = GetBytePerPixel(TexbFlags) * tWidth * tHeight; 275 | std::unique_ptr rawData(new uint8_t[dataSize]); 276 | 277 | if(TexbFlags & TEXB_FLAG_COMPRESSED) 278 | { 279 | // Read 4 more byte to get the compression type. 280 | readData(4); texbSize -= 4; 281 | uint32_t compressType = (scratchpad[0] << 24) | (scratchpad[1] << 16) | (scratchpad[2] << 8) | scratchpad[3]; 282 | 283 | if(compressType == 0) 284 | { 285 | // deflate compression. 286 | readData(texbSize); 287 | uLongf destLen = (uLongf) dataSize; 288 | uLong sourceLen = (uLong) texbSize; texbSize = 0; 289 | 290 | int result = uncompress2(rawData.get(), &destLen, scratchpad, &sourceLen); 291 | 292 | if (result != Z_OK) 293 | throw std::runtime_error("zlib decompress error"); 294 | } 295 | else if (compressType == 0x8D64) 296 | throw std::runtime_error("TODO ETC1 decompression"); 297 | else 298 | // Upps, I don't know the kind of the compression. 299 | throw std::runtime_error("unknown or unsupported compression"); 300 | } 301 | else 302 | readData(dataSize, rawData.get()); 303 | 304 | std::unique_ptr rgbaData(new uint8_t[tWidth * tHeight * 4]); 305 | convert_map(rawData.get(), tWidth, tHeight, TexbFlags, rgbaData.get()); 306 | 307 | texb->RawImage = rgbaData.get(); 308 | rawData.release(); 309 | rgbaData.release(); 310 | 311 | for(size_t i = 0; i < texb->ImageList_Id.size(); i++) 312 | { 313 | TextureImage* timg = texb->ImageList_Id[i]; 314 | texb->ImageList_Names[timg->Name] = i; 315 | 316 | FixedVertexIndexFormat* Vrtx = texb->VertexIndexUVs[i]; 317 | Point v[4] = { 318 | {Vrtx->Vertices[0].X / 65536, Vrtx->Vertices[0].Y / 65536}, 319 | {Vrtx->Vertices[1].X / 65536, Vrtx->Vertices[1].Y / 65536}, 320 | {Vrtx->Vertices[2].X / 65536, Vrtx->Vertices[2].Y / 65536}, 321 | {Vrtx->Vertices[3].X / 65536, Vrtx->Vertices[3].Y / 65536}, 322 | }; 323 | UVPoint t[4]={ 324 | {std::min(Vrtx->Vertices[0].U / 65536.0, 1.0), std::min(Vrtx->Vertices[0].V / 65536.0, 1.0)}, 325 | {std::min(Vrtx->Vertices[1].U / 65536.0, 1.0), std::min(Vrtx->Vertices[1].V / 65536.0, 1.0)}, 326 | {std::min(Vrtx->Vertices[2].U / 65536.0, 1.0), std::min(Vrtx->Vertices[2].V / 65536.0, 1.0)}, 327 | {std::min(Vrtx->Vertices[3].U / 65536.0, 1.0), std::min(Vrtx->Vertices[3].V / 65536.0, 1.0)}, 328 | }; 329 | 330 | auto exes = {v[0].X, v[1].X, v[2].X, v[3].X}; 331 | auto ways = {v[0].Y, v[1].Y, v[2].Y, v[3].Y}; 332 | uint32_t min_x = std::min(exes), max_x = std::max(exes); 333 | uint32_t min_y = std::min(ways), max_y = std::max(ways); 334 | 335 | size_t rawBmpSize = timg->Width * timg->Height; 336 | uint32_t* texbBmp = (uint32_t *) texb->RawImage; // uh type punning 337 | std::unique_ptr rawBmp(new uint32_t[rawBmpSize]); 338 | 339 | memset(rawBmp.get(), 0, rawBmpSize * sizeof(uint32_t)); 340 | 341 | for(uint32_t y = min_y; y < max_y; y++) 342 | { 343 | for(uint32_t x = min_x; x < max_x; x++) 344 | { 345 | UVPoint uv = xy2uv(x, y, v[0], v[1], v[2], v[3], t[0], t[1], t[2], t[3]); 346 | size_t idx = x + y * timg->Width; 347 | 348 | if (idx >= 0 && idx < rawBmpSize) 349 | // TODO: Bilinear interpolate 350 | rawBmp[idx] = texbBmp[uint32_t(uv.U * tWidth + 0.5) + uint32_t(uv.V * tHeight + 0.5) * tWidth]; 351 | } 352 | } 353 | 354 | timg->RawImage = (uint8_t *) rawBmp.release(); // uh type punning again 355 | } 356 | 357 | return texb.release(); 358 | } 359 | 360 | size_t TextureBank::GetImageCount() const noexcept 361 | { 362 | return ImageList_Id.size(); 363 | } 364 | -------------------------------------------------------------------------------- /src/TEXBModify.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * TEXBModify.cpp 3 | * Modification of TextureBank class 4 | **/ 5 | 6 | #include "TEXB.h" 7 | #include "xy2uv.h" 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | TextureBank::TextureBank(uint32_t _Width,uint32_t _Height):Width(RawImageWidth),Height(RawImageHeight),Flags(_Flags) 19 | { 20 | uint32_t rawimage_size=_Width*_Height*4; 21 | RawImageWidth=_Width; 22 | RawImageHeight=_Height; 23 | RawImage=LIBTEXB_ALLOC(uint8_t,rawimage_size); // 4-byte/pixel 24 | _Flags=0; 25 | 26 | memset(RawImage,0,rawimage_size); 27 | } 28 | 29 | TextureBank::~TextureBank() 30 | { 31 | for(uint32_t i=0;iImageList_Id.size();i++) 32 | { 33 | TextureImage* a=this->ImageList_Id[i]; 34 | delete this->VertexIndexUVs[i]; 35 | if(a->from_texb==this) 36 | delete a; 37 | } 38 | } 39 | 40 | TextureBank* TextureBank::Clone() 41 | { 42 | TextureBank* texb=new TextureBank; 43 | size_t memsize=Width*Height*4; 44 | 45 | texb->RawImageWidth=Width; 46 | texb->RawImageHeight=Height; 47 | texb->RawImage=LIBTEXB_ALLOC(uint8_t,memsize); 48 | texb->Name=Name; 49 | memcpy(texb->RawImage,RawImage,memsize); 50 | 51 | memsize=ImageList_Id.size(); 52 | for(uint32_t i=0;iClone(); 55 | FixedVertexIndexFormat *VrtxMem = new FixedVertexIndexFormat(*VertexIndexUVs[i]); 56 | timg->from_texb=texb; 57 | texb->ImageList_Id.push_back(timg); 58 | texb->ImageList_Names[timg->Name]=i; 59 | texb->VertexIndexUVs.push_back(VrtxMem); 60 | 61 | memcpy(VrtxMem,VertexIndexUVs[i],70); 62 | } 63 | 64 | return texb; 65 | } 66 | 67 | int32_t TextureBank::ReplaceImage(TextureImage* Image) 68 | { 69 | std::map::iterator i=ImageList_Names.find(Image->Name); 70 | if(i!=ImageList_Names.end()) 71 | return ReplaceImage(Image,i->second); 72 | 73 | return EINVAL; 74 | } 75 | 76 | int32_t TextureBank::ReplaceImage(TextureImage* Image,size_t Index) 77 | { 78 | if(Index>=ImageList_Id.size()) return ERANGE; 79 | 80 | TextureImage* target=ImageList_Id[Index]; 81 | if(target->Width!=Image->Width || target->Height!=Image->Height || target->Name!=Image->Name) 82 | return EINVAL; 83 | 84 | if(target->from_texb==this) delete target; 85 | ImageList_Id[Index]=Image; 86 | 87 | // Copy raw TIMG image to raw TEXB image 88 | FixedVertexIndexFormat* Vrtx=VertexIndexUVs[Index]; 89 | uint32_t* texbBmp=reinterpret_cast(RawImage); 90 | uint32_t* rawBmp=reinterpret_cast(Image->RawImage); 91 | Point v[4]={ 92 | {Vrtx->Vertices[0].X/65536,Vrtx->Vertices[0].Y/65536}, 93 | {Vrtx->Vertices[1].X/65536,Vrtx->Vertices[1].Y/65536}, 94 | {Vrtx->Vertices[2].X/65536,Vrtx->Vertices[2].Y/65536}, 95 | {Vrtx->Vertices[3].X/65536,Vrtx->Vertices[3].Y/65536} 96 | }; 97 | UVPoint t[4]={ 98 | {Vrtx->Vertices[0].U/65536.0,Vrtx->Vertices[0].V/65536.0}, 99 | {Vrtx->Vertices[1].U/65536.0,Vrtx->Vertices[1].V/65536.0}, 100 | {Vrtx->Vertices[2].U/65536.0,Vrtx->Vertices[2].V/65536.0}, 101 | {Vrtx->Vertices[3].V/65536.0,Vrtx->Vertices[3].V/65536.0} 102 | }; 103 | 104 | for(uint32_t y=0;yHeight;y++) 105 | { 106 | for(uint32_t x=0;xWidth;x++) 107 | { 108 | UVPoint uv=xy2uv(x,y,v[0],v[1],v[2],v[3],t[0],t[1],t[2],t[3]); 109 | texbBmp[uint32_t(uv.U*Width+0.5)+uint32_t(uv.V*Height+0.5)*Width]=rawBmp[x+y*Image->Width]; 110 | } 111 | } 112 | 113 | return 0; 114 | } 115 | 116 | void TextureBank::ReplaceImage(TextureImage *image, size_t index, bool _) 117 | { 118 | 119 | } 120 | 121 | int32_t TextureBank::DefineImage(const Point* Vertexes,const UVPoint* UVs,std::string Name,uint32_t* Index) 122 | { 123 | if(Index==NULL) return EINVAL; 124 | 125 | uint32_t ImgWidth = Vertexes[2].X - Vertexes[0].X; 126 | uint32_t ImgHeight = Vertexes[2].Y - Vertexes[0].Y; 127 | uint32_t Left = uint32_t(UVs[0].U * Width); 128 | uint32_t Top = uint32_t(UVs[0].V * Height); 129 | uint32_t Right = uint32_t(UVs[2].U * Width); 130 | uint32_t Bottom = uint32_t(UVs[2].V * Height); 131 | 132 | size_t NewIndex = DefineImage(Name, Top, Left, Bottom, Right, ImgWidth, ImgHeight); 133 | *Index = (uint32_t) NewIndex; 134 | return 0; 135 | 136 | return 0; 137 | } 138 | 139 | int32_t TextureBank::DefineImage(const Point* WhereWidthHeight,std::string Name,uint32_t* Index) 140 | { 141 | Point v[4]={ 142 | {0,0}, 143 | {WhereWidthHeight[1].X,0}, 144 | {WhereWidthHeight[1].X,WhereWidthHeight[1].Y}, 145 | {0,WhereWidthHeight[1].Y} 146 | }; 147 | // Applied MiraiNoHana TEXBModify.cpp patch. 148 | UVPoint t[4]={ 149 | {double(WhereWidthHeight[0].X)/double(Width),double(WhereWidthHeight[0].Y)/double(Height)}, 150 | {double(WhereWidthHeight[1].X+WhereWidthHeight[0].X)/double(Width),double(WhereWidthHeight[0].Y)/double(Height)}, 151 | {double(WhereWidthHeight[1].X+WhereWidthHeight[0].X)/double(Width),double(WhereWidthHeight[1].Y+WhereWidthHeight[0].Y)/double(Height)}, 152 | {double(WhereWidthHeight[0].X)/double(Width),double(WhereWidthHeight[1].Y+WhereWidthHeight[0].Y)/double(Height)} 153 | }; 154 | return DefineImage(v,t,Name,Index); 155 | } 156 | 157 | size_t TextureBank::DefineImage(const std::string &name, uint32_t top, uint32_t left, uint32_t bottom, uint32_t right, uint32_t width, uint32_t height) 158 | { 159 | TextureImage* timg = new TextureImage(width, height, TEXB_GET_CHANNEL_KIND(Flags), TEXB_GET_PIXEL_FORMAT(Flags)); 160 | timg->from_texb = this; 161 | timg->Name = Name; 162 | 163 | FixedVertexIndexFormat *VertData = new FixedVertexIndexFormat(); 164 | /* 165 | Layout 166 | 0-----1 167 | | /| 168 | | / | 169 | | / | 170 | | / | 171 | |/ | 172 | 3-----2 173 | */ 174 | VertData->Indices = {0, 1, 3, 3, 1, 2}; 175 | VertData->Vertices = { 176 | VertexFormat({0 , 0 , left * 65536 , top * 65536 }), 177 | VertexFormat({width * 65536, 0 , right * 65536, top * 65536 }), 178 | VertexFormat({width * 65536, height * 65536, right * 65536, bottom * 65536}), 179 | VertexFormat({0 , height * 65536, left * 65536 , bottom * 65536}) 180 | }; 181 | 182 | size_t Index = ImageList_Id.size(); 183 | VertexIndexUVs.push_back(VertData); 184 | ImageList_Id.push_back(timg); 185 | ImageList_Names[Name] = Index; 186 | 187 | return Index; 188 | } 189 | 190 | void TextureBank::ReflectChanges() 191 | { 192 | for(uint32_t i=0;i(RawImage); 197 | uint32_t* rawBmp=reinterpret_cast(timg->RawImage); 198 | Point v[4] = { 199 | {Vi->Vertices[0].X / 65536, Vi->Vertices[0].Y / 65536}, 200 | {Vi->Vertices[1].X / 65536, Vi->Vertices[1].Y / 65536}, 201 | {Vi->Vertices[2].X / 65536, Vi->Vertices[2].Y / 65536}, 202 | {Vi->Vertices[3].X / 65536, Vi->Vertices[3].Y / 65536}, 203 | }; 204 | UVPoint t[4] = { 205 | {Vi->Vertices[0].U / 65536.0, Vi->Vertices[0].V / 65536.0}, 206 | {Vi->Vertices[1].U / 65536.0, Vi->Vertices[1].V / 65536.0}, 207 | {Vi->Vertices[2].U / 65536.0, Vi->Vertices[2].V / 65536.0}, 208 | {Vi->Vertices[3].U / 65536.0, Vi->Vertices[3].V / 65536.0}, 209 | }; 210 | 211 | uint32_t min_x = std::min(v[0].X, std::min(v[1].X, std::min(v[2].X, v[3].X))); 212 | uint32_t min_y = std::min(v[0].Y, std::min(v[1].Y, std::min(v[2].Y, v[3].Y))); 213 | uint32_t max_x = std::max(v[0].X, std::max(v[1].X, std::max(v[2].X, v[3].X))); 214 | uint32_t max_y = std::max(v[0].Y, std::max(v[1].Y, std::max(v[2].Y, v[3].Y))); 215 | 216 | for(uint32_t y = min_y; y < max_y; y++) 217 | { 218 | for(uint32_t x = min_x; x < max_x; x++) 219 | { 220 | UVPoint uv=xy2uv(x, y, v[0], v[1], v[2], v[3], t[0], t[1], t[2], t[3]); 221 | texbBmp[uint32_t(uv.U * Width + 0.5) + uint32_t(uv.V * Height + 0.5) * Width] = rawBmp[x + y * timg->Width]; 222 | } 223 | } 224 | } 225 | } 226 | -------------------------------------------------------------------------------- /src/TEXBPixel.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * TEXBPixel.cpp 3 | * Copy operation of various Byte/Pixel types for TEXB 4 | * 5 | * Part of http://github.com/summertriangle-dev/hatedelay with minor edits to suit libtexb 6 | * License of this (and the original) code are below 7 | * 8 | * Redistribution and use in source and binary forms, with or without 9 | * modification, are permitted provided that the following conditions are met: 10 | * 11 | * 1. Redistributions of source code must retain the above copyright notice, this 12 | * list of conditions and the following disclaimer. 13 | * 2. Redistributions in binary form must reproduce the above copyright notice, 14 | * this list of conditions and the following disclaimer in the documentation 15 | * and/or other materials provided with the distribution. 16 | * 3. The output of the software, and the software itself, will not be used 17 | * for financial gains. This includes, but is not limited to, charging to 18 | * view output generated by the software, charging for source code or object 19 | * code, displaying the output generated by the software/offering the software 20 | * for download alongside advertisements, etc. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS "AS IS" AND 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 24 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR 26 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 27 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 29 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | * 33 | **/ 34 | 35 | #include "TEXB.h" 36 | #include "TEXBPixel.h" 37 | 38 | #include 39 | 40 | #include 41 | 42 | #include 43 | 44 | /* implementations dubious? it is not care either way. */ 45 | void copy_1bpp_luma(uint8_t *raw, int len, uint8_t *output) { 46 | memset(output, 255, len * 4); 47 | for (int i = 0, ctr = 0; i < len; ctr = (++i) * 4) { 48 | output[ctr] = raw[i]; 49 | output[ctr + 1] = raw[i]; 50 | output[ctr + 2] = raw[i]; 51 | } 52 | } 53 | 54 | void copy_1bpp_alpha(uint8_t *raw, int len, uint8_t *output) { 55 | memset(output, 0, len * 4); 56 | for (int i = 0, ctr = 0; i < len; ctr = (++i) * 4) { 57 | output[ctr + 3] = raw[i]; 58 | } 59 | } 60 | 61 | void copy_2bpp_lumalpha(uint8_t *raw, int len, uint8_t *output) { 62 | memset(output, 0, len * 4); 63 | for (int i = 0, ctr = 0; i < len; ctr = (i += 2) * 4) { 64 | output[ctr] = raw[i]; 65 | output[ctr + 1] = raw[i]; 66 | output[ctr + 2] = raw[i]; 67 | output[ctr + 3] = raw[i + 1]; 68 | } 69 | } 70 | 71 | void copy_2bpp_rgb565(uint8_t *raw, int len, uint8_t *output) { 72 | memset(output, 255, len * 4); 73 | unsigned short *pixels = (unsigned short *) raw; 74 | for (int i = 0, ctr = 0; i < len; ctr = (++i) * 4) { 75 | unsigned short pixel = pixels[i]; 76 | uint8_t shift = (pixel & 0xF800) >> 8; 77 | output[ctr] = shift | (shift >> 5); 78 | shift = (pixel & 0x07E0) >> 3; 79 | output[ctr + 1] = shift | (shift >> 6); 80 | shift = (pixel & 0x001F) << 3; 81 | output[ctr + 2] = shift | (shift >> 5); 82 | } 83 | } 84 | 85 | void copy_2bpp_rgba5551(uint8_t *raw, int len, uint8_t *output) { 86 | unsigned short *pixels = (unsigned short *) raw; 87 | for (int i = 0, ctr = 0; i < len; ctr = (++i) * 4) { 88 | unsigned short pixel = pixels[i]; 89 | uint8_t shift = (pixel & 0xF800) >> 8; 90 | output[ctr] = shift | (shift >> 5); 91 | shift = (pixel & 0x07C0) >> 3; 92 | output[ctr + 1] = shift | (shift >> 5); 93 | shift = (pixel & 0x003E) << 2; 94 | output[ctr + 2] = shift | (shift >> 5); 95 | output[ctr + 3] = (pixel % 2)? 255 : 0; 96 | } 97 | } 98 | 99 | void copy_2bpp_rgba4444(uint8_t *raw, int len, uint8_t *output) { 100 | unsigned short *pixels = (unsigned short *) raw; 101 | for (int i = 0, ctr = 0; i < len; ctr = (++i) * 4) { 102 | unsigned short pixel = pixels[i]; 103 | uint8_t shift = (pixel & 0xF000) >> 8; 104 | output[ctr] = shift | (shift >> 4); 105 | shift = (pixel & 0x0F00) >> 4; 106 | output[ctr + 1] = shift | (shift >> 4); 107 | shift = pixel & 0x00F0; 108 | output[ctr + 2] = shift | (shift >> 4); 109 | shift = pixel & 0x000F; 110 | output[ctr + 3] = shift | (shift << 4); 111 | } 112 | } 113 | 114 | void copy_3bpp_rgb(uint8_t *raw, int len, uint8_t *output) { 115 | memset(output, 255, len * 4); 116 | for (int i = 0, ctr = 0; i < len; ctr = (i += 3) * 4) { 117 | output[ctr] = raw[i]; 118 | output[ctr + 1] = raw[i + 1]; 119 | output[ctr + 2] = raw[i + 2]; 120 | } 121 | } 122 | 123 | void convert_map(uint8_t *raw, uint32_t w, uint32_t h, uint16_t texb_flags, uint8_t *output) { 124 | convert_map(raw, w, h, TEXB_GET_CHANNEL_KIND(texb_flags), TEXB_GET_PIXEL_FORMAT(texb_flags), output); 125 | } 126 | 127 | void convert_map(uint8_t *raw, uint32_t w, uint32_t h, TEXB_CHANNEL_KIND img_format, TEXB_PIXEL_FORMAT pix_format, uint8_t *output) { 128 | switch (pix_format) { 129 | case TEXB_PIXEL_FORMAT_BYTE: { 130 | switch (img_format) { 131 | case TEXB_CHANNEL_KIND_LUMINANCE: 132 | copy_1bpp_luma(raw, w * h, output); 133 | break; 134 | case TEXB_CHANNEL_KIND_ALPHA: 135 | copy_1bpp_alpha(raw, w * h, output); 136 | break; 137 | case TEXB_CHANNEL_KIND_LUMINANCE_ALPHA: 138 | copy_2bpp_lumalpha(raw, w * h, output); 139 | break; 140 | case TEXB_CHANNEL_KIND_RGB: 141 | copy_3bpp_rgb(raw, w * h, output); 142 | break; 143 | case TEXB_CHANNEL_KIND_RGBA: 144 | memcpy(output, raw, w * h * 4); 145 | break; 146 | default: 147 | break; 148 | } 149 | 150 | break; 151 | } 152 | case TEXB_PIXEL_FORMAT_RGB565: 153 | copy_2bpp_rgb565(raw, w * h, output); 154 | break; 155 | case TEXB_PIXEL_FORMAT_RGBA5551: 156 | copy_2bpp_rgba5551(raw, w * h, output); 157 | break; 158 | case TEXB_PIXEL_FORMAT_RGBA4444: 159 | copy_2bpp_rgba4444(raw, w * h, output); 160 | break; 161 | default: 162 | break; 163 | } 164 | 165 | return; 166 | } 167 | 168 | UVPoint xy2uv(uint32_t x,uint32_t y,Point v0,Point v1,Point v2,Point v3,UVPoint t0,UVPoint t1,UVPoint t2,UVPoint t3) 169 | { 170 | uint32_t xyw=abs(int( 171 | std::max(v0.X,std::max(v1.X,std::max(v2.X,v3.X))) - 172 | std::min(v0.X,std::min(v1.X,std::min(v2.X,v3.X))) 173 | )); 174 | uint32_t xyh=abs(int( 175 | std::max(v0.Y,std::max(v1.Y,std::max(v2.Y,v3.Y))) - 176 | std::min(v0.Y,std::min(v1.Y,std::min(v2.Y,v3.Y))) 177 | )); 178 | double atotal=xyw*xyh; 179 | double a0 = (x - v0.X) * (y - v0.Y) / atotal; 180 | double a1 = (v1.X - x) * (y - v1.Y) / atotal; 181 | double a2 = (v2.X - x) * (v2.Y - y) / atotal; 182 | double a3 = (x - v3.X) * (v3.Y - y) / atotal; 183 | UVPoint ret; 184 | 185 | ret.U=t2.U * a0 + t3.U * a1 + t0.U * a2 + t1.U * a3; 186 | ret.V=t2.V * a0 + t3.V * a1 + t0.V * a2 + t1.V * a3; 187 | 188 | return ret; 189 | } 190 | 191 | size_t GetBytePerPixel(uint16_t TexbFlags) 192 | { 193 | uint8_t iff=TexbFlags&7; 194 | return GetBytePerPixel(iff, uint8_t(TexbFlags)>>6); 195 | } 196 | 197 | size_t GetBytePerPixel(uint8_t chankind, uint8_t pixfmt) 198 | { 199 | switch(pixfmt) 200 | { 201 | case 0: 202 | case 1: 203 | case 2: 204 | return 2; 205 | case 3: 206 | { 207 | // [0] ALPHA | [1] LUMA | [2] LUMALPHA | [3] RGB | [4] RGBA 208 | switch(chankind) 209 | { 210 | case 0: 211 | return 1; 212 | default: 213 | return chankind; 214 | } 215 | } 216 | default: 217 | return 0; 218 | } 219 | } 220 | -------------------------------------------------------------------------------- /src/TEXBPixel.h: -------------------------------------------------------------------------------- 1 | /** 2 | * TEXBPixel.h 3 | * Forward-declaration of functions in TEXBPixel.cpp 4 | **/ 5 | 6 | #ifndef TEXB_PIXEL_H_ 7 | #define TEXB_PIXEL_H_ 8 | 9 | #include 10 | 11 | #include "TEXB/TEXBLib.h" 12 | 13 | void convert_map(uint8_t *raw, uint32_t w, uint32_t h, uint16_t texb_flags, uint8_t *output); 14 | void convert_map(uint8_t *raw, uint32_t w, uint32_t h, TEXB_CHANNEL_KIND img_format, TEXB_PIXEL_FORMAT pix_format, uint8_t *output); 15 | 16 | void copy_1bpp_luma(uint8_t *raw, int len, uint8_t *output); 17 | void copy_1bpp_alpha(uint8_t *raw, int len, uint8_t *output); 18 | void copy_2bpp_lumalpha(uint8_t *raw, int len, uint8_t *output); 19 | void copy_2bpp_rgb565(uint8_t *raw, int len, uint8_t *output); 20 | void copy_2bpp_rgba5551(uint8_t *raw, int len, uint8_t *output); 21 | void copy_2bpp_rgba4444(uint8_t *raw, int len, uint8_t *output); 22 | void copy_3bpp_rgb(uint8_t *raw, int len, uint8_t *output); 23 | size_t GetBytePerPixel(uint16_t TexbFlags); 24 | size_t GetBytePerPixel(uint8_t iff, uint8_t pixfmt); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /src/TEXBSave.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * TEXBSave.cpp 3 | * Saves TextureBank to TEXB 4 | **/ 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #include 16 | 17 | #include "TEXB.h" 18 | #include "zlib.h" 19 | 20 | inline void ToBE(uint8_t *dest, uint32_t v) 21 | { 22 | dest[0]=v>>24; 23 | dest[1]=(v>>16)&255; 24 | dest[2]=(v>>8)&255; 25 | dest[3]=v&255; 26 | } 27 | 28 | int32_t TextureBank::SaveToMemory(uint8_t*& Memory,size_t* MemorySize,uint32_t CompressLevel) 29 | { 30 | if(MemorySize==NULL) return EINVAL; 31 | if(CompressLevel>9) CompressLevel=9; 32 | 33 | uint16_t temp_short=0; 34 | uint32_t temp_int=0; 35 | uint8_t* temp_buffer; 36 | uint8_t prebuf[4]; 37 | 38 | std::stringstream sstream(std::ios::binary|std::ios::out); 39 | sstream.write("TEXB",4); 40 | sstream.write("\0\0\0\0",4); 41 | 42 | size_t nameSize=Name.length()+7; 43 | temp_short=uint16_t((nameSize+1)/2*2); 44 | prebuf[0]=temp_short>>8; 45 | prebuf[1]=temp_short&255; 46 | sstream.write(reinterpret_cast(prebuf),2); 47 | sstream.put('T'); 48 | sstream.write((Name+".texb").c_str(),nameSize-1); 49 | if(nameSize%2==1) sstream.put(0); 50 | 51 | prebuf[0]=RawImageWidth>>8; 52 | prebuf[1]=RawImageWidth&255; 53 | prebuf[2]=RawImageHeight>>8; 54 | prebuf[3]=RawImageHeight&255; 55 | sstream.write(reinterpret_cast(prebuf),4); 56 | 57 | prebuf[0]=0; 58 | prebuf[1]=CompressLevel>0?204:196; 59 | sstream.write(reinterpret_cast(prebuf),2); 60 | 61 | size_t tImgs=ImageList_Id.size(); 62 | temp_short=uint16_t(tImgs*4); 63 | prebuf[0]=temp_short>>8; 64 | prebuf[1]=temp_short&255; 65 | temp_short=uint16_t(tImgs*6); 66 | prebuf[2]=temp_short>>8; 67 | prebuf[3]=temp_short&255; 68 | sstream.write(reinterpret_cast(prebuf),4); 69 | prebuf[0]=(tImgs>>8)&255; 70 | prebuf[1]=tImgs&255; 71 | sstream.write(reinterpret_cast(prebuf),2); 72 | 73 | for(uint32_t i=0;iName.length()+11; 79 | temp_short=uint16_t((nameSize+1)/2*2); 80 | prebuf[0]=temp_short>>8; 81 | prebuf[1]=temp_short&255; 82 | sstream.write(reinterpret_cast(prebuf),2); 83 | sstream.put('I'); 84 | sstream.write((cur->Name+".png.imag").c_str(),nameSize-1); 85 | if(nameSize%2==1) sstream.put(0); 86 | 87 | sstream.write("\xFF\xFF\x00\x01\x00\x00\x00\x00\x00\x01\x00\x01\x04\x06",14); 88 | prebuf[0]=cur->Width>>8; 89 | prebuf[1]=cur->Width&255; 90 | prebuf[2]=cur->Height>>8; 91 | prebuf[3]=cur->Height&255; 92 | sstream.write(reinterpret_cast(prebuf),4); 93 | sstream.write("\0\0\0\0",4); 94 | 95 | FixedVertexIndexFormat *vrtxCur = VertexIndexUVs[i]; 96 | 97 | for(uint32_t j=0;j<4;j++) 98 | { 99 | ToBE(prebuf, vrtxCur->Vertices[j].X); 100 | sstream.write(reinterpret_cast(prebuf),4); 101 | ToBE(prebuf, vrtxCur->Vertices[j].Y); 102 | sstream.write(reinterpret_cast(prebuf),4); 103 | ToBE(prebuf, vrtxCur->Vertices[j].U); 104 | sstream.write(reinterpret_cast(prebuf),4); 105 | ToBE(prebuf, vrtxCur->Vertices[j].V); 106 | sstream.write(reinterpret_cast(prebuf),4); 107 | } 108 | sstream.write(reinterpret_cast(vrtxCur)+64,6); 109 | 110 | temp_int=(uint32_t)sstream.tellp(); 111 | sstream.seekp(curStreamSize-2,std::ios::beg); 112 | temp_short=uint16_t(temp_int-=(uint32_t)curStreamSize); 113 | prebuf[0]=temp_short>>8; 114 | prebuf[1]=temp_short&255; 115 | sstream.write(reinterpret_cast(prebuf),2); 116 | sstream.seekp(0,std::ios::end); 117 | } 118 | 119 | if(CompressLevel>0) 120 | { 121 | z_stream zstate; 122 | memset(&zstate,0,sizeof(z_stream)); 123 | 124 | temp_int=deflateInit(&zstate,CompressLevel); 125 | if(temp_int==Z_MEM_ERROR) return ENOMEM; 126 | 127 | temp_int=RawImageWidth*RawImageHeight*4; 128 | temp_buffer=LIBTEXB_ALLOC(uint8_t,temp_int); 129 | zstate.avail_in = temp_int; 130 | zstate.next_in = RawImage; 131 | zstate.avail_out = temp_int; 132 | zstate.next_out = temp_buffer; 133 | 134 | deflate(&zstate,Z_FINISH); 135 | deflateEnd(&zstate); 136 | 137 | temp_int-=zstate.avail_out; 138 | sstream.write("\0\0\0\0",4); 139 | sstream.write(reinterpret_cast(temp_buffer),temp_int); 140 | LIBTEXB_FREE(temp_buffer); 141 | } 142 | else 143 | { 144 | temp_int=RawImageWidth*RawImageHeight*4; 145 | sstream.write(reinterpret_cast(RawImage),temp_int); 146 | } 147 | 148 | temp_int=static_cast(sstream.tellp())-8; 149 | prebuf[0]=temp_int>>24; 150 | prebuf[1]=(temp_int>>16)&255; 151 | prebuf[2]=(temp_int>>8)&255; 152 | prebuf[3]=temp_int&255; 153 | sstream.seekp(4,std::ios::beg); 154 | sstream.write(reinterpret_cast(prebuf),4); 155 | 156 | sstream.seekp(0,std::ios::end); 157 | temp_int=(uint32_t)sstream.tellp(); 158 | temp_buffer=LIBTEXB_ALLOC(uint8_t,temp_int); 159 | memcpy(temp_buffer,sstream.str().c_str(),temp_int); 160 | Memory=temp_buffer; 161 | *MemorySize=temp_int; 162 | 163 | return 0; 164 | } 165 | 166 | inline int32_t TextureBank::SaveToMemory(uint8_t** Memory,size_t* MemorySize,uint32_t CompressLevel) 167 | { 168 | return SaveToMemory(*Memory,MemorySize,CompressLevel); 169 | } 170 | 171 | int32_t TextureBank::SaveToFile(std::string Filename,uint32_t CompressLevel) 172 | { 173 | FILE* fptr; 174 | uint8_t* temp_buffer; 175 | size_t bufsize; 176 | uint32_t ret; 177 | 178 | fptr=fopen(Filename.c_str(),"wb"); 179 | if(fptr==NULL) return errno; 180 | 181 | ret=SaveToMemory(&temp_buffer,&bufsize,CompressLevel); 182 | if(ret==0) 183 | { 184 | fwrite(temp_buffer,1,bufsize,fptr); 185 | fclose(fptr); 186 | } 187 | 188 | LIBTEXB_FREE(temp_buffer); 189 | return ret; 190 | } 191 | -------------------------------------------------------------------------------- /src/TEXBVersion.h.in: -------------------------------------------------------------------------------- 1 | #ifndef _TEXB_VERSION_H_ 2 | #define _TEXB_VERSION_H_ 3 | 4 | #define LIBTEXB_VERSION "@LIBTEXB_VERSION@" 5 | #define LIBTEXB_VERSION_MAJOR @LIBTEXB_VERSION_MAJOR@ 6 | #define LIBTEXB_VERSION_MINOR @LIBTEXB_VERSION_MINOR@ 7 | #define LIBTEXB_VERSION_PATCH @LIBTEXB_VERSION_PATCH@ 8 | #define LIBTEXB_VERSION_NUM ((LIBTEXB_VERSION_MAJOR << 18) | (LIBTEXB_VERSION_MINOR << 9) | LIBTEXB_VERSION_PATCH) 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /src/TIMG.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * TIMG.cpp 3 | * TextureImage class implementation 4 | **/ 5 | 6 | #include "TEXB.h" 7 | #include "TEXBPixel.h" 8 | 9 | #include 10 | #include 11 | 12 | TextureImage::TextureImage(uint32_t width,uint32_t height,uint8_t* raw_image) 13 | : TextureImage(width, height, TEXB_CHANNEL_KIND_RGBA, TEXB_PIXEL_FORMAT_BYTE, raw_image) 14 | { } 15 | 16 | TextureImage::TextureImage(uint32_t width, uint32_t height, TEXB_CHANNEL_KIND channelkind, TEXB_PIXEL_FORMAT pixfmt, uint8_t *pixels) 17 | : from_texb(nullptr) 18 | , Width(width) 19 | , Height(height) 20 | , Name("") 21 | , ChannelKind(channelkind) 22 | , PixelFormat(pixfmt) 23 | { 24 | size_t srcSize = width * height * GetBytePerPixel((uint8_t) channelkind, (uint8_t) pixfmt); 25 | size_t size = width * height * 4; 26 | 27 | RawImage = new uint8_t[size]; 28 | 29 | if (pixels) 30 | convert_map(pixels, width, height, channelkind, pixfmt, RawImage); 31 | else 32 | memset(RawImage, 255, size); 33 | } 34 | 35 | TextureImage::~TextureImage() 36 | { 37 | LIBTEXB_FREE(RawImage); 38 | } 39 | 40 | TextureImage* TextureImage::Clone() 41 | { 42 | TextureImage* timg = new TextureImage(Width, Height, ChannelKind, PixelFormat, RawImage); 43 | timg->Name=Name; 44 | return timg; 45 | } 46 | -------------------------------------------------------------------------------- /src/xy2uv.h: -------------------------------------------------------------------------------- 1 | /** 2 | * xy2uv.h 3 | * Coordinate conversion 4 | **/ 5 | 6 | #include "TEXB.h" 7 | 8 | #include 9 | 10 | UVPoint xy2uv(uint32_t x,uint32_t y,Point v0,Point v1,Point v2,Point v3,UVPoint t0,UVPoint t1,UVPoint t2,UVPoint t3); 11 | -------------------------------------------------------------------------------- /tclap-1.2.1/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 | -------------------------------------------------------------------------------- /tclap-1.2.1/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 | virtual ~ValueLike() {} 41 | }; 42 | 43 | /** 44 | * A string like argument value type is a value that can be set using 45 | * operator=(string). Usefull if the value type contains spaces which 46 | * will be broken up into individual tokens by operator>>. 47 | */ 48 | struct StringLike { 49 | virtual ~StringLike() {} 50 | }; 51 | 52 | /** 53 | * A class can inherit from this object to make it have string like 54 | * traits. This is a compile time thing and does not add any overhead 55 | * to the inherenting class. 56 | */ 57 | struct StringLikeTrait { 58 | typedef StringLike ValueCategory; 59 | virtual ~StringLikeTrait() {} 60 | }; 61 | 62 | /** 63 | * A class can inherit from this object to make it have value like 64 | * traits. This is a compile time thing and does not add any overhead 65 | * to the inherenting class. 66 | */ 67 | struct ValueLikeTrait { 68 | typedef ValueLike ValueCategory; 69 | virtual ~ValueLikeTrait() {} 70 | }; 71 | 72 | /** 73 | * Arg traits are used to get compile type specialization when parsing 74 | * argument values. Using an ArgTraits you can specify the way that 75 | * values gets assigned to any particular type during parsing. The two 76 | * supported types are StringLike and ValueLike. 77 | */ 78 | template 79 | struct ArgTraits { 80 | typedef typename T::ValueCategory ValueCategory; 81 | virtual ~ArgTraits() {} 82 | //typedef ValueLike ValueCategory; 83 | }; 84 | 85 | #endif 86 | 87 | } // namespace 88 | -------------------------------------------------------------------------------- /tclap-1.2.1/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 | -------------------------------------------------------------------------------- /tclap-1.2.1/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 | -------------------------------------------------------------------------------- /tclap-1.2.1/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 | -------------------------------------------------------------------------------- /tclap-1.2.1/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 xversion = _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 << xversion << 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 << "" << std::endl; 269 | std::cout << "" << std::endl; 270 | } 271 | 272 | std::cout << "" << std::endl; 273 | std::cout << "" << std::endl; 287 | std::cout << "" << std::endl; 288 | 289 | std::cout << "" << std::endl; 290 | std::cout << "" << std::endl; 291 | std::cout << desc << std::endl; 292 | std::cout << "" << std::endl; 293 | std::cout << "" << std::endl; 294 | 295 | std::cout << "" << std::endl; 296 | } 297 | 298 | } //namespace TCLAP 299 | #endif 300 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/HelpVisitor.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: HelpVisitor.h 5 | * 6 | * Copyright (c) 2003, 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_HELP_VISITOR_H 23 | #define TCLAP_HELP_VISITOR_H 24 | 25 | #include 26 | #include 27 | #include 28 | 29 | namespace TCLAP { 30 | 31 | /** 32 | * A Visitor object that calls the usage method of the given CmdLineOutput 33 | * object for the specified CmdLine object. 34 | */ 35 | class HelpVisitor: public Visitor 36 | { 37 | private: 38 | /** 39 | * Prevent accidental copying. 40 | */ 41 | HelpVisitor(const HelpVisitor& rhs); 42 | HelpVisitor& operator=(const HelpVisitor& rhs); 43 | 44 | protected: 45 | 46 | /** 47 | * The CmdLine the output will be generated for. 48 | */ 49 | CmdLineInterface* _cmd; 50 | 51 | /** 52 | * The output object. 53 | */ 54 | CmdLineOutput** _out; 55 | 56 | public: 57 | 58 | /** 59 | * Constructor. 60 | * \param cmd - The CmdLine the output will be generated for. 61 | * \param out - The type of output. 62 | */ 63 | HelpVisitor(CmdLineInterface* cmd, CmdLineOutput** out) 64 | : Visitor(), _cmd( cmd ), _out( out ) { } 65 | 66 | /** 67 | * Calls the usage method of the CmdLineOutput for the 68 | * specified CmdLine. 69 | */ 70 | void visit() { (*_out)->usage(*_cmd); throw ExitException(0); } 71 | 72 | }; 73 | 74 | } 75 | 76 | #endif 77 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/IgnoreRestVisitor.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: IgnoreRestVisitor.h 5 | * 6 | * Copyright (c) 2003, 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 | 23 | #ifndef TCLAP_IGNORE_REST_VISITOR_H 24 | #define TCLAP_IGNORE_REST_VISITOR_H 25 | 26 | #include 27 | #include 28 | 29 | namespace TCLAP { 30 | 31 | /** 32 | * A Vistor that tells the CmdLine to begin ignoring arguments after 33 | * this one is parsed. 34 | */ 35 | class IgnoreRestVisitor: public Visitor 36 | { 37 | public: 38 | 39 | /** 40 | * Constructor. 41 | */ 42 | IgnoreRestVisitor() : Visitor() {} 43 | 44 | /** 45 | * Sets Arg::_ignoreRest. 46 | */ 47 | void visit() { Arg::beginIgnoring(); } 48 | }; 49 | 50 | } 51 | 52 | #endif 53 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/MultiArg.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * file: MultiArg.h 4 | * 5 | * Copyright (c) 2003, Michael E. Smoot . 6 | * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno. 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 | 23 | #ifndef TCLAP_MULTIPLE_ARGUMENT_H 24 | #define TCLAP_MULTIPLE_ARGUMENT_H 25 | 26 | #include 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | namespace TCLAP { 33 | /** 34 | * An argument that allows multiple values of type T to be specified. Very 35 | * similar to a ValueArg, except a vector of values will be returned 36 | * instead of just one. 37 | */ 38 | template 39 | class MultiArg : public Arg 40 | { 41 | public: 42 | typedef std::vector container_type; 43 | typedef typename container_type::iterator iterator; 44 | typedef typename container_type::const_iterator const_iterator; 45 | 46 | protected: 47 | 48 | /** 49 | * The list of values parsed from the CmdLine. 50 | */ 51 | std::vector _values; 52 | 53 | /** 54 | * The description of type T to be used in the usage. 55 | */ 56 | std::string _typeDesc; 57 | 58 | /** 59 | * A list of constraint on this Arg. 60 | */ 61 | Constraint* _constraint; 62 | 63 | /** 64 | * Extracts the value from the string. 65 | * Attempts to parse string as type T, if this fails an exception 66 | * is thrown. 67 | * \param val - The string to be read. 68 | */ 69 | void _extractValue( const std::string& val ); 70 | 71 | /** 72 | * Used by XorHandler to decide whether to keep parsing for this arg. 73 | */ 74 | bool _allowMore; 75 | 76 | public: 77 | 78 | /** 79 | * Constructor. 80 | * \param flag - The one character flag that identifies this 81 | * argument on the command line. 82 | * \param name - A one word name for the argument. Can be 83 | * used as a long flag on the command line. 84 | * \param desc - A description of what the argument is for or 85 | * does. 86 | * \param req - Whether the argument is required on the command 87 | * line. 88 | * \param typeDesc - A short, human readable description of the 89 | * type that this object expects. This is used in the generation 90 | * of the USAGE statement. The goal is to be helpful to the end user 91 | * of the program. 92 | * \param v - An optional visitor. You probably should not 93 | * use this unless you have a very good reason. 94 | */ 95 | MultiArg( const std::string& flag, 96 | const std::string& name, 97 | const std::string& desc, 98 | bool req, 99 | const std::string& typeDesc, 100 | Visitor* v = NULL); 101 | 102 | /** 103 | * Constructor. 104 | * \param flag - The one character flag that identifies this 105 | * argument on the command line. 106 | * \param name - A one word name for the argument. Can be 107 | * used as a long flag on the command line. 108 | * \param desc - A description of what the argument is for or 109 | * does. 110 | * \param req - Whether the argument is required on the command 111 | * line. 112 | * \param typeDesc - A short, human readable description of the 113 | * type that this object expects. This is used in the generation 114 | * of the USAGE statement. The goal is to be helpful to the end user 115 | * of the program. 116 | * \param parser - A CmdLine parser object to add this Arg to 117 | * \param v - An optional visitor. You probably should not 118 | * use this unless you have a very good reason. 119 | */ 120 | MultiArg( const std::string& flag, 121 | const std::string& name, 122 | const std::string& desc, 123 | bool req, 124 | const std::string& typeDesc, 125 | CmdLineInterface& parser, 126 | Visitor* v = NULL ); 127 | 128 | /** 129 | * Constructor. 130 | * \param flag - The one character flag that identifies this 131 | * argument on the command line. 132 | * \param name - A one word name for the argument. Can be 133 | * used as a long flag on the command line. 134 | * \param desc - A description of what the argument is for or 135 | * does. 136 | * \param req - Whether the argument is required on the command 137 | * line. 138 | * \param constraint - A pointer to a Constraint object used 139 | * to constrain this Arg. 140 | * \param v - An optional visitor. You probably should not 141 | * use this unless you have a very good reason. 142 | */ 143 | MultiArg( const std::string& flag, 144 | const std::string& name, 145 | const std::string& desc, 146 | bool req, 147 | Constraint* constraint, 148 | Visitor* v = NULL ); 149 | 150 | /** 151 | * Constructor. 152 | * \param flag - The one character flag that identifies this 153 | * argument on the command line. 154 | * \param name - A one word name for the argument. Can be 155 | * used as a long flag on the command line. 156 | * \param desc - A description of what the argument is for or 157 | * does. 158 | * \param req - Whether the argument is required on the command 159 | * line. 160 | * \param constraint - A pointer to a Constraint object used 161 | * to constrain this Arg. 162 | * \param parser - A CmdLine parser object to add this Arg to 163 | * \param v - An optional visitor. You probably should not 164 | * use this unless you have a very good reason. 165 | */ 166 | MultiArg( const std::string& flag, 167 | const std::string& name, 168 | const std::string& desc, 169 | bool req, 170 | Constraint* constraint, 171 | CmdLineInterface& parser, 172 | Visitor* v = NULL ); 173 | 174 | /** 175 | * Handles the processing of the argument. 176 | * This re-implements the Arg version of this method to set the 177 | * _value of the argument appropriately. It knows the difference 178 | * between labeled and unlabeled. 179 | * \param i - Pointer the the current argument in the list. 180 | * \param args - Mutable list of strings. Passed from main(). 181 | */ 182 | virtual bool processArg(int* i, std::vector& args); 183 | 184 | /** 185 | * Returns a vector of type T containing the values parsed from 186 | * the command line. 187 | */ 188 | const std::vector& getValue(); 189 | 190 | /** 191 | * Returns an iterator over the values parsed from the command 192 | * line. 193 | */ 194 | const_iterator begin() const { return _values.begin(); } 195 | 196 | /** 197 | * Returns the end of the values parsed from the command 198 | * line. 199 | */ 200 | const_iterator end() const { return _values.end(); } 201 | 202 | /** 203 | * Returns the a short id string. Used in the usage. 204 | * \param val - value to be used. 205 | */ 206 | virtual std::string shortID(const std::string& val="val") const; 207 | 208 | /** 209 | * Returns the a long id string. Used in the usage. 210 | * \param val - value to be used. 211 | */ 212 | virtual std::string longID(const std::string& val="val") const; 213 | 214 | /** 215 | * Once we've matched the first value, then the arg is no longer 216 | * required. 217 | */ 218 | virtual bool isRequired() const; 219 | 220 | virtual bool allowMore(); 221 | 222 | virtual void reset(); 223 | 224 | private: 225 | /** 226 | * Prevent accidental copying 227 | */ 228 | MultiArg(const MultiArg& rhs); 229 | MultiArg& operator=(const MultiArg& rhs); 230 | 231 | }; 232 | 233 | template 234 | MultiArg::MultiArg(const std::string& flag, 235 | const std::string& name, 236 | const std::string& desc, 237 | bool req, 238 | const std::string& typeDesc, 239 | Visitor* v) : 240 | Arg( flag, name, desc, req, true, v ), 241 | _values(std::vector()), 242 | _typeDesc( typeDesc ), 243 | _constraint( NULL ), 244 | _allowMore(false) 245 | { 246 | _acceptsMultipleValues = true; 247 | } 248 | 249 | template 250 | MultiArg::MultiArg(const std::string& flag, 251 | const std::string& name, 252 | const std::string& desc, 253 | bool req, 254 | const std::string& typeDesc, 255 | CmdLineInterface& parser, 256 | Visitor* v) 257 | : Arg( flag, name, desc, req, true, v ), 258 | _values(std::vector()), 259 | _typeDesc( typeDesc ), 260 | _constraint( NULL ), 261 | _allowMore(false) 262 | { 263 | parser.add( this ); 264 | _acceptsMultipleValues = true; 265 | } 266 | 267 | /** 268 | * 269 | */ 270 | template 271 | MultiArg::MultiArg(const std::string& flag, 272 | const std::string& name, 273 | const std::string& desc, 274 | bool req, 275 | Constraint* constraint, 276 | Visitor* v) 277 | : Arg( flag, name, desc, req, true, v ), 278 | _values(std::vector()), 279 | _typeDesc( constraint->shortID() ), 280 | _constraint( constraint ), 281 | _allowMore(false) 282 | { 283 | _acceptsMultipleValues = true; 284 | } 285 | 286 | template 287 | MultiArg::MultiArg(const std::string& flag, 288 | const std::string& name, 289 | const std::string& desc, 290 | bool req, 291 | Constraint* constraint, 292 | CmdLineInterface& parser, 293 | Visitor* v) 294 | : Arg( flag, name, desc, req, true, v ), 295 | _values(std::vector()), 296 | _typeDesc( constraint->shortID() ), 297 | _constraint( constraint ), 298 | _allowMore(false) 299 | { 300 | parser.add( this ); 301 | _acceptsMultipleValues = true; 302 | } 303 | 304 | template 305 | const std::vector& MultiArg::getValue() { return _values; } 306 | 307 | template 308 | bool MultiArg::processArg(int *i, std::vector& args) 309 | { 310 | if ( _ignoreable && Arg::ignoreRest() ) 311 | return false; 312 | 313 | if ( _hasBlanks( args[*i] ) ) 314 | return false; 315 | 316 | std::string flag = args[*i]; 317 | std::string value = ""; 318 | 319 | trimFlag( flag, value ); 320 | 321 | if ( argMatches( flag ) ) 322 | { 323 | if ( Arg::delimiter() != ' ' && value == "" ) 324 | throw( ArgParseException( 325 | "Couldn't find delimiter for this argument!", 326 | toString() ) ); 327 | 328 | // always take the first one, regardless of start string 329 | if ( value == "" ) 330 | { 331 | (*i)++; 332 | if ( static_cast(*i) < args.size() ) 333 | _extractValue( args[*i] ); 334 | else 335 | throw( ArgParseException("Missing a value for this argument!", 336 | toString() ) ); 337 | } 338 | else 339 | _extractValue( value ); 340 | 341 | /* 342 | // continuing taking the args until we hit one with a start string 343 | while ( (unsigned int)(*i)+1 < args.size() && 344 | args[(*i)+1].find_first_of( Arg::flagStartString() ) != 0 && 345 | args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 ) 346 | _extractValue( args[++(*i)] ); 347 | */ 348 | 349 | _alreadySet = true; 350 | _checkWithVisitor(); 351 | 352 | return true; 353 | } 354 | else 355 | return false; 356 | } 357 | 358 | /** 359 | * 360 | */ 361 | template 362 | std::string MultiArg::shortID(const std::string& val) const 363 | { 364 | static_cast(val); // Ignore input, don't warn 365 | return Arg::shortID(_typeDesc) + " ... "; 366 | } 367 | 368 | /** 369 | * 370 | */ 371 | template 372 | std::string MultiArg::longID(const std::string& val) const 373 | { 374 | static_cast(val); // Ignore input, don't warn 375 | return Arg::longID(_typeDesc) + " (accepted multiple times)"; 376 | } 377 | 378 | /** 379 | * Once we've matched the first value, then the arg is no longer 380 | * required. 381 | */ 382 | template 383 | bool MultiArg::isRequired() const 384 | { 385 | if ( _required ) 386 | { 387 | if ( _values.size() > 1 ) 388 | return false; 389 | else 390 | return true; 391 | } 392 | else 393 | return false; 394 | 395 | } 396 | 397 | template 398 | void MultiArg::_extractValue( const std::string& val ) 399 | { 400 | try { 401 | T tmp; 402 | ExtractValue(tmp, val, typename ArgTraits::ValueCategory()); 403 | _values.push_back(tmp); 404 | } catch( ArgParseException &e) { 405 | throw ArgParseException(e.error(), toString()); 406 | } 407 | 408 | if ( _constraint != NULL ) 409 | if ( ! _constraint->check( _values.back() ) ) 410 | throw( CmdLineParseException( "Value '" + val + 411 | "' does not meet constraint: " + 412 | _constraint->description(), 413 | toString() ) ); 414 | } 415 | 416 | template 417 | bool MultiArg::allowMore() 418 | { 419 | bool am = _allowMore; 420 | _allowMore = true; 421 | return am; 422 | } 423 | 424 | template 425 | void MultiArg::reset() 426 | { 427 | Arg::reset(); 428 | _values.clear(); 429 | } 430 | 431 | } // namespace TCLAP 432 | 433 | #endif 434 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/MultiSwitchArg.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: MultiSwitchArg.h 5 | * 6 | * Copyright (c) 2003, Michael E. Smoot . 7 | * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno. 8 | * Copyright (c) 2005, Michael E. Smoot, Daniel Aarno, Erik Zeek. 9 | * All rights reverved. 10 | * 11 | * See the file COPYING in the top directory of this distribution for 12 | * more information. 13 | * 14 | * THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20 | * DEALINGS IN THE SOFTWARE. 21 | * 22 | *****************************************************************************/ 23 | 24 | 25 | #ifndef TCLAP_MULTI_SWITCH_ARG_H 26 | #define TCLAP_MULTI_SWITCH_ARG_H 27 | 28 | #include 29 | #include 30 | 31 | #include 32 | 33 | namespace TCLAP { 34 | 35 | /** 36 | * A multiple switch argument. If the switch is set on the command line, then 37 | * the getValue method will return the number of times the switch appears. 38 | */ 39 | class MultiSwitchArg : public SwitchArg 40 | { 41 | protected: 42 | 43 | /** 44 | * The value of the switch. 45 | */ 46 | int _value; 47 | 48 | /** 49 | * Used to support the reset() method so that ValueArg can be 50 | * reset to their constructed value. 51 | */ 52 | int _default; 53 | 54 | public: 55 | 56 | /** 57 | * MultiSwitchArg constructor. 58 | * \param flag - The one character flag that identifies this 59 | * argument on the command line. 60 | * \param name - A one word name for the argument. Can be 61 | * used as a long flag on the command line. 62 | * \param desc - A description of what the argument is for or 63 | * does. 64 | * \param init - Optional. The initial/default value of this Arg. 65 | * Defaults to 0. 66 | * \param v - An optional visitor. You probably should not 67 | * use this unless you have a very good reason. 68 | */ 69 | MultiSwitchArg(const std::string& flag, 70 | const std::string& name, 71 | const std::string& desc, 72 | int init = 0, 73 | Visitor* v = NULL); 74 | 75 | 76 | /** 77 | * MultiSwitchArg constructor. 78 | * \param flag - The one character flag that identifies this 79 | * argument on the command line. 80 | * \param name - A one word name for the argument. Can be 81 | * used as a long flag on the command line. 82 | * \param desc - A description of what the argument is for or 83 | * does. 84 | * \param parser - A CmdLine parser object to add this Arg to 85 | * \param init - Optional. The initial/default value of this Arg. 86 | * Defaults to 0. 87 | * \param v - An optional visitor. You probably should not 88 | * use this unless you have a very good reason. 89 | */ 90 | MultiSwitchArg(const std::string& flag, 91 | const std::string& name, 92 | const std::string& desc, 93 | CmdLineInterface& parser, 94 | int init = 0, 95 | Visitor* v = NULL); 96 | 97 | 98 | /** 99 | * Handles the processing of the argument. 100 | * This re-implements the SwitchArg version of this method to set the 101 | * _value of the argument appropriately. 102 | * \param i - Pointer the the current argument in the list. 103 | * \param args - Mutable list of strings. Passed 104 | * in from main(). 105 | */ 106 | virtual bool processArg(int* i, std::vector& args); 107 | 108 | /** 109 | * Returns int, the number of times the switch has been set. 110 | */ 111 | int getValue(); 112 | 113 | /** 114 | * Returns the shortID for this Arg. 115 | */ 116 | std::string shortID(const std::string& val) const; 117 | 118 | /** 119 | * Returns the longID for this Arg. 120 | */ 121 | std::string longID(const std::string& val) const; 122 | 123 | void reset(); 124 | 125 | }; 126 | 127 | ////////////////////////////////////////////////////////////////////// 128 | //BEGIN MultiSwitchArg.cpp 129 | ////////////////////////////////////////////////////////////////////// 130 | inline MultiSwitchArg::MultiSwitchArg(const std::string& flag, 131 | const std::string& name, 132 | const std::string& desc, 133 | int init, 134 | Visitor* v ) 135 | : SwitchArg(flag, name, desc, false, v), 136 | _value( init ), 137 | _default( init ) 138 | { } 139 | 140 | inline MultiSwitchArg::MultiSwitchArg(const std::string& flag, 141 | const std::string& name, 142 | const std::string& desc, 143 | CmdLineInterface& parser, 144 | int init, 145 | Visitor* v ) 146 | : SwitchArg(flag, name, desc, false, v), 147 | _value( init ), 148 | _default( init ) 149 | { 150 | parser.add( this ); 151 | } 152 | 153 | inline int MultiSwitchArg::getValue() { return _value; } 154 | 155 | inline bool MultiSwitchArg::processArg(int *i, std::vector& args) 156 | { 157 | if ( _ignoreable && Arg::ignoreRest() ) 158 | return false; 159 | 160 | if ( argMatches( args[*i] )) 161 | { 162 | // so the isSet() method will work 163 | _alreadySet = true; 164 | 165 | // Matched argument: increment value. 166 | ++_value; 167 | 168 | _checkWithVisitor(); 169 | 170 | return true; 171 | } 172 | else if ( combinedSwitchesMatch( args[*i] ) ) 173 | { 174 | // so the isSet() method will work 175 | _alreadySet = true; 176 | 177 | // Matched argument: increment value. 178 | ++_value; 179 | 180 | // Check for more in argument and increment value. 181 | while ( combinedSwitchesMatch( args[*i] ) ) 182 | ++_value; 183 | 184 | _checkWithVisitor(); 185 | 186 | return false; 187 | } 188 | else 189 | return false; 190 | } 191 | 192 | inline std::string 193 | MultiSwitchArg::shortID(const std::string& val) const 194 | { 195 | return Arg::shortID(val) + " ... "; 196 | } 197 | 198 | inline std::string 199 | MultiSwitchArg::longID(const std::string& val) const 200 | { 201 | return Arg::longID(val) + " (accepted multiple times)"; 202 | } 203 | 204 | inline void 205 | MultiSwitchArg::reset() 206 | { 207 | MultiSwitchArg::_value = MultiSwitchArg::_default; 208 | } 209 | 210 | ////////////////////////////////////////////////////////////////////// 211 | //END MultiSwitchArg.cpp 212 | ////////////////////////////////////////////////////////////////////// 213 | 214 | } //namespace TCLAP 215 | 216 | #endif 217 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/OptionalUnlabeledTracker.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | /****************************************************************************** 4 | * 5 | * file: OptionalUnlabeledTracker.h 6 | * 7 | * Copyright (c) 2005, 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_OPTIONAL_UNLABELED_TRACKER_H 25 | #define TCLAP_OPTIONAL_UNLABELED_TRACKER_H 26 | 27 | #include 28 | 29 | namespace TCLAP { 30 | 31 | class OptionalUnlabeledTracker 32 | { 33 | 34 | public: 35 | 36 | static void check( bool req, const std::string& argName ); 37 | 38 | static void gotOptional() { alreadyOptionalRef() = true; } 39 | 40 | static bool& alreadyOptional() { return alreadyOptionalRef(); } 41 | 42 | private: 43 | 44 | static bool& alreadyOptionalRef() { static bool ct = false; return ct; } 45 | }; 46 | 47 | 48 | inline void OptionalUnlabeledTracker::check( bool req, const std::string& argName ) 49 | { 50 | if ( OptionalUnlabeledTracker::alreadyOptional() ) 51 | throw( SpecificationException( 52 | "You can't specify ANY Unlabeled Arg following an optional Unlabeled Arg", 53 | argName ) ); 54 | 55 | if ( !req ) 56 | OptionalUnlabeledTracker::gotOptional(); 57 | } 58 | 59 | 60 | } // namespace TCLAP 61 | 62 | #endif 63 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/StandardTraits.h: -------------------------------------------------------------------------------- 1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- 2 | 3 | /****************************************************************************** 4 | * 5 | * file: StandardTraits.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_STANDARD_TRAITS_H 27 | #define TCLAP_STANDARD_TRAITS_H 28 | 29 | #ifdef HAVE_CONFIG_H 30 | #include // To check for long long 31 | #endif 32 | 33 | // If Microsoft has already typedef'd wchar_t as an unsigned 34 | // short, then compiles will break because it's as if we're 35 | // creating ArgTraits twice for unsigned short. Thus... 36 | #ifdef _MSC_VER 37 | #ifndef _NATIVE_WCHAR_T_DEFINED 38 | #define TCLAP_DONT_DECLARE_WCHAR_T_ARGTRAITS 39 | #endif 40 | #endif 41 | 42 | namespace TCLAP { 43 | 44 | // ====================================================================== 45 | // Integer types 46 | // ====================================================================== 47 | 48 | /** 49 | * longs have value-like semantics. 50 | */ 51 | template<> 52 | struct ArgTraits { 53 | typedef ValueLike ValueCategory; 54 | }; 55 | 56 | /** 57 | * ints have value-like semantics. 58 | */ 59 | template<> 60 | struct ArgTraits { 61 | typedef ValueLike ValueCategory; 62 | }; 63 | 64 | /** 65 | * shorts have value-like semantics. 66 | */ 67 | template<> 68 | struct ArgTraits { 69 | typedef ValueLike ValueCategory; 70 | }; 71 | 72 | /** 73 | * chars have value-like semantics. 74 | */ 75 | template<> 76 | struct ArgTraits { 77 | typedef ValueLike ValueCategory; 78 | }; 79 | 80 | #ifdef HAVE_LONG_LONG 81 | /** 82 | * long longs have value-like semantics. 83 | */ 84 | template<> 85 | struct ArgTraits { 86 | typedef ValueLike ValueCategory; 87 | }; 88 | #endif 89 | 90 | // ====================================================================== 91 | // Unsigned integer types 92 | // ====================================================================== 93 | 94 | /** 95 | * unsigned longs have value-like semantics. 96 | */ 97 | template<> 98 | struct ArgTraits { 99 | typedef ValueLike ValueCategory; 100 | }; 101 | 102 | /** 103 | * unsigned ints have value-like semantics. 104 | */ 105 | template<> 106 | struct ArgTraits { 107 | typedef ValueLike ValueCategory; 108 | }; 109 | 110 | /** 111 | * unsigned shorts have value-like semantics. 112 | */ 113 | template<> 114 | struct ArgTraits { 115 | typedef ValueLike ValueCategory; 116 | }; 117 | 118 | /** 119 | * unsigned chars have value-like semantics. 120 | */ 121 | template<> 122 | struct ArgTraits { 123 | typedef ValueLike ValueCategory; 124 | }; 125 | 126 | // Microsoft implements size_t awkwardly. 127 | #if defined(_MSC_VER) && defined(_M_X64) 128 | /** 129 | * size_ts have value-like semantics. 130 | */ 131 | template<> 132 | struct ArgTraits { 133 | typedef ValueLike ValueCategory; 134 | }; 135 | #endif 136 | 137 | 138 | #ifdef HAVE_LONG_LONG 139 | /** 140 | * unsigned long longs have value-like semantics. 141 | */ 142 | template<> 143 | struct ArgTraits { 144 | typedef ValueLike ValueCategory; 145 | }; 146 | #endif 147 | 148 | // ====================================================================== 149 | // Float types 150 | // ====================================================================== 151 | 152 | /** 153 | * floats have value-like semantics. 154 | */ 155 | template<> 156 | struct ArgTraits { 157 | typedef ValueLike ValueCategory; 158 | }; 159 | 160 | /** 161 | * doubles have value-like semantics. 162 | */ 163 | template<> 164 | struct ArgTraits { 165 | typedef ValueLike ValueCategory; 166 | }; 167 | 168 | // ====================================================================== 169 | // Other types 170 | // ====================================================================== 171 | 172 | /** 173 | * bools have value-like semantics. 174 | */ 175 | template<> 176 | struct ArgTraits { 177 | typedef ValueLike ValueCategory; 178 | }; 179 | 180 | 181 | /** 182 | * wchar_ts have value-like semantics. 183 | */ 184 | #ifndef TCLAP_DONT_DECLARE_WCHAR_T_ARGTRAITS 185 | template<> 186 | struct ArgTraits { 187 | typedef ValueLike ValueCategory; 188 | }; 189 | #endif 190 | 191 | /** 192 | * Strings have string like argument traits. 193 | */ 194 | template<> 195 | struct ArgTraits { 196 | typedef StringLike ValueCategory; 197 | }; 198 | 199 | template 200 | void SetString(T &dst, const std::string &src) 201 | { 202 | dst = src; 203 | } 204 | 205 | } // namespace 206 | 207 | #endif 208 | 209 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/StdOutput.h: -------------------------------------------------------------------------------- 1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- 2 | 3 | /****************************************************************************** 4 | * 5 | * file: StdOutput.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_STDCMDLINEOUTPUT_H 24 | #define TCLAP_STDCMDLINEOUTPUT_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 isolates any output from the CmdLine object so that it 41 | * may be easily modified. 42 | */ 43 | class StdOutput : 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 | * Writes a brief usage message with short args. 75 | * \param c - The CmdLine object the output is generated for. 76 | * \param os - The stream to write the message to. 77 | */ 78 | void _shortUsage( CmdLineInterface& c, std::ostream& os ) const; 79 | 80 | /** 81 | * Writes a longer usage message with long and short args, 82 | * provides descriptions and prints message. 83 | * \param c - The CmdLine object the output is generated for. 84 | * \param os - The stream to write the message to. 85 | */ 86 | void _longUsage( CmdLineInterface& c, std::ostream& os ) const; 87 | 88 | /** 89 | * This function inserts line breaks and indents long strings 90 | * according the params input. It will only break lines at spaces, 91 | * commas and pipes. 92 | * \param os - The stream to be printed to. 93 | * \param s - The string to be printed. 94 | * \param maxWidth - The maxWidth allowed for the output line. 95 | * \param indentSpaces - The number of spaces to indent the first line. 96 | * \param secondLineOffset - The number of spaces to indent the second 97 | * and all subsequent lines in addition to indentSpaces. 98 | */ 99 | void spacePrint( std::ostream& os, 100 | const std::string& s, 101 | int maxWidth, 102 | int indentSpaces, 103 | int secondLineOffset ) const; 104 | 105 | }; 106 | 107 | 108 | inline void StdOutput::version(CmdLineInterface& _cmd) 109 | { 110 | std::string progName = _cmd.getProgramName(); 111 | std::string xversion = _cmd.getVersion(); 112 | 113 | std::cerr << std::endl << progName << " version: " 114 | << xversion << std::endl << std::endl; 115 | } 116 | 117 | inline void StdOutput::usage(CmdLineInterface& _cmd ) 118 | { 119 | std::cerr << std::endl << "USAGE: " << std::endl << std::endl; 120 | 121 | _shortUsage( _cmd, std::cerr ); 122 | 123 | std::cerr << std::endl << std::endl << "Where: " << std::endl << std::endl; 124 | 125 | _longUsage( _cmd, std::cerr ); 126 | 127 | std::cerr << std::endl; 128 | 129 | } 130 | 131 | inline void StdOutput::failure( CmdLineInterface& _cmd, 132 | ArgException& e ) 133 | { 134 | std::string progName = _cmd.getProgramName(); 135 | 136 | std::cerr << "PARSE ERROR: " << e.argId() << std::endl 137 | << " " << e.error() << std::endl << std::endl; 138 | 139 | if ( _cmd.hasHelpAndVersion() ) 140 | { 141 | std::cerr << "Brief USAGE: " << std::endl; 142 | 143 | _shortUsage( _cmd, std::cerr ); 144 | 145 | std::cerr << std::endl << "For complete USAGE and HELP type: " 146 | << std::endl << " " << progName << " --help" 147 | << std::endl << std::endl; 148 | } 149 | else 150 | usage(_cmd); 151 | 152 | throw ExitException(1); 153 | } 154 | 155 | inline void 156 | StdOutput::_shortUsage( CmdLineInterface& _cmd, 157 | std::ostream& os ) const 158 | { 159 | std::list argList = _cmd.getArgList(); 160 | std::string progName = _cmd.getProgramName(); 161 | XorHandler xorHandler = _cmd.getXorHandler(); 162 | std::vector< std::vector > xorList = xorHandler.getXorList(); 163 | 164 | std::string s = progName + " "; 165 | 166 | // first the xor 167 | for ( int i = 0; static_cast(i) < xorList.size(); i++ ) 168 | { 169 | s += " {"; 170 | for ( ArgVectorIterator it = xorList[i].begin(); 171 | it != xorList[i].end(); it++ ) 172 | s += (*it)->shortID() + "|"; 173 | 174 | s[s.length()-1] = '}'; 175 | } 176 | 177 | // then the rest 178 | for (ArgListIterator it = argList.begin(); it != argList.end(); it++) 179 | if ( !xorHandler.contains( (*it) ) ) 180 | s += " " + (*it)->shortID(); 181 | 182 | // if the program name is too long, then adjust the second line offset 183 | int secondLineOffset = static_cast(progName.length()) + 2; 184 | if ( secondLineOffset > 75/2 ) 185 | secondLineOffset = static_cast(75/2); 186 | 187 | spacePrint( os, s, 75, 3, secondLineOffset ); 188 | } 189 | 190 | inline void 191 | StdOutput::_longUsage( CmdLineInterface& _cmd, 192 | std::ostream& os ) const 193 | { 194 | std::list argList = _cmd.getArgList(); 195 | std::string message = _cmd.getMessage(); 196 | XorHandler xorHandler = _cmd.getXorHandler(); 197 | std::vector< std::vector > xorList = xorHandler.getXorList(); 198 | 199 | // first the xor 200 | for ( int i = 0; static_cast(i) < xorList.size(); i++ ) 201 | { 202 | for ( ArgVectorIterator it = xorList[i].begin(); 203 | it != xorList[i].end(); 204 | it++ ) 205 | { 206 | spacePrint( os, (*it)->longID(), 75, 3, 3 ); 207 | spacePrint( os, (*it)->getDescription(), 75, 5, 0 ); 208 | 209 | if ( it+1 != xorList[i].end() ) 210 | spacePrint(os, "-- OR --", 75, 9, 0); 211 | } 212 | os << std::endl << std::endl; 213 | } 214 | 215 | // then the rest 216 | for (ArgListIterator it = argList.begin(); it != argList.end(); it++) 217 | if ( !xorHandler.contains( (*it) ) ) 218 | { 219 | spacePrint( os, (*it)->longID(), 75, 3, 3 ); 220 | spacePrint( os, (*it)->getDescription(), 75, 5, 0 ); 221 | os << std::endl; 222 | } 223 | 224 | os << std::endl; 225 | 226 | spacePrint( os, message, 75, 3, 0 ); 227 | } 228 | 229 | inline void StdOutput::spacePrint( std::ostream& os, 230 | const std::string& s, 231 | int maxWidth, 232 | int indentSpaces, 233 | int secondLineOffset ) const 234 | { 235 | int len = static_cast(s.length()); 236 | 237 | if ( (len + indentSpaces > maxWidth) && maxWidth > 0 ) 238 | { 239 | int allowedLen = maxWidth - indentSpaces; 240 | int start = 0; 241 | while ( start < len ) 242 | { 243 | // find the substring length 244 | // int stringLen = std::min( len - start, allowedLen ); 245 | // doing it this way to support a VisualC++ 2005 bug 246 | using namespace std; 247 | int stringLen = min( len - start, allowedLen ); 248 | 249 | // trim the length so it doesn't end in middle of a word 250 | if ( stringLen == allowedLen ) 251 | while ( stringLen >= 0 && 252 | s[stringLen+start] != ' ' && 253 | s[stringLen+start] != ',' && 254 | s[stringLen+start] != '|' ) 255 | stringLen--; 256 | 257 | // ok, the word is longer than the line, so just split 258 | // wherever the line ends 259 | if ( stringLen <= 0 ) 260 | stringLen = allowedLen; 261 | 262 | // check for newlines 263 | for ( int i = 0; i < stringLen; i++ ) 264 | if ( s[start+i] == '\n' ) 265 | stringLen = i+1; 266 | 267 | // print the indent 268 | for ( int i = 0; i < indentSpaces; i++ ) 269 | os << " "; 270 | 271 | if ( start == 0 ) 272 | { 273 | // handle second line offsets 274 | indentSpaces += secondLineOffset; 275 | 276 | // adjust allowed len 277 | allowedLen -= secondLineOffset; 278 | } 279 | 280 | os << s.substr(start,stringLen) << std::endl; 281 | 282 | // so we don't start a line with a space 283 | while ( s[stringLen+start] == ' ' && start < len ) 284 | start++; 285 | 286 | start += stringLen; 287 | } 288 | } 289 | else 290 | { 291 | for ( int i = 0; i < indentSpaces; i++ ) 292 | os << " "; 293 | os << s << std::endl; 294 | } 295 | } 296 | 297 | } //namespace TCLAP 298 | #endif 299 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/SwitchArg.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: SwitchArg.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 | 24 | #ifndef TCLAP_SWITCH_ARG_H 25 | #define TCLAP_SWITCH_ARG_H 26 | 27 | #include 28 | #include 29 | 30 | #include 31 | 32 | namespace TCLAP { 33 | 34 | /** 35 | * A simple switch argument. If the switch is set on the command line, then 36 | * the getValue method will return the opposite of the default value for the 37 | * switch. 38 | */ 39 | class SwitchArg : public Arg 40 | { 41 | protected: 42 | 43 | /** 44 | * The value of the switch. 45 | */ 46 | bool _value; 47 | 48 | /** 49 | * Used to support the reset() method so that ValueArg can be 50 | * reset to their constructed value. 51 | */ 52 | bool _default; 53 | 54 | public: 55 | 56 | /** 57 | * SwitchArg constructor. 58 | * \param flag - The one character flag that identifies this 59 | * argument on the command line. 60 | * \param name - A one word name for the argument. Can be 61 | * used as a long flag on the command line. 62 | * \param desc - A description of what the argument is for or 63 | * does. 64 | * \param def - The default value for this Switch. 65 | * \param v - An optional visitor. You probably should not 66 | * use this unless you have a very good reason. 67 | */ 68 | SwitchArg(const std::string& flag, 69 | const std::string& name, 70 | const std::string& desc, 71 | bool def = false, 72 | Visitor* v = NULL); 73 | 74 | 75 | /** 76 | * SwitchArg constructor. 77 | * \param flag - The one character flag that identifies this 78 | * argument on the command line. 79 | * \param name - A one word name for the argument. Can be 80 | * used as a long flag on the command line. 81 | * \param desc - A description of what the argument is for or 82 | * does. 83 | * \param parser - A CmdLine parser object to add this Arg to 84 | * \param def - The default value for this Switch. 85 | * \param v - An optional visitor. You probably should not 86 | * use this unless you have a very good reason. 87 | */ 88 | SwitchArg(const std::string& flag, 89 | const std::string& name, 90 | const std::string& desc, 91 | CmdLineInterface& parser, 92 | bool def = false, 93 | Visitor* v = NULL); 94 | 95 | 96 | /** 97 | * Handles the processing of the argument. 98 | * This re-implements the Arg version of this method to set the 99 | * _value of the argument appropriately. 100 | * \param i - Pointer the the current argument in the list. 101 | * \param args - Mutable list of strings. Passed 102 | * in from main(). 103 | */ 104 | virtual bool processArg(int* i, std::vector& args); 105 | 106 | /** 107 | * Checks a string to see if any of the chars in the string 108 | * match the flag for this Switch. 109 | */ 110 | bool combinedSwitchesMatch(std::string& combined); 111 | 112 | /** 113 | * Returns bool, whether or not the switch has been set. 114 | */ 115 | bool getValue(); 116 | 117 | virtual void reset(); 118 | 119 | private: 120 | /** 121 | * Checks to see if we've found the last match in 122 | * a combined string. 123 | */ 124 | bool lastCombined(std::string& combined); 125 | 126 | /** 127 | * Does the common processing of processArg. 128 | */ 129 | void commonProcessing(); 130 | }; 131 | 132 | ////////////////////////////////////////////////////////////////////// 133 | //BEGIN SwitchArg.cpp 134 | ////////////////////////////////////////////////////////////////////// 135 | inline SwitchArg::SwitchArg(const std::string& flag, 136 | const std::string& name, 137 | const std::string& desc, 138 | bool default_val, 139 | Visitor* v ) 140 | : Arg(flag, name, desc, false, false, v), 141 | _value( default_val ), 142 | _default( default_val ) 143 | { } 144 | 145 | inline SwitchArg::SwitchArg(const std::string& flag, 146 | const std::string& name, 147 | const std::string& desc, 148 | CmdLineInterface& parser, 149 | bool default_val, 150 | Visitor* v ) 151 | : Arg(flag, name, desc, false, false, v), 152 | _value( default_val ), 153 | _default(default_val) 154 | { 155 | parser.add( this ); 156 | } 157 | 158 | inline bool SwitchArg::getValue() { return _value; } 159 | 160 | inline bool SwitchArg::lastCombined(std::string& combinedSwitches ) 161 | { 162 | for ( unsigned int i = 1; i < combinedSwitches.length(); i++ ) 163 | if ( combinedSwitches[i] != Arg::blankChar() ) 164 | return false; 165 | 166 | return true; 167 | } 168 | 169 | inline bool SwitchArg::combinedSwitchesMatch(std::string& combinedSwitches ) 170 | { 171 | // make sure this is actually a combined switch 172 | if ( combinedSwitches.length() > 0 && 173 | combinedSwitches[0] != Arg::flagStartString()[0] ) 174 | return false; 175 | 176 | // make sure it isn't a long name 177 | if ( combinedSwitches.substr( 0, Arg::nameStartString().length() ) == 178 | Arg::nameStartString() ) 179 | return false; 180 | 181 | // make sure the delimiter isn't in the string 182 | if ( combinedSwitches.find_first_of( Arg::delimiter() ) != std::string::npos ) 183 | return false; 184 | 185 | // ok, we're not specifying a ValueArg, so we know that we have 186 | // a combined switch list. 187 | for ( unsigned int i = 1; i < combinedSwitches.length(); i++ ) 188 | if ( _flag.length() > 0 && 189 | combinedSwitches[i] == _flag[0] && 190 | _flag[0] != Arg::flagStartString()[0] ) 191 | { 192 | // update the combined switches so this one is no longer present 193 | // this is necessary so that no unlabeled args are matched 194 | // later in the processing. 195 | //combinedSwitches.erase(i,1); 196 | combinedSwitches[i] = Arg::blankChar(); 197 | return true; 198 | } 199 | 200 | // none of the switches passed in the list match. 201 | return false; 202 | } 203 | 204 | inline void SwitchArg::commonProcessing() 205 | { 206 | if ( _xorSet ) 207 | throw(CmdLineParseException( 208 | "Mutually exclusive argument already set!", toString())); 209 | 210 | if ( _alreadySet ) 211 | throw(CmdLineParseException("Argument already set!", toString())); 212 | 213 | _alreadySet = true; 214 | 215 | if ( _value == true ) 216 | _value = false; 217 | else 218 | _value = true; 219 | 220 | _checkWithVisitor(); 221 | } 222 | 223 | inline bool SwitchArg::processArg(int *i, std::vector& args) 224 | { 225 | if ( _ignoreable && Arg::ignoreRest() ) 226 | return false; 227 | 228 | // if the whole string matches the flag or name string 229 | if ( argMatches( args[*i] ) ) 230 | { 231 | commonProcessing(); 232 | 233 | return true; 234 | } 235 | // if a substring matches the flag as part of a combination 236 | else if ( combinedSwitchesMatch( args[*i] ) ) 237 | { 238 | // check again to ensure we don't misinterpret 239 | // this as a MultiSwitchArg 240 | if ( combinedSwitchesMatch( args[*i] ) ) 241 | throw(CmdLineParseException("Argument already set!", 242 | toString())); 243 | 244 | commonProcessing(); 245 | 246 | // We only want to return true if we've found the last combined 247 | // match in the string, otherwise we return true so that other 248 | // switches in the combination will have a chance to match. 249 | return lastCombined( args[*i] ); 250 | } 251 | else 252 | return false; 253 | } 254 | 255 | inline void SwitchArg::reset() 256 | { 257 | Arg::reset(); 258 | _value = _default; 259 | } 260 | ////////////////////////////////////////////////////////////////////// 261 | //End SwitchArg.cpp 262 | ////////////////////////////////////////////////////////////////////// 263 | 264 | } //namespace TCLAP 265 | 266 | #endif 267 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/UnlabeledMultiArg.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: UnlabeledMultiArg.h 5 | * 6 | * Copyright (c) 2003, 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 | 23 | #ifndef TCLAP_MULTIPLE_UNLABELED_ARGUMENT_H 24 | #define TCLAP_MULTIPLE_UNLABELED_ARGUMENT_H 25 | 26 | #include 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | namespace TCLAP { 33 | 34 | /** 35 | * Just like a MultiArg, except that the arguments are unlabeled. Basically, 36 | * this Arg will slurp up everything that hasn't been matched to another 37 | * Arg. 38 | */ 39 | template 40 | class UnlabeledMultiArg : public MultiArg 41 | { 42 | 43 | // If compiler has two stage name lookup (as gcc >= 3.4 does) 44 | // this is requried to prevent undef. symbols 45 | using MultiArg::_ignoreable; 46 | using MultiArg::_hasBlanks; 47 | using MultiArg::_extractValue; 48 | using MultiArg::_typeDesc; 49 | using MultiArg::_name; 50 | using MultiArg::_description; 51 | using MultiArg::_alreadySet; 52 | using MultiArg::toString; 53 | 54 | public: 55 | 56 | /** 57 | * Constructor. 58 | * \param name - The name of the Arg. Note that this is used for 59 | * identification, not as a long flag. 60 | * \param desc - A description of what the argument is for or 61 | * does. 62 | * \param req - Whether the argument is required on the command 63 | * line. 64 | * \param typeDesc - A short, human readable description of the 65 | * type that this object expects. This is used in the generation 66 | * of the USAGE statement. The goal is to be helpful to the end user 67 | * of the program. 68 | * \param ignoreable - Whether or not this argument can be ignored 69 | * using the "--" flag. 70 | * \param v - An optional visitor. You probably should not 71 | * use this unless you have a very good reason. 72 | */ 73 | UnlabeledMultiArg( const std::string& name, 74 | const std::string& desc, 75 | bool req, 76 | const std::string& typeDesc, 77 | bool ignoreable = false, 78 | Visitor* v = NULL ); 79 | /** 80 | * Constructor. 81 | * \param name - The name of the Arg. Note that this is used for 82 | * identification, not as a long flag. 83 | * \param desc - A description of what the argument is for or 84 | * does. 85 | * \param req - Whether the argument is required on the command 86 | * line. 87 | * \param typeDesc - A short, human readable description of the 88 | * type that this object expects. This is used in the generation 89 | * of the USAGE statement. The goal is to be helpful to the end user 90 | * of the program. 91 | * \param parser - A CmdLine parser object to add this Arg to 92 | * \param ignoreable - Whether or not this argument can be ignored 93 | * using the "--" flag. 94 | * \param v - An optional visitor. You probably should not 95 | * use this unless you have a very good reason. 96 | */ 97 | UnlabeledMultiArg( const std::string& name, 98 | const std::string& desc, 99 | bool req, 100 | const std::string& typeDesc, 101 | CmdLineInterface& parser, 102 | bool ignoreable = false, 103 | Visitor* v = NULL ); 104 | 105 | /** 106 | * Constructor. 107 | * \param name - The name of the Arg. Note that this is used for 108 | * identification, not as a long flag. 109 | * \param desc - A description of what the argument is for or 110 | * does. 111 | * \param req - Whether the argument is required on the command 112 | * line. 113 | * \param constraint - A pointer to a Constraint object used 114 | * to constrain this Arg. 115 | * \param ignoreable - Whether or not this argument can be ignored 116 | * using the "--" flag. 117 | * \param v - An optional visitor. You probably should not 118 | * use this unless you have a very good reason. 119 | */ 120 | UnlabeledMultiArg( const std::string& name, 121 | const std::string& desc, 122 | bool req, 123 | Constraint* constraint, 124 | bool ignoreable = false, 125 | Visitor* v = NULL ); 126 | 127 | /** 128 | * Constructor. 129 | * \param name - The name of the Arg. Note that this is used for 130 | * identification, not as a long flag. 131 | * \param desc - A description of what the argument is for or 132 | * does. 133 | * \param req - Whether the argument is required on the command 134 | * line. 135 | * \param constraint - A pointer to a Constraint object used 136 | * to constrain this Arg. 137 | * \param parser - A CmdLine parser object to add this Arg to 138 | * \param ignoreable - Whether or not this argument can be ignored 139 | * using the "--" flag. 140 | * \param v - An optional visitor. You probably should not 141 | * use this unless you have a very good reason. 142 | */ 143 | UnlabeledMultiArg( const std::string& name, 144 | const std::string& desc, 145 | bool req, 146 | Constraint* constraint, 147 | CmdLineInterface& parser, 148 | bool ignoreable = false, 149 | Visitor* v = NULL ); 150 | 151 | /** 152 | * Handles the processing of the argument. 153 | * This re-implements the Arg version of this method to set the 154 | * _value of the argument appropriately. It knows the difference 155 | * between labeled and unlabeled. 156 | * \param i - Pointer the the current argument in the list. 157 | * \param args - Mutable list of strings. Passed from main(). 158 | */ 159 | virtual bool processArg(int* i, std::vector& args); 160 | 161 | /** 162 | * Returns the a short id string. Used in the usage. 163 | * \param val - value to be used. 164 | */ 165 | virtual std::string shortID(const std::string& val="val") const; 166 | 167 | /** 168 | * Returns the a long id string. Used in the usage. 169 | * \param val - value to be used. 170 | */ 171 | virtual std::string longID(const std::string& val="val") const; 172 | 173 | /** 174 | * Opertor ==. 175 | * \param a - The Arg to be compared to this. 176 | */ 177 | virtual bool operator==(const Arg& a) const; 178 | 179 | /** 180 | * Pushes this to back of list rather than front. 181 | * \param argList - The list this should be added to. 182 | */ 183 | virtual void addToList( std::list& argList ) const; 184 | }; 185 | 186 | template 187 | UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, 188 | const std::string& desc, 189 | bool req, 190 | const std::string& typeDesc, 191 | bool ignoreable, 192 | Visitor* v) 193 | : MultiArg("", name, desc, req, typeDesc, v) 194 | { 195 | _ignoreable = ignoreable; 196 | OptionalUnlabeledTracker::check(true, toString()); 197 | } 198 | 199 | template 200 | UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, 201 | const std::string& desc, 202 | bool req, 203 | const std::string& typeDesc, 204 | CmdLineInterface& parser, 205 | bool ignoreable, 206 | Visitor* v) 207 | : MultiArg("", name, desc, req, typeDesc, v) 208 | { 209 | _ignoreable = ignoreable; 210 | OptionalUnlabeledTracker::check(true, toString()); 211 | parser.add( this ); 212 | } 213 | 214 | 215 | template 216 | UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, 217 | const std::string& desc, 218 | bool req, 219 | Constraint* constraint, 220 | bool ignoreable, 221 | Visitor* v) 222 | : MultiArg("", name, desc, req, constraint, v) 223 | { 224 | _ignoreable = ignoreable; 225 | OptionalUnlabeledTracker::check(true, toString()); 226 | } 227 | 228 | template 229 | UnlabeledMultiArg::UnlabeledMultiArg(const std::string& name, 230 | const std::string& desc, 231 | bool req, 232 | Constraint* constraint, 233 | CmdLineInterface& parser, 234 | bool ignoreable, 235 | Visitor* v) 236 | : MultiArg("", name, desc, req, constraint, v) 237 | { 238 | _ignoreable = ignoreable; 239 | OptionalUnlabeledTracker::check(true, toString()); 240 | parser.add( this ); 241 | } 242 | 243 | 244 | template 245 | bool UnlabeledMultiArg::processArg(int *i, std::vector& args) 246 | { 247 | 248 | if ( _hasBlanks( args[*i] ) ) 249 | return false; 250 | 251 | // never ignore an unlabeled multi arg 252 | 253 | 254 | // always take the first value, regardless of the start string 255 | _extractValue( args[(*i)] ); 256 | 257 | /* 258 | // continue taking args until we hit the end or a start string 259 | while ( (unsigned int)(*i)+1 < args.size() && 260 | args[(*i)+1].find_first_of( Arg::flagStartString() ) != 0 && 261 | args[(*i)+1].find_first_of( Arg::nameStartString() ) != 0 ) 262 | _extractValue( args[++(*i)] ); 263 | */ 264 | 265 | _alreadySet = true; 266 | 267 | return true; 268 | } 269 | 270 | template 271 | std::string UnlabeledMultiArg::shortID(const std::string& val) const 272 | { 273 | static_cast(val); // Ignore input, don't warn 274 | return std::string("<") + _typeDesc + "> ..."; 275 | } 276 | 277 | template 278 | std::string UnlabeledMultiArg::longID(const std::string& val) const 279 | { 280 | static_cast(val); // Ignore input, don't warn 281 | return std::string("<") + _typeDesc + "> (accepted multiple times)"; 282 | } 283 | 284 | template 285 | bool UnlabeledMultiArg::operator==(const Arg& a) const 286 | { 287 | if ( _name == a.getName() || _description == a.getDescription() ) 288 | return true; 289 | else 290 | return false; 291 | } 292 | 293 | template 294 | void UnlabeledMultiArg::addToList( std::list& argList ) const 295 | { 296 | argList.push_back( const_cast(static_cast(this)) ); 297 | } 298 | 299 | } 300 | 301 | #endif 302 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/UnlabeledValueArg.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: UnlabeledValueArg.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 | 24 | #ifndef TCLAP_UNLABELED_VALUE_ARGUMENT_H 25 | #define TCLAP_UNLABELED_VALUE_ARGUMENT_H 26 | 27 | #include 28 | #include 29 | 30 | #include 31 | #include 32 | 33 | 34 | namespace TCLAP { 35 | 36 | /** 37 | * The basic unlabeled argument that parses a value. 38 | * This is a template class, which means the type T defines the type 39 | * that a given object will attempt to parse when an UnlabeledValueArg 40 | * is reached in the list of args that the CmdLine iterates over. 41 | */ 42 | template 43 | class UnlabeledValueArg : public ValueArg 44 | { 45 | 46 | // If compiler has two stage name lookup (as gcc >= 3.4 does) 47 | // this is requried to prevent undef. symbols 48 | using ValueArg::_ignoreable; 49 | using ValueArg::_hasBlanks; 50 | using ValueArg::_extractValue; 51 | using ValueArg::_typeDesc; 52 | using ValueArg::_name; 53 | using ValueArg::_description; 54 | using ValueArg::_alreadySet; 55 | using ValueArg::toString; 56 | 57 | public: 58 | 59 | /** 60 | * UnlabeledValueArg constructor. 61 | * \param name - A one word name for the argument. Note that this is used for 62 | * identification, not as a long flag. 63 | * \param desc - A description of what the argument is for or 64 | * does. 65 | * \param req - Whether the argument is required on the command 66 | * line. 67 | * \param value - The default value assigned to this argument if it 68 | * is not present on the command line. 69 | * \param typeDesc - A short, human readable description of the 70 | * type that this object expects. This is used in the generation 71 | * of the USAGE statement. The goal is to be helpful to the end user 72 | * of the program. 73 | * \param ignoreable - Allows you to specify that this argument can be 74 | * ignored if the '--' flag is set. This defaults to false (cannot 75 | * be ignored) and should generally stay that way unless you have 76 | * some special need for certain arguments to be ignored. 77 | * \param v - Optional Vistor. You should leave this blank unless 78 | * you have a very good reason. 79 | */ 80 | UnlabeledValueArg( const std::string& name, 81 | const std::string& desc, 82 | bool req, 83 | T value, 84 | const std::string& typeDesc, 85 | bool ignoreable = false, 86 | Visitor* v = NULL); 87 | 88 | /** 89 | * UnlabeledValueArg constructor. 90 | * \param name - A one word name for the argument. Note that this is used for 91 | * identification, not as a long flag. 92 | * \param desc - A description of what the argument is for or 93 | * does. 94 | * \param req - Whether the argument is required on the command 95 | * line. 96 | * \param value - The default value assigned to this argument if it 97 | * is not present on the command line. 98 | * \param typeDesc - A short, human readable description of the 99 | * type that this object expects. This is used in the generation 100 | * of the USAGE statement. The goal is to be helpful to the end user 101 | * of the program. 102 | * \param parser - A CmdLine parser object to add this Arg to 103 | * \param ignoreable - Allows you to specify that this argument can be 104 | * ignored if the '--' flag is set. This defaults to false (cannot 105 | * be ignored) and should generally stay that way unless you have 106 | * some special need for certain arguments to be ignored. 107 | * \param v - Optional Vistor. You should leave this blank unless 108 | * you have a very good reason. 109 | */ 110 | UnlabeledValueArg( const std::string& name, 111 | const std::string& desc, 112 | bool req, 113 | T value, 114 | const std::string& typeDesc, 115 | CmdLineInterface& parser, 116 | bool ignoreable = false, 117 | Visitor* v = NULL ); 118 | 119 | /** 120 | * UnlabeledValueArg constructor. 121 | * \param name - A one word name for the argument. Note that this is used for 122 | * identification, not as a long flag. 123 | * \param desc - A description of what the argument is for or 124 | * does. 125 | * \param req - Whether the argument is required on the command 126 | * line. 127 | * \param value - The default value assigned to this argument if it 128 | * is not present on the command line. 129 | * \param constraint - A pointer to a Constraint object used 130 | * to constrain this Arg. 131 | * \param ignoreable - Allows you to specify that this argument can be 132 | * ignored if the '--' flag is set. This defaults to false (cannot 133 | * be ignored) and should generally stay that way unless you have 134 | * some special need for certain arguments to be ignored. 135 | * \param v - Optional Vistor. You should leave this blank unless 136 | * you have a very good reason. 137 | */ 138 | UnlabeledValueArg( const std::string& name, 139 | const std::string& desc, 140 | bool req, 141 | T value, 142 | Constraint* constraint, 143 | bool ignoreable = false, 144 | Visitor* v = NULL ); 145 | 146 | 147 | /** 148 | * UnlabeledValueArg constructor. 149 | * \param name - A one word name for the argument. Note that this is used for 150 | * identification, not as a long flag. 151 | * \param desc - A description of what the argument is for or 152 | * does. 153 | * \param req - Whether the argument is required on the command 154 | * line. 155 | * \param value - The default value assigned to this argument if it 156 | * is not present on the command line. 157 | * \param constraint - A pointer to a Constraint object used 158 | * to constrain this Arg. 159 | * \param parser - A CmdLine parser object to add this Arg to 160 | * \param ignoreable - Allows you to specify that this argument can be 161 | * ignored if the '--' flag is set. This defaults to false (cannot 162 | * be ignored) and should generally stay that way unless you have 163 | * some special need for certain arguments to be ignored. 164 | * \param v - Optional Vistor. You should leave this blank unless 165 | * you have a very good reason. 166 | */ 167 | UnlabeledValueArg( const std::string& name, 168 | const std::string& desc, 169 | bool req, 170 | T value, 171 | Constraint* constraint, 172 | CmdLineInterface& parser, 173 | bool ignoreable = false, 174 | Visitor* v = NULL); 175 | 176 | /** 177 | * Handles the processing of the argument. 178 | * This re-implements the Arg version of this method to set the 179 | * _value of the argument appropriately. Handling specific to 180 | * unlabled arguments. 181 | * \param i - Pointer the the current argument in the list. 182 | * \param args - Mutable list of strings. 183 | */ 184 | virtual bool processArg(int* i, std::vector& args); 185 | 186 | /** 187 | * Overrides shortID for specific behavior. 188 | */ 189 | virtual std::string shortID(const std::string& val="val") const; 190 | 191 | /** 192 | * Overrides longID for specific behavior. 193 | */ 194 | virtual std::string longID(const std::string& val="val") const; 195 | 196 | /** 197 | * Overrides operator== for specific behavior. 198 | */ 199 | virtual bool operator==(const Arg& a ) const; 200 | 201 | /** 202 | * Instead of pushing to the front of list, push to the back. 203 | * \param argList - The list to add this to. 204 | */ 205 | virtual void addToList( std::list& argList ) const; 206 | 207 | }; 208 | 209 | /** 210 | * Constructor implemenation. 211 | */ 212 | template 213 | UnlabeledValueArg::UnlabeledValueArg(const std::string& name, 214 | const std::string& desc, 215 | bool req, 216 | T val, 217 | const std::string& typeDesc, 218 | bool ignoreable, 219 | Visitor* v) 220 | : ValueArg("", name, desc, req, val, typeDesc, v) 221 | { 222 | _ignoreable = ignoreable; 223 | 224 | OptionalUnlabeledTracker::check(req, toString()); 225 | 226 | } 227 | 228 | template 229 | UnlabeledValueArg::UnlabeledValueArg(const std::string& name, 230 | const std::string& desc, 231 | bool req, 232 | T val, 233 | const std::string& typeDesc, 234 | CmdLineInterface& parser, 235 | bool ignoreable, 236 | Visitor* v) 237 | : ValueArg("", name, desc, req, val, typeDesc, v) 238 | { 239 | _ignoreable = ignoreable; 240 | OptionalUnlabeledTracker::check(req, toString()); 241 | parser.add( this ); 242 | } 243 | 244 | /** 245 | * Constructor implemenation. 246 | */ 247 | template 248 | UnlabeledValueArg::UnlabeledValueArg(const std::string& name, 249 | const std::string& desc, 250 | bool req, 251 | T val, 252 | Constraint* constraint, 253 | bool ignoreable, 254 | Visitor* v) 255 | : ValueArg("", name, desc, req, val, constraint, v) 256 | { 257 | _ignoreable = ignoreable; 258 | OptionalUnlabeledTracker::check(req, toString()); 259 | } 260 | 261 | template 262 | UnlabeledValueArg::UnlabeledValueArg(const std::string& name, 263 | const std::string& desc, 264 | bool req, 265 | T val, 266 | Constraint* constraint, 267 | CmdLineInterface& parser, 268 | bool ignoreable, 269 | Visitor* v) 270 | : ValueArg("", name, desc, req, val, constraint, v) 271 | { 272 | _ignoreable = ignoreable; 273 | OptionalUnlabeledTracker::check(req, toString()); 274 | parser.add( this ); 275 | } 276 | 277 | /** 278 | * Implementation of processArg(). 279 | */ 280 | template 281 | bool UnlabeledValueArg::processArg(int *i, std::vector& args) 282 | { 283 | 284 | if ( _alreadySet ) 285 | return false; 286 | 287 | if ( _hasBlanks( args[*i] ) ) 288 | return false; 289 | 290 | // never ignore an unlabeled arg 291 | 292 | _extractValue( args[*i] ); 293 | _alreadySet = true; 294 | return true; 295 | } 296 | 297 | /** 298 | * Overriding shortID for specific output. 299 | */ 300 | template 301 | std::string UnlabeledValueArg::shortID(const std::string& val) const 302 | { 303 | static_cast(val); // Ignore input, don't warn 304 | return std::string("<") + _typeDesc + ">"; 305 | } 306 | 307 | /** 308 | * Overriding longID for specific output. 309 | */ 310 | template 311 | std::string UnlabeledValueArg::longID(const std::string& val) const 312 | { 313 | static_cast(val); // Ignore input, don't warn 314 | 315 | // Ideally we would like to be able to use RTTI to return the name 316 | // of the type required for this argument. However, g++ at least, 317 | // doesn't appear to return terribly useful "names" of the types. 318 | return std::string("<") + _typeDesc + ">"; 319 | } 320 | 321 | /** 322 | * Overriding operator== for specific behavior. 323 | */ 324 | template 325 | bool UnlabeledValueArg::operator==(const Arg& a ) const 326 | { 327 | if ( _name == a.getName() || _description == a.getDescription() ) 328 | return true; 329 | else 330 | return false; 331 | } 332 | 333 | template 334 | void UnlabeledValueArg::addToList( std::list& argList ) const 335 | { 336 | argList.push_back( const_cast(static_cast(this)) ); 337 | } 338 | 339 | } 340 | #endif 341 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/ValueArg.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * 3 | * file: ValueArg.h 4 | * 5 | * Copyright (c) 2003, Michael E. Smoot . 6 | * Copyright (c) 2004, Michael E. Smoot, Daniel Aarno. 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 | 23 | #ifndef TCLAP_VALUE_ARGUMENT_H 24 | #define TCLAP_VALUE_ARGUMENT_H 25 | 26 | #include 27 | #include 28 | 29 | #include 30 | #include 31 | 32 | namespace TCLAP { 33 | 34 | /** 35 | * The basic labeled argument that parses a value. 36 | * This is a template class, which means the type T defines the type 37 | * that a given object will attempt to parse when the flag/name is matched 38 | * on the command line. While there is nothing stopping you from creating 39 | * an unflagged ValueArg, it is unwise and would cause significant problems. 40 | * Instead use an UnlabeledValueArg. 41 | */ 42 | template 43 | class ValueArg : public Arg 44 | { 45 | protected: 46 | 47 | /** 48 | * The value parsed from the command line. 49 | * Can be of any type, as long as the >> operator for the type 50 | * is defined. 51 | */ 52 | T _value; 53 | 54 | /** 55 | * Used to support the reset() method so that ValueArg can be 56 | * reset to their constructed value. 57 | */ 58 | T _default; 59 | 60 | /** 61 | * A human readable description of the type to be parsed. 62 | * This is a hack, plain and simple. Ideally we would use RTTI to 63 | * return the name of type T, but until there is some sort of 64 | * consistent support for human readable names, we are left to our 65 | * own devices. 66 | */ 67 | std::string _typeDesc; 68 | 69 | /** 70 | * A Constraint this Arg must conform to. 71 | */ 72 | Constraint* _constraint; 73 | 74 | /** 75 | * Extracts the value from the string. 76 | * Attempts to parse string as type T, if this fails an exception 77 | * is thrown. 78 | * \param val - value to be parsed. 79 | */ 80 | void _extractValue( const std::string& val ); 81 | 82 | public: 83 | 84 | /** 85 | * Labeled ValueArg constructor. 86 | * You could conceivably call this constructor with a blank flag, 87 | * but that would make you a bad person. It would also cause 88 | * an exception to be thrown. If you want an unlabeled argument, 89 | * use the other constructor. 90 | * \param flag - The one character flag that identifies this 91 | * argument on the command line. 92 | * \param name - A one word name for the argument. Can be 93 | * used as a long flag on the command line. 94 | * \param desc - A description of what the argument is for or 95 | * does. 96 | * \param req - Whether the argument is required on the command 97 | * line. 98 | * \param value - The default value assigned to this argument if it 99 | * is not present on the command line. 100 | * \param typeDesc - A short, human readable description of the 101 | * type that this object expects. This is used in the generation 102 | * of the USAGE statement. The goal is to be helpful to the end user 103 | * of the program. 104 | * \param v - An optional visitor. You probably should not 105 | * use this unless you have a very good reason. 106 | */ 107 | ValueArg( const std::string& flag, 108 | const std::string& name, 109 | const std::string& desc, 110 | bool req, 111 | T value, 112 | const std::string& typeDesc, 113 | Visitor* v = NULL); 114 | 115 | 116 | /** 117 | * Labeled ValueArg constructor. 118 | * You could conceivably call this constructor with a blank flag, 119 | * but that would make you a bad person. It would also cause 120 | * an exception to be thrown. If you want an unlabeled argument, 121 | * use the other constructor. 122 | * \param flag - The one character flag that identifies this 123 | * argument on the command line. 124 | * \param name - A one word name for the argument. Can be 125 | * used as a long flag on the command line. 126 | * \param desc - A description of what the argument is for or 127 | * does. 128 | * \param req - Whether the argument is required on the command 129 | * line. 130 | * \param value - The default value assigned to this argument if it 131 | * is not present on the command line. 132 | * \param typeDesc - A short, human readable description of the 133 | * type that this object expects. This is used in the generation 134 | * of the USAGE statement. The goal is to be helpful to the end user 135 | * of the program. 136 | * \param parser - A CmdLine parser object to add this Arg to 137 | * \param v - An optional visitor. You probably should not 138 | * use this unless you have a very good reason. 139 | */ 140 | ValueArg( const std::string& flag, 141 | const std::string& name, 142 | const std::string& desc, 143 | bool req, 144 | T value, 145 | const std::string& typeDesc, 146 | CmdLineInterface& parser, 147 | Visitor* v = NULL ); 148 | 149 | /** 150 | * Labeled ValueArg constructor. 151 | * You could conceivably call this constructor with a blank flag, 152 | * but that would make you a bad person. It would also cause 153 | * an exception to be thrown. If you want an unlabeled argument, 154 | * use the other constructor. 155 | * \param flag - The one character flag that identifies this 156 | * argument on the command line. 157 | * \param name - A one word name for the argument. Can be 158 | * used as a long flag on the command line. 159 | * \param desc - A description of what the argument is for or 160 | * does. 161 | * \param req - Whether the argument is required on the command 162 | * line. 163 | * \param value - The default value assigned to this argument if it 164 | * is not present on the command line. 165 | * \param constraint - A pointer to a Constraint object used 166 | * to constrain this Arg. 167 | * \param parser - A CmdLine parser object to add this Arg to. 168 | * \param v - An optional visitor. You probably should not 169 | * use this unless you have a very good reason. 170 | */ 171 | ValueArg( const std::string& flag, 172 | const std::string& name, 173 | const std::string& desc, 174 | bool req, 175 | T value, 176 | Constraint* constraint, 177 | CmdLineInterface& parser, 178 | Visitor* v = NULL ); 179 | 180 | /** 181 | * Labeled ValueArg constructor. 182 | * You could conceivably call this constructor with a blank flag, 183 | * but that would make you a bad person. It would also cause 184 | * an exception to be thrown. If you want an unlabeled argument, 185 | * use the other constructor. 186 | * \param flag - The one character flag that identifies this 187 | * argument on the command line. 188 | * \param name - A one word name for the argument. Can be 189 | * used as a long flag on the command line. 190 | * \param desc - A description of what the argument is for or 191 | * does. 192 | * \param req - Whether the argument is required on the command 193 | * line. 194 | * \param value - The default value assigned to this argument if it 195 | * is not present on the command line. 196 | * \param constraint - A pointer to a Constraint object used 197 | * to constrain this Arg. 198 | * \param v - An optional visitor. You probably should not 199 | * use this unless you have a very good reason. 200 | */ 201 | ValueArg( const std::string& flag, 202 | const std::string& name, 203 | const std::string& desc, 204 | bool req, 205 | T value, 206 | Constraint* constraint, 207 | Visitor* v = NULL ); 208 | 209 | /** 210 | * Handles the processing of the argument. 211 | * This re-implements the Arg version of this method to set the 212 | * _value of the argument appropriately. It knows the difference 213 | * between labeled and unlabeled. 214 | * \param i - Pointer the the current argument in the list. 215 | * \param args - Mutable list of strings. Passed 216 | * in from main(). 217 | */ 218 | virtual bool processArg(int* i, std::vector& args); 219 | 220 | /** 221 | * Returns the value of the argument. 222 | */ 223 | T& getValue() ; 224 | 225 | /** 226 | * Specialization of shortID. 227 | * \param val - value to be used. 228 | */ 229 | virtual std::string shortID(const std::string& val = "val") const; 230 | 231 | /** 232 | * Specialization of longID. 233 | * \param val - value to be used. 234 | */ 235 | virtual std::string longID(const std::string& val = "val") const; 236 | 237 | virtual void reset() ; 238 | 239 | private: 240 | /** 241 | * Prevent accidental copying 242 | */ 243 | ValueArg(const ValueArg& rhs); 244 | ValueArg& operator=(const ValueArg& rhs); 245 | }; 246 | 247 | 248 | /** 249 | * Constructor implementation. 250 | */ 251 | template 252 | ValueArg::ValueArg(const std::string& flag, 253 | const std::string& name, 254 | const std::string& desc, 255 | bool req, 256 | T val, 257 | const std::string& typeDesc, 258 | Visitor* v) 259 | : Arg(flag, name, desc, req, true, v), 260 | _value( val ), 261 | _default( val ), 262 | _typeDesc( typeDesc ), 263 | _constraint( NULL ) 264 | { } 265 | 266 | template 267 | ValueArg::ValueArg(const std::string& flag, 268 | const std::string& name, 269 | const std::string& desc, 270 | bool req, 271 | T val, 272 | const std::string& typeDesc, 273 | CmdLineInterface& parser, 274 | Visitor* v) 275 | : Arg(flag, name, desc, req, true, v), 276 | _value( val ), 277 | _default( val ), 278 | _typeDesc( typeDesc ), 279 | _constraint( NULL ) 280 | { 281 | parser.add( this ); 282 | } 283 | 284 | template 285 | ValueArg::ValueArg(const std::string& flag, 286 | const std::string& name, 287 | const std::string& desc, 288 | bool req, 289 | T val, 290 | Constraint* constraint, 291 | Visitor* v) 292 | : Arg(flag, name, desc, req, true, v), 293 | _value( val ), 294 | _default( val ), 295 | _typeDesc( constraint->shortID() ), 296 | _constraint( constraint ) 297 | { } 298 | 299 | template 300 | ValueArg::ValueArg(const std::string& flag, 301 | const std::string& name, 302 | const std::string& desc, 303 | bool req, 304 | T val, 305 | Constraint* constraint, 306 | CmdLineInterface& parser, 307 | Visitor* v) 308 | : Arg(flag, name, desc, req, true, v), 309 | _value( val ), 310 | _default( val ), 311 | _typeDesc( constraint->shortID() ), 312 | _constraint( constraint ) 313 | { 314 | parser.add( this ); 315 | } 316 | 317 | 318 | /** 319 | * Implementation of getValue(). 320 | */ 321 | template 322 | T& ValueArg::getValue() { return _value; } 323 | 324 | /** 325 | * Implementation of processArg(). 326 | */ 327 | template 328 | bool ValueArg::processArg(int *i, std::vector& args) 329 | { 330 | if ( _ignoreable && Arg::ignoreRest() ) 331 | return false; 332 | 333 | if ( _hasBlanks( args[*i] ) ) 334 | return false; 335 | 336 | std::string flag = args[*i]; 337 | 338 | std::string value = ""; 339 | trimFlag( flag, value ); 340 | 341 | if ( argMatches( flag ) ) 342 | { 343 | if ( _alreadySet ) 344 | { 345 | if ( _xorSet ) 346 | throw( CmdLineParseException( 347 | "Mutually exclusive argument already set!", 348 | toString()) ); 349 | else 350 | throw( CmdLineParseException("Argument already set!", 351 | toString()) ); 352 | } 353 | 354 | if ( Arg::delimiter() != ' ' && value == "" ) 355 | throw( ArgParseException( 356 | "Couldn't find delimiter for this argument!", 357 | toString() ) ); 358 | 359 | if ( value == "" ) 360 | { 361 | (*i)++; 362 | if ( static_cast(*i) < args.size() ) 363 | _extractValue( args[*i] ); 364 | else 365 | throw( ArgParseException("Missing a value for this argument!", 366 | toString() ) ); 367 | } 368 | else 369 | _extractValue( value ); 370 | 371 | _alreadySet = true; 372 | _checkWithVisitor(); 373 | return true; 374 | } 375 | else 376 | return false; 377 | } 378 | 379 | /** 380 | * Implementation of shortID. 381 | */ 382 | template 383 | std::string ValueArg::shortID(const std::string& val) const 384 | { 385 | static_cast(val); // Ignore input, don't warn 386 | return Arg::shortID( _typeDesc ); 387 | } 388 | 389 | /** 390 | * Implementation of longID. 391 | */ 392 | template 393 | std::string ValueArg::longID(const std::string& val) const 394 | { 395 | static_cast(val); // Ignore input, don't warn 396 | return Arg::longID( _typeDesc ); 397 | } 398 | 399 | template 400 | void ValueArg::_extractValue( const std::string& val ) 401 | { 402 | try { 403 | ExtractValue(_value, val, typename ArgTraits::ValueCategory()); 404 | } catch( ArgParseException &e) { 405 | throw ArgParseException(e.error(), toString()); 406 | } 407 | 408 | if ( _constraint != NULL ) 409 | if ( ! _constraint->check( _value ) ) 410 | throw( CmdLineParseException( "Value '" + val + 411 | + "' does not meet constraint: " 412 | + _constraint->description(), 413 | toString() ) ); 414 | } 415 | 416 | template 417 | void ValueArg::reset() 418 | { 419 | Arg::reset(); 420 | _value = _default; 421 | } 422 | 423 | } // namespace TCLAP 424 | 425 | #endif 426 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/ValuesConstraint.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | /****************************************************************************** 4 | * 5 | * file: ValuesConstraint.h 6 | * 7 | * Copyright (c) 2005, 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_VALUESCONSTRAINT_H 24 | #define TCLAP_VALUESCONSTRAINT_H 25 | 26 | #include 27 | #include 28 | #include 29 | 30 | #ifdef HAVE_CONFIG_H 31 | #include 32 | #else 33 | #define HAVE_SSTREAM 34 | #endif 35 | 36 | #if defined(HAVE_SSTREAM) 37 | #include 38 | #elif defined(HAVE_STRSTREAM) 39 | #include 40 | #else 41 | #error "Need a stringstream (sstream or strstream) to compile!" 42 | #endif 43 | 44 | namespace TCLAP { 45 | 46 | /** 47 | * A Constraint that constrains the Arg to only those values specified 48 | * in the constraint. 49 | */ 50 | template 51 | class ValuesConstraint : public Constraint 52 | { 53 | 54 | public: 55 | 56 | /** 57 | * Constructor. 58 | * \param allowed - vector of allowed values. 59 | */ 60 | ValuesConstraint(std::vector& allowed); 61 | 62 | /** 63 | * Virtual destructor. 64 | */ 65 | virtual ~ValuesConstraint() {} 66 | 67 | /** 68 | * Returns a description of the Constraint. 69 | */ 70 | virtual std::string description() const; 71 | 72 | /** 73 | * Returns the short ID for the Constraint. 74 | */ 75 | virtual std::string shortID() const; 76 | 77 | /** 78 | * The method used to verify that the value parsed from the command 79 | * line meets the constraint. 80 | * \param value - The value that will be checked. 81 | */ 82 | virtual bool check(const T& value) const; 83 | 84 | protected: 85 | 86 | /** 87 | * The list of valid values. 88 | */ 89 | std::vector _allowed; 90 | 91 | /** 92 | * The string used to describe the allowed values of this constraint. 93 | */ 94 | std::string _typeDesc; 95 | 96 | }; 97 | 98 | template 99 | ValuesConstraint::ValuesConstraint(std::vector& allowed) 100 | : _allowed(allowed), 101 | _typeDesc("") 102 | { 103 | for ( unsigned int i = 0; i < _allowed.size(); i++ ) 104 | { 105 | 106 | #if defined(HAVE_SSTREAM) 107 | std::ostringstream os; 108 | #elif defined(HAVE_STRSTREAM) 109 | std::ostrstream os; 110 | #else 111 | #error "Need a stringstream (sstream or strstream) to compile!" 112 | #endif 113 | 114 | os << _allowed[i]; 115 | 116 | std::string temp( os.str() ); 117 | 118 | if ( i > 0 ) 119 | _typeDesc += "|"; 120 | _typeDesc += temp; 121 | } 122 | } 123 | 124 | template 125 | bool ValuesConstraint::check( const T& val ) const 126 | { 127 | if ( std::find(_allowed.begin(),_allowed.end(),val) == _allowed.end() ) 128 | return false; 129 | else 130 | return true; 131 | } 132 | 133 | template 134 | std::string ValuesConstraint::shortID() const 135 | { 136 | return _typeDesc; 137 | } 138 | 139 | template 140 | std::string ValuesConstraint::description() const 141 | { 142 | return _typeDesc; 143 | } 144 | 145 | 146 | } //namespace TCLAP 147 | #endif 148 | 149 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/VersionVisitor.h: -------------------------------------------------------------------------------- 1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- 2 | 3 | /****************************************************************************** 4 | * 5 | * file: VersionVisitor.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_VERSION_VISITOR_H 25 | #define TCLAP_VERSION_VISITOR_H 26 | 27 | #include 28 | #include 29 | #include 30 | 31 | namespace TCLAP { 32 | 33 | /** 34 | * A Vistor that will call the version method of the given CmdLineOutput 35 | * for the specified CmdLine object and then exit. 36 | */ 37 | class VersionVisitor: public Visitor 38 | { 39 | private: 40 | /** 41 | * Prevent accidental copying 42 | */ 43 | VersionVisitor(const VersionVisitor& rhs); 44 | VersionVisitor& operator=(const VersionVisitor& rhs); 45 | 46 | protected: 47 | 48 | /** 49 | * The CmdLine of interest. 50 | */ 51 | CmdLineInterface* _cmd; 52 | 53 | /** 54 | * The output object. 55 | */ 56 | CmdLineOutput** _out; 57 | 58 | public: 59 | 60 | /** 61 | * Constructor. 62 | * \param cmd - The CmdLine the output is generated for. 63 | * \param out - The type of output. 64 | */ 65 | VersionVisitor( CmdLineInterface* cmd, CmdLineOutput** out ) 66 | : Visitor(), _cmd( cmd ), _out( out ) { } 67 | 68 | /** 69 | * Calls the version method of the output object using the 70 | * specified CmdLine. 71 | */ 72 | void visit() { 73 | (*_out)->version(*_cmd); 74 | throw ExitException(0); 75 | } 76 | 77 | }; 78 | 79 | } 80 | 81 | #endif 82 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/Visitor.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: Visitor.h 5 | * 6 | * Copyright (c) 2003, 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 | 23 | #ifndef TCLAP_VISITOR_H 24 | #define TCLAP_VISITOR_H 25 | 26 | namespace TCLAP { 27 | 28 | /** 29 | * A base class that defines the interface for visitors. 30 | */ 31 | class Visitor 32 | { 33 | public: 34 | 35 | /** 36 | * Constructor. Does nothing. 37 | */ 38 | Visitor() { } 39 | 40 | /** 41 | * Destructor. Does nothing. 42 | */ 43 | virtual ~Visitor() { } 44 | 45 | /** 46 | * Does nothing. Should be overridden by child. 47 | */ 48 | virtual void visit() { } 49 | }; 50 | 51 | } 52 | 53 | #endif 54 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/XorHandler.h: -------------------------------------------------------------------------------- 1 | 2 | /****************************************************************************** 3 | * 4 | * file: XorHandler.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_XORHANDLER_H 24 | #define TCLAP_XORHANDLER_H 25 | 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | 32 | namespace TCLAP { 33 | 34 | /** 35 | * This class handles lists of Arg's that are to be XOR'd on the command 36 | * line. This is used by CmdLine and you shouldn't ever use it. 37 | */ 38 | class XorHandler 39 | { 40 | protected: 41 | 42 | /** 43 | * The list of of lists of Arg's to be or'd together. 44 | */ 45 | std::vector< std::vector > _orList; 46 | 47 | public: 48 | 49 | /** 50 | * Constructor. Does nothing. 51 | */ 52 | XorHandler( ) : _orList(std::vector< std::vector >()) {} 53 | 54 | /** 55 | * Add a list of Arg*'s that will be orred together. 56 | * \param ors - list of Arg* that will be xor'd. 57 | */ 58 | void add( std::vector& ors ); 59 | 60 | /** 61 | * Checks whether the specified Arg is in one of the xor lists and 62 | * if it does match one, returns the size of the xor list that the 63 | * Arg matched. If the Arg matches, then it also sets the rest of 64 | * the Arg's in the list. You shouldn't use this. 65 | * \param a - The Arg to be checked. 66 | */ 67 | int check( const Arg* a ); 68 | 69 | /** 70 | * Returns the XOR specific short usage. 71 | */ 72 | std::string shortUsage(); 73 | 74 | /** 75 | * Prints the XOR specific long usage. 76 | * \param os - Stream to print to. 77 | */ 78 | void printLongUsage(std::ostream& os); 79 | 80 | /** 81 | * Simply checks whether the Arg is contained in one of the arg 82 | * lists. 83 | * \param a - The Arg to be checked. 84 | */ 85 | bool contains( const Arg* a ); 86 | 87 | std::vector< std::vector >& getXorList(); 88 | 89 | }; 90 | 91 | 92 | ////////////////////////////////////////////////////////////////////// 93 | //BEGIN XOR.cpp 94 | ////////////////////////////////////////////////////////////////////// 95 | inline void XorHandler::add( std::vector& ors ) 96 | { 97 | _orList.push_back( ors ); 98 | } 99 | 100 | inline int XorHandler::check( const Arg* a ) 101 | { 102 | // iterate over each XOR list 103 | for ( int i = 0; static_cast(i) < _orList.size(); i++ ) 104 | { 105 | // if the XOR list contains the arg.. 106 | ArgVectorIterator ait = std::find( _orList[i].begin(), 107 | _orList[i].end(), a ); 108 | if ( ait != _orList[i].end() ) 109 | { 110 | // first check to see if a mutually exclusive switch 111 | // has not already been set 112 | for ( ArgVectorIterator it = _orList[i].begin(); 113 | it != _orList[i].end(); 114 | it++ ) 115 | if ( a != (*it) && (*it)->isSet() ) 116 | throw(CmdLineParseException( 117 | "Mutually exclusive argument already set!", 118 | (*it)->toString())); 119 | 120 | // go through and set each arg that is not a 121 | for ( ArgVectorIterator it = _orList[i].begin(); 122 | it != _orList[i].end(); 123 | it++ ) 124 | if ( a != (*it) ) 125 | (*it)->xorSet(); 126 | 127 | // return the number of required args that have now been set 128 | if ( (*ait)->allowMore() ) 129 | return 0; 130 | else 131 | return static_cast(_orList[i].size()); 132 | } 133 | } 134 | 135 | if ( a->isRequired() ) 136 | return 1; 137 | else 138 | return 0; 139 | } 140 | 141 | inline bool XorHandler::contains( const Arg* a ) 142 | { 143 | for ( int i = 0; static_cast(i) < _orList.size(); i++ ) 144 | for ( ArgVectorIterator it = _orList[i].begin(); 145 | it != _orList[i].end(); 146 | it++ ) 147 | if ( a == (*it) ) 148 | return true; 149 | 150 | return false; 151 | } 152 | 153 | inline std::vector< std::vector >& XorHandler::getXorList() 154 | { 155 | return _orList; 156 | } 157 | 158 | 159 | 160 | ////////////////////////////////////////////////////////////////////// 161 | //END XOR.cpp 162 | ////////////////////////////////////////////////////////////////////// 163 | 164 | } //namespace TCLAP 165 | 166 | #endif 167 | -------------------------------------------------------------------------------- /tclap-1.2.1/tclap/ZshCompletionOutput.h: -------------------------------------------------------------------------------- 1 | // -*- Mode: c++; c-basic-offset: 4; tab-width: 4; -*- 2 | 3 | /****************************************************************************** 4 | * 5 | * file: ZshCompletionOutput.h 6 | * 7 | * Copyright (c) 2006, Oliver Kiddle 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_ZSHCOMPLETIONOUTPUT_H 24 | #define TCLAP_ZSHCOMPLETIONOUTPUT_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 a Zsh completion function as output from the usage() 41 | * method for the given CmdLine and its Args. 42 | */ 43 | class ZshCompletionOutput : public CmdLineOutput 44 | { 45 | 46 | public: 47 | 48 | ZshCompletionOutput(); 49 | 50 | /** 51 | * Prints the usage to stdout. Can be overridden to 52 | * produce alternative behavior. 53 | * \param c - The CmdLine object the output is generated for. 54 | */ 55 | virtual void usage(CmdLineInterface& c); 56 | 57 | /** 58 | * Prints the version to stdout. Can be overridden 59 | * to produce alternative behavior. 60 | * \param c - The CmdLine object the output is generated for. 61 | */ 62 | virtual void version(CmdLineInterface& c); 63 | 64 | /** 65 | * Prints (to stderr) an error message, short usage 66 | * Can be overridden to produce alternative behavior. 67 | * \param c - The CmdLine object the output is generated for. 68 | * \param e - The ArgException that caused the failure. 69 | */ 70 | virtual void failure(CmdLineInterface& c, 71 | ArgException& e ); 72 | 73 | protected: 74 | 75 | void basename( std::string& s ); 76 | void quoteSpecialChars( std::string& s ); 77 | 78 | std::string getMutexList( CmdLineInterface& _cmd, Arg* a ); 79 | void printOption( Arg* it, std::string mutex ); 80 | void printArg( Arg* it ); 81 | 82 | std::map common; 83 | char theDelimiter; 84 | }; 85 | 86 | ZshCompletionOutput::ZshCompletionOutput() 87 | : common(std::map()), 88 | theDelimiter('=') 89 | { 90 | common["host"] = "_hosts"; 91 | common["hostname"] = "_hosts"; 92 | common["file"] = "_files"; 93 | common["filename"] = "_files"; 94 | common["user"] = "_users"; 95 | common["username"] = "_users"; 96 | common["directory"] = "_directories"; 97 | common["path"] = "_directories"; 98 | common["url"] = "_urls"; 99 | } 100 | 101 | inline void ZshCompletionOutput::version(CmdLineInterface& _cmd) 102 | { 103 | std::cout << _cmd.getVersion() << std::endl; 104 | } 105 | 106 | inline void ZshCompletionOutput::usage(CmdLineInterface& _cmd ) 107 | { 108 | std::list argList = _cmd.getArgList(); 109 | std::string progName = _cmd.getProgramName(); 110 | std::string xversion = _cmd.getVersion(); 111 | theDelimiter = _cmd.getDelimiter(); 112 | basename(progName); 113 | 114 | std::cout << "#compdef " << progName << std::endl << std::endl << 115 | "# " << progName << " version " << _cmd.getVersion() << std::endl << std::endl << 116 | "_arguments -s -S"; 117 | 118 | for (ArgListIterator it = argList.begin(); it != argList.end(); it++) 119 | { 120 | if ( (*it)->shortID().at(0) == '<' ) 121 | printArg((*it)); 122 | else if ( (*it)->getFlag() != "-" ) 123 | printOption((*it), getMutexList(_cmd, *it)); 124 | } 125 | 126 | std::cout << std::endl; 127 | } 128 | 129 | inline void ZshCompletionOutput::failure( CmdLineInterface& _cmd, 130 | ArgException& e ) 131 | { 132 | static_cast(_cmd); // unused 133 | std::cout << e.what() << std::endl; 134 | } 135 | 136 | inline void ZshCompletionOutput::quoteSpecialChars( std::string& s ) 137 | { 138 | size_t idx = s.find_last_of(':'); 139 | while ( idx != std::string::npos ) 140 | { 141 | s.insert(idx, 1, '\\'); 142 | idx = s.find_last_of(':', idx); 143 | } 144 | idx = s.find_last_of('\''); 145 | while ( idx != std::string::npos ) 146 | { 147 | s.insert(idx, "'\\'"); 148 | if (idx == 0) 149 | idx = std::string::npos; 150 | else 151 | idx = s.find_last_of('\'', --idx); 152 | } 153 | } 154 | 155 | inline void ZshCompletionOutput::basename( std::string& s ) 156 | { 157 | size_t p = s.find_last_of('/'); 158 | if ( p != std::string::npos ) 159 | { 160 | s.erase(0, p + 1); 161 | } 162 | } 163 | 164 | inline void ZshCompletionOutput::printArg(Arg* a) 165 | { 166 | static int count = 1; 167 | 168 | std::cout << " \\" << std::endl << " '"; 169 | if ( a->acceptsMultipleValues() ) 170 | std::cout << '*'; 171 | else 172 | std::cout << count++; 173 | std::cout << ':'; 174 | if ( !a->isRequired() ) 175 | std::cout << ':'; 176 | 177 | std::cout << a->getName() << ':'; 178 | std::map::iterator compArg = common.find(a->getName()); 179 | if ( compArg != common.end() ) 180 | { 181 | std::cout << compArg->second; 182 | } 183 | else 184 | { 185 | std::cout << "_guard \"^-*\" " << a->getName(); 186 | } 187 | std::cout << '\''; 188 | } 189 | 190 | inline void ZshCompletionOutput::printOption(Arg* a, std::string mutex) 191 | { 192 | std::string flag = a->flagStartChar() + a->getFlag(); 193 | std::string name = a->nameStartString() + a->getName(); 194 | std::string desc = a->getDescription(); 195 | 196 | // remove full stop and capitalisation from description as 197 | // this is the convention for zsh function 198 | if (!desc.compare(0, 12, "(required) ")) 199 | { 200 | desc.erase(0, 12); 201 | } 202 | if (!desc.compare(0, 15, "(OR required) ")) 203 | { 204 | desc.erase(0, 15); 205 | } 206 | size_t len = desc.length(); 207 | if (len && desc.at(--len) == '.') 208 | { 209 | desc.erase(len); 210 | } 211 | if (len) 212 | { 213 | desc.replace(0, 1, 1, tolower(desc.at(0))); 214 | } 215 | 216 | std::cout << " \\" << std::endl << " '" << mutex; 217 | 218 | if ( a->getFlag().empty() ) 219 | { 220 | std::cout << name; 221 | } 222 | else 223 | { 224 | std::cout << "'{" << flag << ',' << name << "}'"; 225 | } 226 | if ( theDelimiter == '=' && a->isValueRequired() ) 227 | std::cout << "=-"; 228 | quoteSpecialChars(desc); 229 | std::cout << '[' << desc << ']'; 230 | 231 | if ( a->isValueRequired() ) 232 | { 233 | std::string arg = a->shortID(); 234 | arg.erase(0, arg.find_last_of(theDelimiter) + 1); 235 | if ( arg.at(arg.length()-1) == ']' ) 236 | arg.erase(arg.length()-1); 237 | if ( arg.at(arg.length()-1) == ']' ) 238 | { 239 | arg.erase(arg.length()-1); 240 | } 241 | if ( arg.at(0) == '<' ) 242 | { 243 | arg.erase(arg.length()-1); 244 | arg.erase(0, 1); 245 | } 246 | size_t p = arg.find('|'); 247 | if ( p != std::string::npos ) 248 | { 249 | do 250 | { 251 | arg.replace(p, 1, 1, ' '); 252 | } 253 | while ( (p = arg.find_first_of('|', p)) != std::string::npos ); 254 | quoteSpecialChars(arg); 255 | std::cout << ": :(" << arg << ')'; 256 | } 257 | else 258 | { 259 | std::cout << ':' << arg; 260 | std::map::iterator compArg = common.find(arg); 261 | if ( compArg != common.end() ) 262 | { 263 | std::cout << ':' << compArg->second; 264 | } 265 | } 266 | } 267 | 268 | std::cout << '\''; 269 | } 270 | 271 | inline std::string ZshCompletionOutput::getMutexList( CmdLineInterface& _cmd, Arg* a) 272 | { 273 | XorHandler xorHandler = _cmd.getXorHandler(); 274 | std::vector< std::vector > xorList = xorHandler.getXorList(); 275 | 276 | if (a->getName() == "help" || a->getName() == "version") 277 | { 278 | return "(-)"; 279 | } 280 | 281 | std::ostringstream list; 282 | if ( a->acceptsMultipleValues() ) 283 | { 284 | list << '*'; 285 | } 286 | 287 | for ( int i = 0; static_cast(i) < xorList.size(); i++ ) 288 | { 289 | for ( ArgVectorIterator it = xorList[i].begin(); 290 | it != xorList[i].end(); 291 | it++) 292 | if ( a == (*it) ) 293 | { 294 | list << '('; 295 | for ( ArgVectorIterator iu = xorList[i].begin(); 296 | iu != xorList[i].end(); 297 | iu++ ) 298 | { 299 | bool notCur = (*iu) != a; 300 | bool hasFlag = !(*iu)->getFlag().empty(); 301 | if ( iu != xorList[i].begin() && (notCur || hasFlag) ) 302 | list << ' '; 303 | if (hasFlag) 304 | list << (*iu)->flagStartChar() << (*iu)->getFlag() << ' '; 305 | if ( notCur || hasFlag ) 306 | list << (*iu)->nameStartString() << (*iu)->getName(); 307 | } 308 | list << ')'; 309 | return list.str(); 310 | } 311 | } 312 | 313 | // wasn't found in xor list 314 | if (!a->getFlag().empty()) { 315 | list << "(" << a->flagStartChar() << a->getFlag() << ' ' << 316 | a->nameStartString() << a->getName() << ')'; 317 | } 318 | 319 | return list.str(); 320 | } 321 | 322 | } //namespace TCLAP 323 | #endif 324 | --------------------------------------------------------------------------------