├── .editorconfig ├── .gitattributes ├── .gitignore ├── COPYING ├── README ├── benchmarking └── tiling │ ├── README │ ├── gdal │ ├── .gitignore │ ├── Makefile │ ├── gdal_image_tiles_test.cpp │ ├── results.txt │ └── run.sh │ └── gil │ ├── io2 │ ├── Makefile │ ├── io2_tiff_image_tiles_test.cpp │ └── io2_wic_image_tiles_test.cpp │ └── io_new │ └── io_new_read_lots_jpeg_images.cpp ├── boost └── build │ ├── Jamfile │ ├── Jamroot │ ├── config.jam │ └── prj-config.jam ├── cmake ├── CMakeLists.txt ├── README ├── cmake_build_type.h.in └── cmake_ndebug_test.cpp ├── endian ├── endian.hpp ├── endian_example.cpp └── msvc80 │ ├── .gitignore │ ├── endian.sln │ ├── endian.vcproj │ └── endian.vsprops ├── gdal └── ogr_collinear_simplify.cpp ├── postgresql ├── .gitignore ├── CMakeLists.txt ├── README ├── configure.bat └── numeric │ ├── CMakeLists.txt │ └── postgresql_binary_numeric.cpp ├── python ├── .gitignore ├── CMakeLists.txt ├── README ├── build.bat ├── emb │ ├── CMakeLists.txt │ └── emb.cpp ├── faq_incomplete_input.cpp ├── serial_subinterpreters.cpp ├── simple.cpp └── tkinter.cpp ├── scripts ├── create_postgis_template.sh ├── gdalbb.py └── svnkeywords.sh ├── soci ├── examples │ ├── example1.cpp │ ├── example1.vcproj │ ├── example2.cpp │ ├── example2.vcproj │ └── examples.sln └── soci.vsprops ├── wkt ├── Makefile ├── README ├── error_handling_test.cpp ├── ogc_wkt_grammar.hpp ├── ogc_wkt_grammar_test.cpp ├── ogc_wkt_validator.hpp └── ogc_wkt_validator_test.cpp └── yatbinrw ├── msvc80 ├── .gitignore ├── yatbinrw.sln ├── yatbinrw.vcproj └── yatbinrw.vsprops ├── yatbinrw.hpp └── yatbinrw_example.cpp /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig is awesome: http://EditorConfig.org 2 | # 3 | # NOTE: Keep settings in sync with the master .clang-format file 4 | # 5 | # top-most EditorConfig file 6 | root = true 7 | 8 | # C++ source files 9 | [*.{cpp,h,hpp}] 10 | indent_style = space 11 | indent_size = 4 12 | max_line_length = 100 13 | insert_final_newline = true 14 | trim_trailing_whitespace = true 15 | 16 | # CMake configuration files 17 | [{CMakeLists.txt,CMakeSettings.json,*.cmake}] 18 | indent_size = 2 19 | indent_style = space 20 | trim_trailing_whitespace = true 21 | 22 | # CI configuration files 23 | [{.travis.yml,appveyor.yml,Vagrantfile}] 24 | indent_size = 2 25 | indent_style = space 26 | trim_trailing_whitespace = true 27 | 28 | # Markdown/reStructuredText documentation files 29 | [*.{md,markdown,rst}] 30 | indent_size = 2 31 | indent_style = space 32 | trim_trailing_whitespace = true 33 | max_line_length = 80 34 | 35 | # Windows shell scripts 36 | [*.{cmd,bat,ps1}] 37 | end_of_line = crlf 38 | indent_size = 4 39 | indent_style = space 40 | insert_final_newline = false 41 | trim_trailing_whitespace = true 42 | 43 | # Unix shell scripts 44 | [*.sh] 45 | end_of_line = lf 46 | indent_size = 4 47 | indent_style = space 48 | insert_final_newline = false 49 | trim_trailing_whitespace = true 50 | 51 | # Python scripts 52 | [*.{py}] 53 | indent_size = 4 54 | indent_style = space 55 | 56 | [COPYING] 57 | end_of_line = lf 58 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set the default behavior, in case people don't have core.autocrlf set. 2 | * text=auto 3 | 4 | # Explicitly declare text files you want to always be normalized and converted 5 | # to native line endings on checkout. 6 | *.h text 7 | *.hpp text 8 | *.cpp text 9 | 10 | # Declare files that will always have CRLF line endings on checkout. 11 | *.bat text eol=crlf 12 | *.cmd text eol=crlf 13 | *.ps1 text eol=crlf 14 | 15 | # Declare files that will always have LF line endings on checkout. 16 | *.sh text eol=lf 17 | LICENSE text eol=lf -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.kate-swp 3 | _build* 4 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | Copyright © 2006-2009 Mateusz Loskot 2 | 3 | Copyright mostly belongs to Mateusz Loskot , 4 | except for quoted material. 5 | 6 | If not stated differently in particular files, all the source code in this 7 | repository is available under the terms of Boost License available at 8 | http://www.opensource.org/licenses/bsl1.0.html and also quoted below: 9 | 10 | --- 11 | Boost Software License - Version 1.0 12 | 13 | Permission is hereby granted, free of charge, to any person or organization 14 | obtaining a copy of the software and accompanying documentation covered by 15 | this license (the "Software") to use, reproduce, display, distribute, 16 | execute, and transmit the Software, and to prepare derivative works of the 17 | Software, and to permit third-parties to whom the Software is furnished to 18 | do so, all subject to the following: 19 | 20 | The copyright notices in the Software and this entire statement, including 21 | the above license grant, this restriction and the following disclaimer, 22 | must be included in all copies of the Software, in whole or in part, and 23 | all derivative works of the Software, unless such copies or derivative 24 | works are solely in the form of machine-executable object code generated by 25 | a source language processor. 26 | 27 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 28 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 29 | FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 30 | SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 31 | FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 32 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 33 | DEALINGS IN THE SOFTWARE. 34 | --- 35 | 36 | 37 | Some files like Autoconf macros use simple all-permissive license: 38 | 39 | --- 40 | Copyright © 2006-2009 Mateusz Loskot 41 | 42 | Copying and distribution of this file, with or without modification, 43 | are permitted in any medium without royalty provided the copyright 44 | notice and this notice are preserved. 45 | --- 46 | -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | URL: http://github.com/mloskot/workshop/ 2 | 3 | For licensing and copyright information check the COPYING file. 4 | 5 | -- 6 | Mateusz Loskot, mateusz at loskot dot net 7 | http://mateusz.loskot.net/ 8 | -------------------------------------------------------------------------------- /benchmarking/tiling/README: -------------------------------------------------------------------------------- 1 | This is a simple benchmark of raster tiling. 2 | The idea was discussed on Boost mailing list during review of Boost.GIL IO 3 | and Toolbox extensions: 4 | 5 | http://lists.boost.org/Archives/boost/2010/12/173902.php 6 | 7 | The goal is to compare GDAL versus GIL IO (Christian's implementation) 8 | and IO2 (Domagoj's, available from Boost Sandbox) in simple tiling use case. 9 | -------------------------------------------------------------------------------- /benchmarking/tiling/gdal/.gitignore: -------------------------------------------------------------------------------- 1 | output 2 | -------------------------------------------------------------------------------- /benchmarking/tiling/gdal/Makefile: -------------------------------------------------------------------------------- 1 | ifeq ($(JPG), yes) 2 | CPPFLAGS+=-DTEST_OUTPUT_JPG=1 3 | endif 4 | 5 | GDAL_LIBS=$(shell gdal-config --libs) 6 | GDAL_DEPS=$(shell gdal-config --dep-libs) 7 | LIBS=$(GDAL_LIBS) $(GDAL_DEPS) 8 | 9 | CXX=g++ 10 | 11 | all: release200 release256 release512 12 | 13 | debug: debug200 debug256 debug512 14 | 15 | debug200: gdal_image_tiles_test.cpp 16 | $(CXX) -g $(CXXFLAGS) $(CPPFLAGS) -DTEST_TILE_200 -o gdal_image_tiles_test_200_d gdal_image_tiles_test.cpp $(LIBS) 17 | 18 | release200: gdal_image_tiles_test.cpp 19 | $(CXX) -O2 -DNDEBUG $(CXXFLAGS) $(CPPFLAGS) -DTEST_TILE_200 -o gdal_image_tiles_test_200 gdal_image_tiles_test.cpp $(LIBS) 20 | 21 | debug256: gdal_image_tiles_test.cpp 22 | $(CXX) -g $(CXXFLAGS) $(CPPFLAGS) -DTEST_TILE_256 -o gdal_image_tiles_test_256_d gdal_image_tiles_test.cpp $(LIBS) 23 | 24 | release256: gdal_image_tiles_test.cpp 25 | $(CXX) -O2 -DNDEBUG $(CXXFLAGS) $(CPPFLAGS) -DTEST_TILE_256 -o gdal_image_tiles_test_256 gdal_image_tiles_test.cpp $(LIBS) 26 | 27 | debug512: gdal_image_tiles_test.cpp 28 | $(CXX) -g $(CXXFLAGS) $(CPPFLAGS) -DTEST_TILE_512 -o gdal_image_tiles_test_512_d gdal_image_tiles_test.cpp $(LIBS) 29 | 30 | release512: gdal_image_tiles_test.cpp 31 | $(CXX) -O2 -DNDEBUG $(CXXFLAGS) $(CPPFLAGS) -DTEST_TILE_512 -o gdal_image_tiles_test_512 gdal_image_tiles_test.cpp $(LIBS) 32 | 33 | clean: 34 | $(RM) *.o *_200* *_256* *_512* 35 | $(RM) output/*.png 36 | $(RM) output/*.jpg -------------------------------------------------------------------------------- /benchmarking/tiling/gdal/gdal_image_tiles_test.cpp: -------------------------------------------------------------------------------- 1 | // Benchmark of raster tiling using GDAL 2 | // 3 | // Copyright (c) 2010 Mateusz Loskot 4 | // Copyright (c) 2010 Domagoj Saric 5 | // 6 | // Use, modification and distribution is subject to the Boost Software License, 7 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 8 | // http://www.boost.org/LICENSE_1_0.txt) 9 | // 10 | // Preprocessor definitions: 11 | // TEST_TILE_200 12 | // TEST_TILE_256 13 | // TEST_TILE_512 14 | // TEST_OUTPUT_JPG (default output is PNG) 15 | // 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | typedef boost::shared_ptr dataset_t; 26 | typedef std::vector raster_data_t; 27 | 28 | dataset_t make_dataset(GDALDataset* pds) 29 | { 30 | dataset_t ds(pds, ::GDALClose); 31 | return ds; 32 | } 33 | dataset_t open_dataset(char const* const file) 34 | { 35 | dataset_t ds(make_dataset(static_cast(::GDALOpen(file, ::GA_ReadOnly)))); 36 | if(!ds) 37 | { 38 | throw std::runtime_error("failed to open dataset"); 39 | } 40 | return ds; 41 | } 42 | 43 | GDALDriver& find_driver(char const* const name) 44 | { 45 | GDALDriver* drv = GetGDALDriverManager()->GetDriverByName(name); 46 | if (0 == drv) 47 | { 48 | throw std::runtime_error("driver not found"); 49 | } 50 | return *drv; 51 | } 52 | 53 | dataset_t create_tile_dataset(GDALDriver& drv, unsigned int tile_xsize, unsigned int tile_ysize) 54 | { 55 | dataset_t ds(make_dataset(drv.Create("", tile_xsize, tile_ysize, 3, ::GDT_Byte, 0))); 56 | if(!ds) 57 | { 58 | throw std::runtime_error("failed to create output dataset"); 59 | } 60 | return ds; 61 | } 62 | 63 | void save_tile(GDALDriver& drv, dataset_t dstile, char const* const file) 64 | { 65 | dataset_t dspng(make_dataset(drv.CreateCopy(file, dstile.get(), false, 0, 0, 0))); 66 | if(!dspng) 67 | { 68 | throw std::runtime_error("failed to save tile"); 69 | } 70 | } 71 | 72 | void read(dataset_t ds, raster_data_t& data, int x, int y, int xs, int ys, int nbands) 73 | { 74 | assert(data.size() == xs * ys * nbands); 75 | 76 | ::CPLErr err = ds->RasterIO(::GF_Read, x, y, xs, ys, &data[0], 77 | xs, ys, ::GDT_Byte, nbands, 0, 0, 0, 0); 78 | if (::CE_None != err) 79 | { 80 | throw std::runtime_error("raster read failed"); 81 | } 82 | } 83 | 84 | void write(dataset_t ds, raster_data_t const& data, int x, int y, int xs, int ys, int nbands) 85 | { 86 | assert(data.size() == xs * ys * nbands); 87 | 88 | ::CPLErr err = ds->RasterIO(::GF_Write, x, y, xs, ys, 89 | const_cast(&data[0]), 90 | xs, ys, ::GDT_Byte, nbands, 0, 0, 0, 0); 91 | if (::CE_None != err) 92 | { 93 | throw std::runtime_error("raster write failed"); 94 | } 95 | } 96 | 97 | void convert_int_to_hex(unsigned int const integer, char* p_char_buffer) 98 | { 99 | static char const hexDigits[] = "0123456789ABCDEF"; 100 | unsigned char const* p_current_byte(reinterpret_cast(&integer)); 101 | unsigned char const* const p_end(p_current_byte + sizeof(integer)); 102 | while (p_current_byte != p_end) 103 | { 104 | *p_char_buffer-- = hexDigits[ ( ( *p_current_byte ) & 0x0F ) ]; 105 | *p_char_buffer-- = hexDigits[ ( ( *p_current_byte ) & 0xF0 ) >> 4 ]; 106 | ++p_current_byte; 107 | } 108 | } 109 | 110 | int main() 111 | { 112 | using namespace std; 113 | 114 | try 115 | { 116 | ::GDALAllRegister(); 117 | 118 | char input_file_name[] = "/home/mloskot/data/truemarble/TrueMarble.250m.21600x21600.E2.tif"; 119 | 120 | #ifdef TEST_OUTPUT_JPG 121 | char const* const output_driver_name = "JPEG"; 122 | char output_file_name[] = "__out_tile__00000000.jpg"; 123 | char* const p_back_of_file_name_number( 124 | boost::end(output_file_name) - (boost::size(".jpg") + 1)); 125 | #else 126 | char const* const output_driver_name = "PNG"; 127 | char output_file_name[] = "__out_tile__00000000.png"; 128 | char* const p_back_of_file_name_number( 129 | boost::end(output_file_name) - (boost::size(".png") + 1)); 130 | #endif 131 | 132 | #if defined(TEST_TILE_256) 133 | unsigned int const output_tile_size(256); 134 | #elif defined(TEST_TILE_512) 135 | unsigned int const output_tile_size(512); 136 | #else 137 | unsigned int const output_tile_size(200); 138 | #endif 139 | 140 | GDALDriver& memory_driver = find_driver("MEM"); 141 | GDALDriver& output_driver = find_driver(output_driver_name); 142 | 143 | dataset_t input_dataset(open_dataset(input_file_name)); 144 | int const nbands = input_dataset->GetRasterCount(); 145 | assert(3 == nbands); 146 | int const xsize = input_dataset->GetRasterXSize(); 147 | int const ysize = input_dataset->GetRasterYSize(); 148 | unsigned int output_tile_counter(0); 149 | 150 | unsigned int tile_xsize = output_tile_size; 151 | unsigned int tile_ysize = output_tile_size; 152 | raster_data_t data(tile_xsize * tile_ysize * nbands); 153 | 154 | for (int x(0); x < xsize; x += output_tile_size) 155 | { 156 | tile_xsize = x + output_tile_size > xsize ? xsize - x : output_tile_size; 157 | 158 | for (int y(0); y < ysize; y += output_tile_size) 159 | { 160 | tile_ysize = y + output_tile_size > ysize ? ysize - y : output_tile_size; 161 | unsigned int const tile_size = tile_xsize * tile_ysize * nbands; 162 | if (data.size() != tile_size) 163 | data.resize(tile_size); 164 | 165 | read(input_dataset, data, x, y, tile_xsize, tile_ysize, nbands); 166 | 167 | convert_int_to_hex(output_tile_counter++, p_back_of_file_name_number); 168 | dataset_t tile_dataset(create_tile_dataset(memory_driver, tile_xsize, tile_ysize)); 169 | 170 | write(tile_dataset, data, 0, 0, tile_xsize, tile_ysize, nbands); 171 | save_tile(output_driver, tile_dataset, output_file_name); 172 | 173 | #ifndef NDEBUG 174 | if (tile_xsize != output_tile_size || tile_ysize != output_tile_size) 175 | clog << output_file_name << " " << tile_xsize << "\tx\t" << tile_ysize << endl; 176 | #endif 177 | } 178 | } 179 | } 180 | catch (exception const& e) 181 | { 182 | cerr << e.what() << endl; 183 | } 184 | 185 | return 0; 186 | } 187 | -------------------------------------------------------------------------------- /benchmarking/tiling/gdal/results.txt: -------------------------------------------------------------------------------- 1 | = Linux Ubuntu 10.10 (amd64) = 2 | 3 | * CPU: Intel Core 2 Duo P8600 @ 2.40GHz 4 | * RAM: 4 GB 5 | * GCC 4.4.5 6 | * GDAL 1.8dev (SVN trunk) 7 | * Optimisation -O2 8 | * Runtime settings: 9 | ** GDAL_CACHEMAX=512 (http://trac.osgeo.org/gdal/wiki/ConfigOptions) 10 | ** Create tile as MEM dataset (http://gdal.org/frmt_mem.html), then CreateCopy to PNG or JPEG file. 11 | ** Befre each run, all disk caches had been dropped (http://kernelnewbies.org/Linux_2_6_16) 12 | 13 | $ sync (optional) 14 | $ echo 3 | sudo tee -a /proc/sys/vm/drop_caches 15 | 16 | How to run: 17 | 18 | $ make 19 | $ ./run.sh 20 | $ make clean 21 | $ JPG=yes make 22 | $ ./run.sh 23 | 24 | == PNG == 25 | 26 | http://gdal.org/frmt_various.html#PNG 27 | 28 | ZLEVEL=6 (default compression level) 29 | 30 | === Tile size 200x200 === 31 | 32 | $ time ../gdal_image_tiles_test_200 33 | real 11m52.005s 34 | user 10m12.720s 35 | sys 0m38.290s 36 | 37 | $ ls -1 | wc -l 38 | 11664 39 | 40 | $ du -sh . 41 | 460M . 42 | 43 | === Tile size 256x256 === 44 | 45 | $ time ../gdal_image_tiles_test_256 46 | 47 | real 11m51.904s 48 | user 10m11.100s 49 | sys 0m17.570s 50 | 51 | $ ls -1 | wc -l 52 | 7225 53 | 54 | $ du -sh . 55 | 455M . 56 | 57 | === Tile Size 512x512 === 58 | 59 | $ time ../gdal_image_tiles_test_512 60 | 61 | real 11m26.673s 62 | user 10m11.360s 63 | sys 0m4.990s 64 | 65 | $ ls -1 | wc -l 66 | 1849 67 | 68 | $ du -sh . 69 | 432M . 70 | 71 | == JPEG == 72 | 73 | http://gdal.org/frmt_jpeg.html 74 | 75 | QUALITY=75 (default quality level) 76 | 77 | === Tile size 200x200 === 78 | 79 | $ time ../gdal_image_tiles_test_200 80 | 81 | real 2m13.756s 82 | user 1m36.270s 83 | sys 0m34.350s 84 | 85 | $ ls -1 | wc -l 86 | 11664 87 | 88 | $ du -sh . 89 | 67M . 90 | 91 | === Tile size 256x256 === 92 | 93 | $ time ../gdal_image_tiles_test_256 94 | 95 | real 2m31.506s 96 | user 1m23.580s 97 | sys 0m14.770s 98 | 99 | $ ls -1 | wc -l 100 | 7225 101 | 102 | $ du -sh . 103 | 55M . 104 | 105 | === Tile size 512x512 === 106 | 107 | $ time ../gdal_image_tiles_test_512 108 | 109 | real 2m22.735s 110 | user 1m14.230s 111 | sys 0m3.220s 112 | 113 | $ ls -1 | wc -l 114 | 1849 115 | 116 | $ du -sh . 117 | 42M . 118 | -------------------------------------------------------------------------------- /benchmarking/tiling/gdal/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | if test -d output; then 3 | rm -rf output 4 | fi 5 | mkdir output 6 | cd output 7 | echo "200x200" 8 | echo 3 | sudo tee -a /proc/sys/vm/drop_caches 9 | echo 0 | sudo tee -a /proc/sys/vm/drop_caches 10 | time ../gdal_image_tiles_test_200 11 | rm -f *png 12 | echo "256x256" 13 | echo 3 | sudo tee -a /proc/sys/vm/drop_caches 14 | echo 0 | sudo tee -a /proc/sys/vm/drop_caches 15 | time ../gdal_image_tiles_test_256 16 | rm -f *png 17 | echo "512x512" 18 | echo 3 | sudo tee -a /proc/sys/vm/drop_caches 19 | echo 0 | sudo tee -a /proc/sys/vm/drop_caches 20 | time ../gdal_image_tiles_test_512 21 | echo "Done!" 22 | cd .. 23 | rm -rf output 24 | echo 0 | sudo tee -a /proc/sys/vm/drop_caches -------------------------------------------------------------------------------- /benchmarking/tiling/gil/io2/Makefile: -------------------------------------------------------------------------------- 1 | LIBS=-ltiff 2 | INC=-I/home/mloskot/dev/boost/_svn/sandbox/gil 3 | PRG=io2_tiff_image_tiles_test 4 | SRC=$(PRG).cpp 5 | CXX=g++ 6 | 7 | all: release200 release256 release512 8 | 9 | debug: debug200 debug256 debug512 10 | 11 | debug200: $(SRC) 12 | $(CXX) -g $(CXXFLAGS) $(CPPFLAGS) $(INC) $(LIBS) -o $(PRG) $(SRC) 13 | 14 | release200: $(SRC) 15 | $(CXX) -O2 -DNDEBUG $(CXXFLAGS) $(CPPFLAGS) $(INC) $(LIBS) -o $(PRG) $(SRC) 16 | 17 | clean: 18 | $(RM) *.o 19 | $(RM) output/*.png 20 | $(RM) output/*.jpg -------------------------------------------------------------------------------- /benchmarking/tiling/gil/io2/io2_tiff_image_tiles_test.cpp: -------------------------------------------------------------------------------- 1 | //#define BOOST_GIL_EXTERNAL_LIB ( BOOST_LIB_LINK_LOADTIME_OR_STATIC, BOOST_LIB_LOADING_STATIC, BOOST_LIB_INIT_ASSUME ) 2 | #define BOOST_EXCEPTION_DISABLE 3 | 4 | #include "boost/gil/extension/io2/libtiff_image.hpp" 5 | 6 | void convert_int_to_hex( unsigned int integer, wchar_t * p_char_buffer ); 7 | 8 | int __cdecl main( int /*argc*/, char * /*argv*/[] ) 9 | { 10 | using namespace boost; 11 | using namespace boost::gil; 12 | 13 | libtiff_image::guard const tiff_guard; 14 | 15 | typedef image tile_holder_t; 16 | 17 | typedef libtiff_image::reader_for::type reader_t; 18 | reader_t reader( L"TrueMarble.250m.21600x21600.E2.tif" ); 19 | 20 | /* 21 | std::size_t const output_tile_size( 512 ); 22 | tile_holder_t output_tile_holder( output_tile_size, output_tile_size ); 23 | typedef wic_image::writer_for::type writer_t; 24 | 25 | wchar_t output_file_name[] = L"__out_tile__00000000.png"; 26 | wchar_t * const p_back_of_file_name_number( boost::end( output_file_name ) - ( boost::size( L".png" ) + 1 ) ); 27 | 28 | unsigned int output_tile_counter( 0 ); 29 | point2 const input_dimensions ( reader.dimensions() ); 30 | for ( unsigned int x( 0 ); x < input_dimensions.x; x += output_tile_size ) 31 | { 32 | for ( unsigned int y( 0 ); y < input_dimensions.y; y += output_tile_size ) 33 | { 34 | reader.copy_to 35 | ( 36 | reader_t::offset_view 37 | ( 38 | view( output_tile_holder ), 39 | reader_t::offset_t( x, y ) 40 | ), 41 | assert_dimensions_match(), 42 | assert_formats_match () 43 | ); 44 | 45 | convert_int_to_hex( output_tile_counter++, p_back_of_file_name_number ); 46 | writer_t( output_file_name, view( output_tile_holder ), png ).write_default(); 47 | } 48 | } 49 | */ 50 | return 0; 51 | } 52 | 53 | void convert_int_to_hex( unsigned int const integer, wchar_t * p_char_buffer ) 54 | { 55 | static wchar_t const hexDigits[] = L"0123456789ABCDEF"; 56 | unsigned char const * p_current_byte( reinterpret_cast( &integer ) ); 57 | unsigned char const * const p_end ( p_current_byte + sizeof( integer ) ); 58 | while ( p_current_byte != p_end ) 59 | { 60 | *p_char_buffer-- = hexDigits[ ( ( *p_current_byte ) & 0x0F ) ]; 61 | *p_char_buffer-- = hexDigits[ ( ( *p_current_byte ) & 0xF0 ) >> 4 ]; 62 | ++p_current_byte; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /benchmarking/tiling/gil/io2/io2_wic_image_tiles_test.cpp: -------------------------------------------------------------------------------- 1 | #define BOOST_GIL_EXTERNAL_LIB ( BOOST_LIB_LINK_LOADTIME_OR_STATIC, BOOST_LIB_LOADING_STATIC, BOOST_LIB_INIT_ASSUME ) 2 | #define BOOST_EXCEPTION_DISABLE 3 | 4 | #include "boost/gil/extension/io2/wic_image.hpp" 5 | 6 | 7 | void convert_int_to_hex( unsigned int integer, wchar_t * p_char_buffer ); 8 | 9 | int __cdecl main( int /*argc*/, char * /*argv*/[] ) 10 | { 11 | using namespace boost; 12 | using namespace boost::gil; 13 | 14 | wic_image::guard const wic_guard; 15 | 16 | typedef image tile_holder_t; 17 | 18 | typedef wic_image::reader_for::type reader_t; 19 | reader_t reader( L"TrueMarble.250m.21600x21600.E2.tif" ); 20 | 21 | std::size_t const output_tile_size( 512 ); 22 | tile_holder_t output_tile_holder( output_tile_size, output_tile_size ); 23 | typedef wic_image::writer_for::type writer_t; 24 | 25 | wchar_t output_file_name[] = L"__out_tile__00000000.png"; 26 | wchar_t * const p_back_of_file_name_number( boost::end( output_file_name ) - ( boost::size( L".png" ) + 1 ) ); 27 | 28 | unsigned int output_tile_counter( 0 ); 29 | point2 const input_dimensions ( reader.dimensions() ); 30 | for ( unsigned int x( 0 ); x < input_dimensions.x; x += output_tile_size ) 31 | { 32 | for ( unsigned int y( 0 ); y < input_dimensions.y; y += output_tile_size ) 33 | { 34 | reader.copy_to 35 | ( 36 | reader_t::offset_view 37 | ( 38 | view( output_tile_holder ), 39 | reader_t::offset_t( x, y ) 40 | ), 41 | assert_dimensions_match(), 42 | assert_formats_match () 43 | ); 44 | 45 | convert_int_to_hex( output_tile_counter++, p_back_of_file_name_number ); 46 | writer_t( output_file_name, view( output_tile_holder ), png ).write_default(); 47 | } 48 | } 49 | 50 | return 0; 51 | } 52 | 53 | void convert_int_to_hex( unsigned int const integer, wchar_t * p_char_buffer ) 54 | { 55 | static wchar_t const hexDigits[] = L"0123456789ABCDEF"; 56 | unsigned char const * p_current_byte( reinterpret_cast( &integer ) ); 57 | unsigned char const * const p_end ( p_current_byte + sizeof( integer ) ); 58 | while ( p_current_byte != p_end ) 59 | { 60 | *p_char_buffer-- = hexDigits[ ( ( *p_current_byte ) & 0x0F ) ]; 61 | *p_char_buffer-- = hexDigits[ ( ( *p_current_byte ) & 0xF0 ) >> 4 ]; 62 | ++p_current_byte; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /benchmarking/tiling/gil/io_new/io_new_read_lots_jpeg_images.cpp: -------------------------------------------------------------------------------- 1 | // io2_test_vc10.cpp : Defines the entry point for the console application. 2 | // 3 | 4 | #include "stdafx.h" 5 | 6 | #define TEST_IO_NEW 7 | 8 | #define BOOST_FILESYSTEM_VERSION 2 9 | 10 | #define _CRT_SECURE_NO_WARNINGS 1 11 | #define _SCL_SECURE_NO_WARNINGS 1 12 | 13 | #ifdef NDEBUG 14 | #define _SECURE_SCL 0 15 | #define _HAS_ITERATOR_DEBUGGING 0 16 | #endif 17 | 18 | #include 19 | #include 20 | 21 | #include 22 | 23 | #include 24 | 25 | #include 26 | #include 27 | 28 | unsigned int counter = 0; 29 | 30 | 31 | #ifdef TEST_IO_NEW 32 | 33 | #include 34 | 35 | using namespace std; 36 | using namespace boost; 37 | using namespace gil; 38 | 39 | namespace fs = boost::filesystem; 40 | 41 | rgb8_image_t img = rgb8_image_t( 136, 98 ); 42 | 43 | 44 | void read_image( const fs::path& dir_path ) 45 | { 46 | if ( fs::is_directory( dir_path ) ) 47 | { 48 | fs::directory_iterator end_iter; 49 | for( fs::directory_iterator dir_itr( dir_path ) 50 | ; dir_itr != end_iter 51 | ; ++dir_itr 52 | ) 53 | { 54 | if ( fs::is_regular( dir_itr->status() ) 55 | && ( fs::extension ( dir_itr->path() ) == ".jpg" ) 56 | ) 57 | { 58 | string filename = dir_itr->path().string(); 59 | read_image( filename, img, jpeg_tag() ); 60 | 61 | ++counter; 62 | } 63 | else if( is_directory(dir_itr->status()) ) 64 | { 65 | read_image( dir_itr->path() ); 66 | } 67 | } 68 | } 69 | } 70 | 71 | int _tmain(int argc, _TCHAR* argv[]) 72 | { 73 | string in( "C:/chhenning/lots_of_jpegs/" ); 74 | //string in( "C:/chhenning/few_jpegs/" ); 75 | fs::path in_path = fs::system_complete( fs::path( in, fs::native ) ); 76 | 77 | try 78 | { 79 | timer t; 80 | 81 | read_image( in_path ); 82 | 83 | cout << "# files : " << counter << endl; 84 | cout << "time : " << t.elapsed() << " sec" << endl; 85 | } 86 | catch( std::exception& e ) 87 | { 88 | cout << e.what() << endl; 89 | 90 | exit( 1 ); 91 | } 92 | } 93 | 94 | #else 95 | 96 | #include 97 | 98 | using namespace std; 99 | using namespace boost; 100 | using namespace gil; 101 | 102 | namespace fs = boost::filesystem; 103 | 104 | rgb8_image_t img = rgb8_image_t( 136, 98 ); 105 | 106 | void read_image( const fs::path& dir_path ) 107 | { 108 | if ( fs::is_directory( dir_path ) ) 109 | { 110 | fs::directory_iterator end_iter; 111 | for( fs::directory_iterator dir_itr( dir_path ) 112 | ; dir_itr != end_iter 113 | ; ++dir_itr 114 | ) 115 | { 116 | if ( fs::is_regular( dir_itr->status() ) 117 | && ( fs::extension ( dir_itr->path() ) == ".jpg" ) 118 | ) 119 | { 120 | string filename = dir_itr->path().string(); 121 | libjpeg_image::read( filename.c_str(), img ); 122 | 123 | ++counter; 124 | } 125 | else if( is_directory(dir_itr->status()) ) 126 | { 127 | read_image( dir_itr->path() ); 128 | } 129 | } 130 | } 131 | } 132 | 133 | int _tmain(int argc, _TCHAR* argv[]) 134 | { 135 | string in( "C:/chhenning/lots_of_jpegs/" ); 136 | //string in( "C:/chhenning/few_jpegs/" ); 137 | fs::path in_path = fs::system_complete( fs::path( in, fs::native ) ); 138 | 139 | try 140 | { 141 | timer t; 142 | 143 | read_image( in_path ); 144 | 145 | cout << "# files : " << counter << endl; 146 | cout << "time : " << t.elapsed() << " sec" << endl; 147 | } 148 | catch( std::exception& e ) 149 | { 150 | cout << e.what() << endl; 151 | 152 | exit( 1 ); 153 | } 154 | } 155 | #endif -------------------------------------------------------------------------------- /boost/build/Jamfile: -------------------------------------------------------------------------------- 1 | import config ; 2 | 3 | if [ config.is-enabled dbdrivers ] 4 | { 5 | local odbc-support ; 6 | if [ config.is-configured unixodbc ] 7 | { 8 | local odbc_inc = [ config.include-feature unixodbc ] ; 9 | lib odbc : : odbc [ config.search-feature unixodbc ] ; 10 | odbc-support = yes ; 11 | } 12 | else if [ config.is-configured iodbc ] 13 | { 14 | local odbc_inc = [ config.include-feature iodbc ] ; 15 | lib odbc : : iodbc [ config.search-feature iodbc ] ; 16 | odbc-support = yes ; 17 | } 18 | if $(odbc-support) = yes 19 | { 20 | lib tolodbc 21 | : libtolodbc.c odbc 22 | : $(odbc_inc) 23 | ; 24 | } 25 | 26 | if [ config.is-configured mysql ] 27 | { 28 | lib mysql : : mysqlclient [ config.search-feature mysql ] ; 29 | lib tolmysql 30 | : libtolmysql.c mysql 31 | : [ config.include-feature mysql ] 32 | ; 33 | } 34 | 35 | if [ config.is-configured pgsql ] 36 | { 37 | lib pgsql : : pq [ config.search-feature pgsql ] ; 38 | lib tolpgsql 39 | : libtolpgsql.c pgsql 40 | : [ config.include-feature pgsql ] 41 | ; 42 | } 43 | } -------------------------------------------------------------------------------- /boost/build/Jamroot: -------------------------------------------------------------------------------- 1 | import modules config ; 2 | 3 | list_opts = --with-gsl= --with-blas --with-cholmod=/usr/local/cholmod --enable-DS --prefix=/usr/local/tol --with-gsl-includes=/usr/local/gsl-1.9/includes --with-gsl-libraries=/usr/local/gsl-1.9/lib ; 4 | 5 | result = [ MATCH --with-(.+)=(.*) : $(list_opts) ] ; 6 | 7 | # echo [ MATCH "--(with|enable)-(.+)|(prefix)=(.+)" : --prefix ] ; 8 | # echo [ MATCH "--(with|enable)-(.+)|(prefix)=(.+)" : --enable-DS ] ; 9 | # echo [ MATCH "--(with|enable)-(.+)|(prefix)=(.+)" : --with-gsl ] ; 10 | 11 | #ECHO $(result) ; 12 | 13 | # "--(with|enable)-([^=]+)(=(.*))?" 14 | 15 | local all_pkgs ; 16 | local all_enabled ; 17 | 18 | rule trim-left-nulls ( list * ) 19 | { 20 | local _list ; 21 | 22 | for local i in $(list) 23 | { 24 | if $(i) 25 | { 26 | _list += $(i) ; 27 | } 28 | } 29 | return $(_list) ; 30 | } 31 | 32 | rule cmdline ( options * ) 33 | { 34 | if $(options) 35 | { 36 | list_opts = $(options) ; 37 | } 38 | else 39 | { 40 | list_opts = [ modules.peek : ARGV ] ; 41 | } 42 | for opt in $(list_opts) 43 | { 44 | local matched = [ trim-left-nulls 45 | [ MATCH "--(with|enable)-(.+)|(prefix)=(.+)" : $(opt) ] ] ; 46 | 47 | # ECHO matched --> $(matched) ; 48 | 49 | switch $(matched[1]) 50 | { 51 | case prefix : 52 | { 53 | configure prefix : $(matched[2]) ; 54 | } 55 | case with : 56 | { 57 | local with = [ trim-left-nulls [ MATCH 58 | "([^-]+)-(includes|libraries)=(.+)|([^=]+)(=(.*))?" 59 | : $(matched[2]) ] ] ; 60 | #ECHO $(opt) --> $(with[1]) - $(with[2]) - $(with[3]) ; 61 | local p = $(with[1]) ; 62 | if ! ( $(p) in $(all_pkgs) ) 63 | { 64 | all_pkgs += $(p) ; 65 | } 66 | local sub_feat = $(with[2]) ; 67 | if [ MATCH (includes|libraries) : $(sub_feat) ] 68 | { 69 | #ECHO "pkg_$(p)_$(sub_feat) --> $(with[3]) " ; 70 | pkg_$(p)_$(sub_feat) = $(with[3]) ; 71 | } 72 | else 73 | { 74 | pkg_$(p)_prefix = $(with[3]) ; 75 | } 76 | } 77 | case enable : 78 | { 79 | all_enabled += $(matched[2]) ; 80 | enabled_$(matched[2]) += 1 ; 81 | } 82 | } 83 | } 84 | 85 | for local p in $(all_pkgs) 86 | { 87 | # ECHO configure $(p) - $(pkg_$(p)_includes) - $(pkg_$(p)_libraries) ; 88 | configure lib 89 | : $(p) : $(pkg_$(p)_prefix) : $(pkg_$(p)_includes) : $(pkg_$(p)_libraries) ; 90 | } 91 | 92 | for local f in $(all_enabled) 93 | { 94 | configure enable : $(f) ; 95 | } 96 | } 97 | 98 | cmdline ; -------------------------------------------------------------------------------- /boost/build/config.jam: -------------------------------------------------------------------------------- 1 | # $Id$ 2 | # 3 | # Copyright 2006 Jorge Suit Perez Ronda 4 | # Copyright 2006 Mateusz Loskot 5 | 6 | # Distributed under License 7 | # 8 | 9 | # Boost.Build configuration module provides rules to 10 | # manage GNU Autoconf-like options. 11 | 12 | import modules path feature ; 13 | 14 | # configure 15 | # 16 | # rule to configure packages and enables. It is a dispatching routine 17 | # First argument is what to configure: lib or enable 18 | # Rest of arguments depende on what is being configured. 19 | # Example: 20 | # 21 | # configure lib : gsl : /usr/local/gsl ; 22 | # configure enable : dbase ; 23 | rule configure 24 | { 25 | $(1) $(2) : $(3) : $(4) : $(5) : $(6) ; 26 | } 27 | 28 | # lib 29 | # 30 | # Generates configuration option for given package. 31 | rule lib ( pkg : prefix ? : incls ? : libs ? ) 32 | { 33 | if $(pkg) in $(.all.pkgs) 34 | { 35 | ECHO reconfiguring package $(pkg) ; 36 | } 37 | else 38 | { 39 | ECHO configuring package $(pkg) ; 40 | .all.pkgs += $(pkg) ; 41 | } 42 | if $(incls) 43 | { 44 | .with.$(pkg).incls = $(incls) ; 45 | } 46 | if $(libs) 47 | { 48 | .with.$(pkg).libs = $(libs) ; 49 | } 50 | if $(prefix) 51 | { 52 | .with.$(pkg).incls ?= [ path.join $(prefix) include ] ; 53 | .with.$(pkg).libs ?= [ path.join $(prefix) lib ] ; 54 | } 55 | } 56 | 57 | # is-configured 58 | # 59 | # Checks if requeted package has been configured. 60 | rule is-configured ( pkg ) 61 | { 62 | if $(pkg) in $(.all.pkgs) 63 | { 64 | return yes ; 65 | } 66 | } 67 | 68 | # include-feature 69 | # 70 | # Generates attribute for a package. If pkg is ommited 71 | # all features configured are returned. 72 | rule include-feature ( pkg ? ) 73 | { 74 | if $(pkg) 75 | { 76 | if $(.with.$(pkg).incls) 77 | { 78 | return $(.with.$(pkg).incls) ; 79 | } 80 | } 81 | else 82 | { 83 | local incls ; 84 | for local p in $(.all.pkgs) 85 | { 86 | if $(.with.$(p).incls) 87 | { 88 | incls += $(.with.$(p).incls) ; 89 | } 90 | } 91 | return $(incls) ; 92 | } 93 | } 94 | 95 | # search-feature 96 | # 97 | # Generates feature for a package. 98 | rule search-feature ( pkg ) 99 | { 100 | if $(.with.$(pkg).libs) 101 | { 102 | return $(.with.$(pkg).libs) ; 103 | } 104 | } 105 | 106 | # hardcode-dll-paths 107 | # 108 | # generate the list of features for all packages with 109 | # defined. 110 | rule hardcode-dll-paths ( pkg ? ) 111 | { 112 | if $(pkg) 113 | { 114 | if $(.with.$(pkg).libs) 115 | { 116 | return $(.with.$(pkg).libs) ; 117 | } 118 | } 119 | else 120 | { 121 | local props ; 122 | for local p in $(.all.pkgs) 123 | { 124 | if $(.with.$(p).libs) 125 | { 126 | props += $(.with.$(p).libs) ; 127 | } 128 | } 129 | return $(props) ; 130 | } 131 | } 132 | 133 | # enable 134 | # 135 | # Enables requested feature as enabled. All possible features are by 136 | # default disabled: this may be not desirable. A better approach? 137 | # 138 | rule enable ( feature : define ? ) 139 | { 140 | if $(feature) in $(.all.enabled) 141 | { 142 | ECHO feature $(feature) already enabled ; 143 | } 144 | else 145 | { 146 | .all.enabled += $(feature) ; 147 | if $(define) 148 | { 149 | .enable.$(feature).define = $(define) ; 150 | } 151 | else 152 | { 153 | .enable.$(feature).define = _USE_$(feature:U)_ ; 154 | } 155 | } 156 | } 157 | 158 | # is-enabled 159 | # 160 | # Checks if requested feature has been enabled. 161 | rule is-enabled ( feature ) 162 | { 163 | return $(.enable.$(feature).define) ; 164 | } 165 | 166 | # define-enabled 167 | # 168 | # Generates attributes for all enabled features. 169 | rule define-enabled ( ) 170 | { 171 | local defines ; 172 | for local f in $(.all.enabled) 173 | { 174 | defines += $(.enable.$(f).define) ; 175 | } 176 | return $(defines) ; 177 | } 178 | 179 | # prefix 180 | # 181 | # Configure a default prefix to be used when installing. 182 | # 183 | # tag argument does not work well, it is suppose to be used in 184 | # get-prefix when computing dynamic prefix but package.install does 185 | # not handle as a main target. 186 | rule prefix ( prefix : tag ? ) 187 | { 188 | if $(.prefix) 189 | { 190 | ECHO discarging previous prefix $(.prefix) ; 191 | } 192 | .prefix = [ path.make $(prefix) ] ; 193 | .prefix.tag = $(tag) ; 194 | ECHO configuring prefix to $(.prefix) ; 195 | } 196 | 197 | # get-prefix 198 | # 199 | # Return the default prefix to be used when installing. Command line 200 | # option --prefix is handled. 201 | # 202 | # If configure.prefix was not used a default prefix is build (the same 203 | # as package.install 204 | rule get-prefix ( name : properties * ) 205 | { 206 | local prefix = $(.prefix) ; 207 | if ! $(prefix) 208 | { 209 | if [ modules.peek : NT ] { prefix = C:\\$(name) ; } 210 | else if [ modules.peek : UNIX ] { prefix = /usr/local ; } 211 | } 212 | # overwrite with --prefix if it was given 213 | prefix = [ option.get prefix : $(prefix) ] ; 214 | if $(.prefix.tag) 215 | { 216 | local tool = [ feature.get-values : $(properties) ] ; 217 | local dbg = [ feature.get-values : $(properties) ] ; 218 | if $(dbg) = "on" 219 | { 220 | prefix = $(prefix)-$(tool)-debug ; 221 | } 222 | else 223 | { 224 | prefix = $(prefix)-$(tool)-release ; 225 | } 226 | } 227 | return $(prefix) ; 228 | } 229 | 230 | # get-libdir 231 | # 232 | # return the default prefix for installing libs. Command line option 233 | # --libdir is handled. 234 | rule get-libdir ( name : properties * ) 235 | { 236 | local prefix = [ get-prefix $(name) : $(properties) ] ; 237 | 238 | return [ option.get libdir : $(prefix)/lib ] ; 239 | } 240 | 241 | # read 242 | # 243 | # read the given configuration file if it is given. After that the 244 | # command line options are processed. Some of the config options given in file 245 | # can be overwritten this way. 246 | # 247 | # --with-PKG= 248 | # --with-PKG-includes= 249 | # --with-PKG-libraries= 250 | # --enable-FEATURE 251 | # --prefix= 252 | rule read ( file ? ) 253 | { 254 | if $(file) 255 | { 256 | if [ path.exists $(file) ] 257 | { 258 | include $(file) ; 259 | } 260 | else 261 | { 262 | ECHO configuration file $(file) does not exist ; 263 | } 264 | } 265 | # now process command line arguments 266 | cmdline ; 267 | } 268 | 269 | local rule trim-left-nulls ( list * ) 270 | { 271 | local _list ; 272 | 273 | for local i in $(list) 274 | { 275 | if $(i) 276 | { 277 | _list += $(i) ; 278 | } 279 | } 280 | return $(_list) ; 281 | } 282 | 283 | local cmdl_all_pkgs ; 284 | local cmdl_all_enabled ; 285 | 286 | # cmdline 287 | # 288 | # command line option processing. See rule read. 289 | # 290 | # --with-PKG= 291 | # --with-PKG-includes= 292 | # --with-PKG-libraries= 293 | # --enable-FEATURE 294 | # --prefix= 295 | # 296 | rule cmdline ( options * ) 297 | { 298 | if $(options) 299 | { 300 | list_opts = $(options) ; 301 | } 302 | else 303 | { 304 | list_opts = [ modules.peek : ARGV ] ; 305 | } 306 | for opt in $(list_opts) 307 | { 308 | local matched = [ trim-left-nulls 309 | [ MATCH "--(with|enable)-(.+)|(prefix)=(.+)" 310 | : $(opt) ] ] ; 311 | 312 | # ECHO matched --> $(matched) ; 313 | 314 | switch $(matched[1]) 315 | { 316 | case prefix : 317 | { 318 | configure prefix : $(matched[2]) ; 319 | } 320 | case with : 321 | { 322 | local with = [ trim-left-nulls [ MATCH 323 | "([^-]+)-(includes|libraries)=(.+)|([^=]+)(=(.*))?" 324 | : $(matched[2]) ] ] ; 325 | #ECHO $(opt) --> $(with[1]) - $(with[2]) - $(with[3]) ; 326 | local p = $(with[1]) ; 327 | if ! ( $(p) in $(cmdl_all_pkgs) ) 328 | { 329 | cmdl_all_pkgs += $(p) ; 330 | } 331 | local sub_feat = $(with[2]) ; 332 | if [ MATCH (includes|libraries) : $(sub_feat) ] 333 | { 334 | #ECHO "pkg_$(p)_$(sub_feat) --> $(with[3]) " ; 335 | pkg_$(p)_$(sub_feat) = $(with[3]) ; 336 | } 337 | else 338 | { 339 | pkg_$(p)_prefix = $(with[3]) ; 340 | } 341 | } 342 | case enable : 343 | { 344 | cmdl_all_enabled += $(matched[2]) ; 345 | enabled_$(matched[2]) += 1 ; 346 | } 347 | } 348 | } 349 | 350 | for local p in $(cmdl_all_pkgs) 351 | { 352 | # ECHO configure $(p) - $(pkg_$(p)_includes) - $(pkg_$(p)_libraries) ; 353 | configure lib 354 | : $(p) : $(pkg_$(p)_prefix) 355 | : $(pkg_$(p)_includes) 356 | : $(pkg_$(p)_libraries) ; 357 | pkg_$(p)_prefix = ; 358 | pkg_$(p)_includes = ; 359 | pkg_$(p)_libraries = ; 360 | } 361 | 362 | for local f in $(cmdl_all_enabled) 363 | { 364 | configure enable : $(f) ; 365 | } 366 | cmdl_all_pkgs = ; 367 | cmdl_all_enabled = ; 368 | } 369 | 370 | IMPORT config : configure : : configure ; 371 | -------------------------------------------------------------------------------- /boost/build/prj-config.jam: -------------------------------------------------------------------------------- 1 | configure lib : gsl ; 2 | configure lib : cholmod : /usr/local/cholmod ; 3 | configure lib : unixodbc ; 4 | configure lib : mysql : : /usr/include/mysql ; 5 | configure lib : bz2 ; 6 | configure enable : dbdrivers ; 7 | configure prefix : /usr/local/tol ; 8 | -------------------------------------------------------------------------------- /cmake/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | project(workshop-cmake) 3 | 4 | configure_file( 5 | ${CMAKE_SOURCE_DIR}/cmake_build_type.h.in 6 | ${CMAKE_BINARY_DIR}/cmake_build_type.h 7 | @ONLY) 8 | 9 | include_directories(${CMAKE_BINARY_DIR}) 10 | 11 | add_executable(cmake_ndebug_test cmake_ndebug_test.cpp) 12 | -------------------------------------------------------------------------------- /cmake/README: -------------------------------------------------------------------------------- 1 | CMake configuration templates, use examples and tests 2 | 3 | Author: Mateusz Loskot -------------------------------------------------------------------------------- /cmake/cmake_build_type.h.in: -------------------------------------------------------------------------------- 1 | #define CMAKE_BUILD_TYPE "@CMAKE_BUILD_TYPE@" -------------------------------------------------------------------------------- /cmake/cmake_ndebug_test.cpp: -------------------------------------------------------------------------------- 1 | // Does CMake define NDEBUG in non-Debug builds? 2 | #include "cmake_build_type.h" 3 | #include 4 | 5 | int main() 6 | { 7 | // Possible values are empty, Debug, Release, RelWithDebInfo and MinSizeRel. 8 | // This variable is only supported for make based generators. 9 | std::printf("CMAKE_BUILD_TYPE=%s\n", CMAKE_BUILD_TYPE); 10 | #ifdef NDEBUG 11 | std::printf("NDEBUG=%d\n", NDEBUG); 12 | #else 13 | std::puts("NDEBUG not defined"); 14 | #endif 15 | } 16 | -------------------------------------------------------------------------------- /endian/endian.hpp: -------------------------------------------------------------------------------- 1 | // Load/Store values from/to stream of bytes across different endianness. 2 | // 3 | // Copyright Mateusz Loskot 2009 4 | // Use, modification and distribution is subject to the Boost Software License, 5 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 6 | // http://www.boost.org/LICENSE_1_0.txt) 7 | // 8 | // Original design of unrolled_byte_loops templates taken from Boost C++ Libraries, 9 | // source boost/spirit/home/support/detail/integer/endian.hpp 10 | // Copyright Darin Adler 2000 11 | // Copyright Beman Dawes 2006, 2009 12 | // 13 | #ifndef MLOSKOT_ENDIAN_HPP_INCLUDED 14 | #define MLOSKOT_ENDIAN_HPP_INCLUDED 15 | 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include 22 | #include 23 | #include 24 | 25 | #if CHAR_BIT != 8 26 | #error Platforms with CHAR_BIT != 8 are not supported 27 | #endif 28 | 29 | namespace mloskot { namespace endian { 30 | 31 | // Unrolled loops for loading and storing streams of bytes. 32 | 33 | template ::value> 34 | struct unrolled_byte_loops 35 | { 36 | typedef unrolled_byte_loops next; 37 | 38 | template 39 | static T load_forward(Iterator& bytes) 40 | { 41 | T const value = *bytes; 42 | ++bytes; 43 | return value | (next::load_forward(bytes) << 8); 44 | } 45 | 46 | template 47 | static T load_backward(Iterator& bytes) 48 | { 49 | T const value = *(bytes - 1); 50 | --bytes; 51 | return value | (next::load_backward(bytes) << 8); 52 | } 53 | 54 | template 55 | static void store_forward(Iterator& bytes, T value) 56 | { 57 | *bytes = static_cast(value); 58 | ++bytes; 59 | next::store_forward(bytes, value >> 8); 60 | } 61 | 62 | template 63 | static void store_backward(Iterator& bytes, T value) 64 | { 65 | *(bytes - 1) = static_cast(value); 66 | --bytes; 67 | next::store_backward(bytes, value >> 8); 68 | } 69 | }; 70 | 71 | template 72 | struct unrolled_byte_loops 73 | { 74 | template 75 | static T load_forward(Iterator& bytes) 76 | { 77 | return *bytes; 78 | } 79 | 80 | template 81 | static T load_backward(Iterator& bytes) 82 | { 83 | return *(bytes - 1); 84 | } 85 | 86 | template 87 | static void store_forward(Iterator& bytes, T value) 88 | { 89 | // typename Iterator::value_type 90 | *bytes = static_cast(value); 91 | } 92 | 93 | template 94 | static void store_backward(Iterator& bytes, T value) 95 | { 96 | *(bytes - 1) = static_cast(value); 97 | } 98 | }; 99 | 100 | template 101 | struct unrolled_byte_loops 102 | { 103 | template 104 | static T load_forward(Iterator& bytes) 105 | { 106 | return *reinterpret_cast(&*bytes); 107 | } 108 | 109 | template 110 | static T load_backward(Iterator& bytes) 111 | { 112 | return *reinterpret_cast(&*(bytes - 1)); 113 | } 114 | 115 | template 116 | static void store_forward(Iterator& bytes, T value) 117 | { 118 | BOOST_STATIC_ASSERT(boost::is_signed::value); 119 | 120 | *bytes = static_cast(value); 121 | } 122 | 123 | template 124 | static void store_backward(Iterator& bytes, T value) 125 | { 126 | BOOST_STATIC_ASSERT(boost::is_signed::value); 127 | 128 | *(bytes - 1) = static_cast(value); 129 | } 130 | }; 131 | 132 | // Types of endianness 133 | 134 | struct big_endian_tag {}; 135 | struct little_endian_tag {}; 136 | 137 | #ifdef BOOST_BIG_ENDIAN 138 | typedef big_endian_tag native_endian_tag; 139 | #else 140 | typedef little_endian_tag native_endian_tag; 141 | #endif 142 | 143 | namespace detail { 144 | 145 | template 146 | T load_dispatch(Iterator& bytes, E, E) 147 | { 148 | return unrolled_byte_loops::load_forward(bytes); 149 | } 150 | 151 | template 152 | T load_dispatch(Iterator& bytes, E1, E2) 153 | { 154 | std::advance(bytes, N); 155 | return unrolled_byte_loops::load_backward(bytes); 156 | } 157 | 158 | template 159 | void store_dispatch(Iterator& bytes, T value, E, E) 160 | { 161 | return unrolled_byte_loops::store_forward(bytes, value); 162 | } 163 | 164 | template 165 | void store_dispatch(Iterator& bytes, T value, E1, E2) 166 | { 167 | std::advance(bytes, N); 168 | return unrolled_byte_loops::store_backward(bytes, value); 169 | } 170 | 171 | template 172 | struct endian_value_base 173 | { 174 | typedef T value_type; 175 | typedef native_endian_tag endian_type; 176 | 177 | endian_value_base() : value(T()) {} 178 | explicit endian_value_base(T value) : value(value) {} 179 | 180 | operator T() const 181 | { 182 | return value; 183 | } 184 | 185 | protected: 186 | T value; 187 | }; 188 | 189 | } // namespace detail 190 | 191 | template 192 | struct endian_value : public detail::endian_value_base 193 | { 194 | typedef detail::endian_value_base base; 195 | 196 | endian_value() {} 197 | explicit endian_value(T value) : base(value) {} 198 | 199 | template 200 | void load(Iterator bytes) 201 | { 202 | base::value = detail::load_dispatch(bytes, base::endian_type(), E()); 203 | } 204 | 205 | template 206 | void store(Iterator bytes) 207 | { 208 | detail::store_dispatch(bytes, base::value, base::endian_type(), E()); 209 | } 210 | }; 211 | 212 | template <> 213 | struct endian_value : public detail::endian_value_base 214 | { 215 | typedef detail::endian_value_base base; 216 | 217 | endian_value() {} 218 | explicit endian_value(double value) : base(value) {} 219 | 220 | template 221 | void load(Iterator bytes) 222 | { 223 | endian_value raw; 224 | raw.load(bytes); 225 | 226 | double& target_value = base::value; 227 | std::memcpy(&target_value, &raw, sizeof(double)); 228 | } 229 | 230 | template 231 | void store(Iterator bytes) 232 | { 233 | boost::uint64_t raw; 234 | double const& source_value = base::value; 235 | std::memcpy(&raw, &source_value, sizeof(boost::uint64_t)); 236 | 237 | detail::store_dispatch(bytes, raw, base::endian_type(), E()); 238 | } 239 | }; 240 | 241 | }} // namespace mloskot::endian 242 | 243 | #endif // MLOSKOT_ENDIAN_HPP_INCLUDED 244 | -------------------------------------------------------------------------------- /endian/endian_example.cpp: -------------------------------------------------------------------------------- 1 | // Yet Another Tiny Binary Reader/Writer 2 | // 3 | // Simple test and usage example 4 | // 5 | // Copyright Mateusz Loskot 2009 6 | // Use, modification and distribution is subject to the Boost Software License, 7 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 8 | // http://www.boost.org/LICENSE_1_0.txt) 9 | // 10 | #include "endian.hpp" 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include 23 | #include 24 | 25 | namespace { 26 | 27 | template 28 | void hex_to_bytes(std::string const& hex, T& bytes) 29 | { 30 | if (0 != hex.size() % 2) 31 | { 32 | throw std::runtime_error("invalid lenght of hex string"); 33 | } 34 | 35 | std::string::size_type const nchars = 2; 36 | std::string::size_type const nbytes = hex.size() / nchars; 37 | 38 | T(nbytes).swap(bytes); 39 | 40 | for (std::string::size_type i = 0; i < nbytes; ++i) 41 | { 42 | std::istringstream iss(hex.substr(i * nchars, nchars)); 43 | unsigned int n(0); 44 | if (!(iss >> std::hex >> n)) 45 | { 46 | throw std::runtime_error("hex to binary failed"); 47 | } 48 | bytes[i] = static_cast(n); 49 | } 50 | } 51 | 52 | template 53 | void bytes_to_hex(T const& bytes, std::string& hexstr) 54 | { 55 | typedef typename T::size_type size_type; 56 | size_type const nbytes = bytes.size(); 57 | 58 | const char hexchrs[] = "0123456789ABCDEF"; 59 | char hexbyte[3] = { 0 }; 60 | 61 | std::ostringstream oss; 62 | for (size_type i = 0; i < nbytes; ++i) 63 | { 64 | boost::uint8_t byte = static_cast(bytes[i]); 65 | hexbyte[0] = hexchrs[(byte >> 4) & 0xf]; 66 | hexbyte[1] = hexchrs[byte & 0xf]; 67 | hexbyte[2] = '\0'; 68 | oss << std::setw(2) << hexbyte; 69 | } 70 | hexstr = oss.str(); 71 | 72 | if (hexstr.size() != nbytes * 2) 73 | { 74 | throw std::runtime_error("binary to hex failed"); 75 | } 76 | } 77 | 78 | typedef std::vector byte_array; 79 | 80 | } // anonymous namespace 81 | 82 | 83 | int main() 84 | { 85 | using namespace mloskot; 86 | 87 | std::size_t const bytes_size = 21; 88 | 89 | try 90 | { 91 | std::cout << "Test load little-endian" << std::endl; 92 | { 93 | std::string const hex_little("010C0000005839B4C876BEF33F83C0CAA145B61640"); 94 | std::cout << hex_little << std::endl; 95 | 96 | byte_array bytes; 97 | hex_to_bytes(hex_little, bytes); 98 | 99 | endian::endian_value ev8; 100 | ev8.load(bytes.begin()); 101 | 102 | std::cout << (int)ev8 << std::endl; 103 | 104 | endian::endian_value ev32; 105 | ev32.load(bytes.begin() + 1); 106 | 107 | std::cout << ev32 << std::endl; 108 | 109 | endian::endian_value ev64d1; 110 | ev64d1.load(bytes.begin() + 1 + 4); 111 | 112 | std::cout << ev64d1 << std::endl; 113 | 114 | endian::endian_value ev64d2; 115 | ev64d2.load(bytes.begin() + 1 + 4 + 8); 116 | 117 | std::cout << ev64d2 << std::endl; 118 | } 119 | 120 | std::cout << "Test load little-endian (raw pointer)" << std::endl; 121 | { 122 | std::string const hex_little("010C0000005839B4C876BEF33F83C0CAA145B61640"); 123 | std::cout << hex_little << std::endl; 124 | 125 | byte_array bytes; 126 | hex_to_bytes(hex_little, bytes); 127 | 128 | assert(bytes_size == bytes.size()); 129 | boost::uint8_t tbytes[bytes_size] = { 0 }; 130 | std::copy(bytes.begin(), bytes.end(), tbytes); 131 | 132 | endian::endian_value ev8; 133 | ev8.load(tbytes); 134 | 135 | std::cout << (int)ev8 << std::endl; 136 | 137 | endian::endian_value ev32; 138 | ev32.load(tbytes + 1); 139 | 140 | std::cout << ev32 << std::endl; 141 | 142 | endian::endian_value ev64d1; 143 | ev64d1.load(tbytes + 1 + 4); 144 | 145 | std::cout << ev64d1 << std::endl; 146 | 147 | endian::endian_value ev64d2; 148 | ev64d2.load(tbytes + 1 + 4 + 8); 149 | 150 | std::cout << ev64d2 << std::endl; 151 | } 152 | 153 | std::cout << "Test load big-endian" << std::endl; 154 | { 155 | std::string const hex_big("010000000C3FF3BE76C8B439584016B645A1CAC083"); 156 | std::cout << hex_big << std::endl; 157 | 158 | byte_array bytes; 159 | hex_to_bytes(hex_big, bytes); 160 | 161 | endian::endian_value ev8; 162 | ev8.load(bytes.begin()); 163 | 164 | std::cout << (int)ev8 << std::endl; 165 | 166 | endian::endian_value ev32; 167 | ev32.load(bytes.begin() + 1); 168 | 169 | std::cout << ev32 << std::endl; 170 | 171 | endian::endian_value ev64d1; 172 | ev64d1.load(bytes.begin() + 1 + 4); 173 | 174 | std::cout << ev64d1 << std::endl; 175 | 176 | endian::endian_value ev64d2; 177 | ev64d2.load(bytes.begin() + 1 + 4 + 8); 178 | 179 | std::cout << ev64d2 << std::endl; 180 | } 181 | 182 | std::cout << "Test load big-endian (raw pointer)" << std::endl; 183 | { 184 | std::string const hex_big("010000000C3FF3BE76C8B439584016B645A1CAC083"); 185 | std::cout << hex_big << std::endl; 186 | 187 | byte_array bytes; 188 | hex_to_bytes(hex_big, bytes); 189 | 190 | assert(bytes_size == bytes.size()); 191 | boost::uint8_t tbytes[bytes_size] = { 0 }; 192 | std::copy(bytes.begin(), bytes.end(), tbytes); 193 | 194 | endian::endian_value ev8; 195 | ev8.load(tbytes); 196 | 197 | std::cout << (int)ev8 << std::endl; 198 | 199 | endian::endian_value ev32; 200 | ev32.load(tbytes + 1); 201 | 202 | std::cout << ev32 << std::endl; 203 | 204 | endian::endian_value ev64d1; 205 | ev64d1.load(tbytes + 1 + 4); 206 | 207 | std::cout << ev64d1 << std::endl; 208 | 209 | endian::endian_value ev64d2; 210 | ev64d2.load(tbytes + 1 + 4 + 8); 211 | 212 | std::cout << ev64d2 << std::endl; 213 | } 214 | 215 | std::cout << "Test store little-/big-endian" << std::endl; 216 | { 217 | std::size_t const len = 1 + 4 + 8 + 8; 218 | byte_array bytes_big(len, 'X'); 219 | byte_array bytes_little(len, 'X'); 220 | 221 | endian::endian_value ev8(1); 222 | ev8.store(bytes_little.begin()); 223 | ev8.store(bytes_big.begin()); 224 | 225 | endian::endian_value ev32(12); 226 | ev32.store(bytes_little.begin() + 1); 227 | ev32.store(bytes_big.begin() + 1); 228 | 229 | endian::endian_value ev64d1(1.234); 230 | ev64d1.store(bytes_little.begin() + 1 + 4); 231 | ev64d1.store(bytes_big.begin() + 1 + 4); 232 | 233 | endian::endian_value ev64d2(5.678); 234 | ev64d2.store(bytes_little.begin() + 1 + 4 + 8); 235 | ev64d2.store(bytes_big.begin() + 1 + 4 + 8); 236 | 237 | std::string hex; 238 | bytes_to_hex(bytes_little, hex); 239 | std::cout << hex << std::endl; 240 | bytes_to_hex(bytes_big, hex); 241 | std::cout << hex << std::endl; 242 | } 243 | } 244 | catch (std::exception const& e) 245 | { 246 | std::cerr << e.what() << std::endl; 247 | } 248 | 249 | return 0; 250 | } 251 | -------------------------------------------------------------------------------- /endian/msvc80/.gitignore: -------------------------------------------------------------------------------- 1 | *.ncb 2 | *.suo 3 | *.vcproj.*.user 4 | Debug 5 | Release 6 | -------------------------------------------------------------------------------- /endian/msvc80/endian.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 9.00 3 | # Visual Studio 2005 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "endian", "endian.vcproj", "{4D07473F-1E7F-463B-B68A-7044B67883D9}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Win32 = Debug|Win32 9 | Release|Win32 = Release|Win32 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {4D07473F-1E7F-463B-B68A-7044B67883D9}.Debug|Win32.ActiveCfg = Debug|Win32 13 | {4D07473F-1E7F-463B-B68A-7044B67883D9}.Debug|Win32.Build.0 = Debug|Win32 14 | {4D07473F-1E7F-463B-B68A-7044B67883D9}.Release|Win32.ActiveCfg = Release|Win32 15 | {4D07473F-1E7F-463B-B68A-7044B67883D9}.Release|Win32.Build.0 = Release|Win32 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /endian/msvc80/endian.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 26 | 29 | 32 | 35 | 38 | 41 | 54 | 57 | 60 | 63 | 70 | 73 | 76 | 79 | 82 | 85 | 88 | 91 | 94 | 95 | 104 | 107 | 110 | 113 | 116 | 119 | 129 | 132 | 135 | 138 | 147 | 150 | 153 | 156 | 159 | 162 | 165 | 168 | 171 | 172 | 173 | 174 | 175 | 176 | 179 | 180 | 183 | 184 | 185 | 186 | 187 | 188 | -------------------------------------------------------------------------------- /endian/msvc80/endian.vsprops: -------------------------------------------------------------------------------- 1 | 2 | 7 | 12 | 16 | 17 | -------------------------------------------------------------------------------- /gdal/ogr_collinear_simplify.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | inline double calculate_slope(OGRRawPoint const& p1, OGRRawPoint const& p2) 9 | { 10 | // dy y2 - y1 11 | // m = ---- = --------- 12 | // dx x2 - x1 13 | return ((p2.y - p1.y) / (p2.x - p1.x)); 14 | } 15 | 16 | void remove_collinear_points(std::vector const& line, 17 | std::vector& simple, 18 | double const tolerance) 19 | { 20 | typedef std::vector points_t; 21 | simple.reserve(line.size()); 22 | 23 | // First and last points are always inserted. 24 | double m = 0; 25 | simple.push_back(line[0]); 26 | m = calculate_slope(line[0], line[1]); 27 | 28 | // Simplify excluding last point, always inserted 29 | points_t::size_type const size = line.size() - 1; 30 | for (points_t::size_type i = 1; i < size; ++i) 31 | { 32 | OGRRawPoint const& a = line[i]; 33 | OGRRawPoint const& b = line[i + 1]; 34 | double mt = calculate_slope(a, b); 35 | 36 | double const diff = m - mt; 37 | if (!((diff <= tolerance) && (diff >= -tolerance))) 38 | { 39 | simple.push_back(a); 40 | m = mt; 41 | } 42 | } 43 | 44 | // Insert last point 45 | simple.push_back(line[line.size() - 1]); 46 | } 47 | 48 | int main() 49 | { 50 | OGRRegisterAll(); 51 | 52 | const char* pszFile = "D:\\data\\mass\\ldtrai\\ldtraia1.shp"; 53 | const char* pszlayer = "ldtraia1"; 54 | 55 | // Open Shapefile layer 56 | OGRDataSource* poDS = NULL; 57 | poDS = OGRSFDriverRegistrar::Open(pszFile, FALSE); 58 | assert(NULL != poDS && "Can not open input shapefile!"); 59 | OGRLayer* poLayer = NULL; 60 | poLayer = poDS->GetLayerByName(pszlayer); 61 | 62 | // Get sample feature of given FID 63 | const int fid = 0; 64 | OGRFeature* poFeature = NULL; 65 | poFeature = poLayer->GetFeature(fid); 66 | assert(NULL != poFeature); 67 | 68 | // Get linestring geometry of feature 69 | OGRGeometry* poGeom = NULL; 70 | poGeom = poFeature->GetGeometryRef(); 71 | OGRLineString* poLine = NULL; 72 | poLine = static_cast(poGeom); 73 | 74 | // Copy linestring points to simple array 75 | std::vector linePoints(poLine->getNumPoints()); 76 | poLine->getPoints(&linePoints[0], NULL); 77 | 78 | // Simplify linestring by removing collinear or close to collinear points. 79 | // Original points stay untouched, but simplified version is built as new array 80 | std::vector simple; 81 | double tolerance = 0.1; // std::numeric_limits::epsilon(); // Smallest tolerance as possible 82 | remove_collinear_points(linePoints, simple, tolerance); 83 | 84 | // Summary 85 | printf("Before: %u (%u)\n", linePoints.size(), linePoints.capacity()); 86 | printf("After : %u (%u)\n", simple.size(), simple.capacity()); 87 | 88 | OGRFeature::DestroyFeature(poFeature); 89 | OGRDataSource::DestroyDataSource( poDS ); 90 | } 91 | 92 | -------------------------------------------------------------------------------- /postgresql/.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | \#*\# 3 | .\#* 4 | *.bak 5 | *.dll 6 | *.dll.manifest 7 | *.embed.manifest 8 | *.exe 9 | *.exe.manifest 10 | *.exp 11 | *.idb 12 | *.ilk 13 | *.intermediate.manifest 14 | *.ipch 15 | *.kdev4 16 | *.la 17 | *.lib 18 | *.lo 19 | *.ncb 20 | *.o 21 | *.obj 22 | *.opensdf 23 | *.orig 24 | *.pdb 25 | *.pyc 26 | *.res 27 | *.sdf 28 | *.sln 29 | *.sln.docstates 30 | *.suo 31 | *.swp 32 | *.tlog 33 | *.user 34 | *.vcproj 35 | *.vcxproj 36 | *.vcxproj.filters 37 | build 38 | cmake_install.cmake 39 | CMakeCache.txt 40 | CMakeFiles 41 | CTestTestfile.cmake 42 | CPackConfig.cmake 43 | CPackSourceConfig.cmake 44 | Debug 45 | Makefile 46 | Makefile.in 47 | Release 48 | -------------------------------------------------------------------------------- /postgresql/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | project(workshop-postgresql) 3 | 4 | find_package(PostgreSQL) 5 | if (PostgreSQL_FOUND) 6 | include_directories(${PostgreSQL_INCLUDE_DIRS}) 7 | link_directories(${PostgreSQL_LIBRARY_DIRS}) 8 | else() 9 | message(FATAL_ERROR "PostgreSQL not found but required") 10 | endif() 11 | 12 | set(COMMON_LIBRARIES) 13 | 14 | if(MSVC) 15 | add_definitions(-D_CRT_SECURE_NO_WARNINGS) 16 | add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE) 17 | add_definitions(-D_CRT_SECURE_NO_DEPRECATE) 18 | add_definitions(-D_SCL_SECURE_NO_DEPRECATE) 19 | 20 | find_library(WSOCK32_LIBRARY wsock32) 21 | find_library(WS2_32_LIBRARY ws2_32) 22 | if (WSOCK32_LIBRARY AND WS2_32_LIBRARY) 23 | set(COMMON_LIBRARIES ${COMMON_LIBRARIES} ${WSOCK32_LIBRARY} ${WS2_32_LIBRARY}) 24 | endif() 25 | endif() 26 | 27 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}) 28 | 29 | set(COMMON_HEADERS 30 | ${CMAKE_CURRENT_SOURCE_DIR}/tinypq.hpp) 31 | 32 | add_subdirectory(numeric) 33 | -------------------------------------------------------------------------------- /postgresql/README: -------------------------------------------------------------------------------- 1 | Variety of tests and examples of use of the PostgreSQL client library (libpq) API. 2 | 3 | Contents: 4 | * numeric/ - testing NUMERIC data type exchange in binary mode 5 | 6 | Requires https://github.com/mloskot/tinypq 7 | 8 | Author: Mateusz Loskot 9 | -------------------------------------------------------------------------------- /postgresql/configure.bat: -------------------------------------------------------------------------------- 1 | @ECHO OFF 2 | REM *** CONFIGURATION *** 3 | set WORKSHOP=D:\dev\_github\workshop 4 | set MODULE=postgresql 5 | set PGSQL=C:\Program Files (x86)\PostgreSQL\9.0 6 | REM ******************************** 7 | set PROJECT=workshop-%MODULE% 8 | set SRC=%WORKSHOP%\%MODULE% 9 | set BUILD=%WORKSHOP%\%MODULE%\build 10 | set BUILD_TYPE=Debug 11 | set G="Visual Studio 11" 12 | set PostgreSQL_ROOT=%PGSQL% 13 | set PATH=%PGSQL%\bin;%PATH% 14 | IF NOT EXIST %BUILD% mkdir %BUILD% 15 | cd %BUILD% 16 | cmake -G %G% %SRC% 17 | CALL fix_vs11_sln.bat %PROJECT%.sln 18 | cd %SRC% -------------------------------------------------------------------------------- /postgresql/numeric/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_executable(postgresql_binary_numeric 2 | postgresql_binary_numeric.cpp 3 | ${COMMON_HEADERS}) 4 | target_link_libraries(postgresql_binary_numeric 5 | ${PostgreSQL_LIBRARIES} 6 | ${COMMON_LIBRARIES}) -------------------------------------------------------------------------------- /postgresql/numeric/postgresql_binary_numeric.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2011 Mateusz Loskot 3 | // Distributed under the Boost Software License, Version 1.0. 4 | // (See accompanying file LICENSE_1_0.txt or copy at 5 | // http://www.boost.org/LICENSE_1_0.txt) 6 | // 7 | #include 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | // Windows 16 | #include 17 | #include // ntohs 18 | 19 | struct numeric 20 | { 21 | enum sign_mode 22 | { 23 | positive = 0x0000, 24 | negative = 0x4000, 25 | NaN = 0xC000, 26 | null = 0xF000 27 | }; 28 | 29 | enum precision_range 30 | { 31 | max_precision = 1000, 32 | min_scale = 0, 33 | max_scale = max_precision, 34 | }; 35 | 36 | std::vector digits; 37 | sign_mode mode; 38 | int sign; 39 | int exp; 40 | }; 41 | 42 | double floor_div(double dividend, double divisor) 43 | { 44 | if (divisor == 0.0) 45 | throw std::invalid_argument("division by zero"); 46 | 47 | double mod = fmod(dividend, divisor); 48 | 49 | // In float-point arithmetic, dividend - mod is an approximation. 50 | // Thus after division the div may not be an exact integral value, although 51 | // it will always be very close to one. 52 | double div = (dividend - mod) / divisor; 53 | if (mod != 0) 54 | { 55 | // Ensure the remainder has the same sign as the denominator. 56 | if ((divisor < 0) != (mod < 0)) 57 | { 58 | mod += divisor; 59 | div -= 1.0; 60 | } 61 | } 62 | else 63 | { 64 | // The remainder is zero, ensure it has the same sign as the denominator. 65 | mod = _copysign(0.0, divisor); 66 | } 67 | 68 | // Floor quotient to nearest integral value. 69 | double quotient(0); 70 | if (div) 71 | { 72 | quotient = floor(div); 73 | if (div - quotient > 0.5) 74 | quotient += 1.0; 75 | } 76 | else 77 | { 78 | // Division result is zero - get the same sign as the true quotient */ 79 | quotient = _copysign(0.0, dividend / divisor); 80 | } 81 | 82 | return quotient; 83 | } 84 | 85 | numeric read_numeric(char* val) 86 | { 87 | enum { decimal_digits_per_nbase = 4 }; // decimal digits per word 88 | 89 | short* s = reinterpret_cast(val); 90 | u_short const words_count = ntohs(*s); s++; 91 | signed short first_word_weight = ntohs(*s); s++; 92 | u_short const sign = ntohs(*s); s++; 93 | u_short display_scale = ntohs(*s); s++; 94 | assert(display_scale >= 0); 95 | std::clog << "X: nwords=" << words_count << " weight=" << first_word_weight << " sign=" << sign << " dscale=" << display_scale << std::endl; 96 | 97 | numeric n; 98 | 99 | if (sign == numeric::positive) 100 | { 101 | n.mode = numeric::positive; 102 | n.sign = 0; 103 | } 104 | else if (sign == numeric::negative) 105 | { 106 | n.mode = numeric::negative; 107 | n.sign = 1; 108 | } 109 | else if (sign == numeric::NaN) 110 | { 111 | n.mode = numeric::NaN; 112 | return n; 113 | } 114 | else 115 | { 116 | throw std::invalid_argument("invalid sign"); 117 | } 118 | 119 | std::vector digits; 120 | int exp = 0; 121 | if (words_count == 0) 122 | { 123 | exp = -display_scale; 124 | digits.push_back(0); 125 | } 126 | else 127 | { 128 | assert(words_count > 0); 129 | std::vector words(words_count); 130 | for (auto i = 0U; i < words_count; ++i) 131 | { 132 | words[i] = ntohs(*s); 133 | s++; 134 | } 135 | 136 | std::array const shifts = { 1000, 100, 10, 1 }; 137 | for(auto it(words.cbegin()); it != words.cend(); ++it) 138 | { 139 | for (std::size_t si = 0; si < shifts.size(); ++si) 140 | { 141 | double const fd = floor_div(*it, shifts[si]); 142 | short const digit = static_cast(fd) % 10; 143 | assert(digit < 10); 144 | if (!digits.empty() || digit != 0) 145 | digits.push_back(digit); 146 | } 147 | } 148 | 149 | // There are weight + 1 digits before the decimal point. 150 | exp = (first_word_weight + 1 - words_count) * decimal_digits_per_nbase; 151 | if (0 < exp && exp < 20) 152 | { 153 | std::fill_n(std::back_inserter(digits), exp, 0); 154 | exp = 0; 155 | } 156 | 157 | if (display_scale > 0) 158 | { 159 | int const zerofill = display_scale + exp; 160 | if (zerofill < 0) 161 | { 162 | auto last = digits.cbegin() + (digits.size() + zerofill); 163 | digits.erase(last, digits.cend()); 164 | exp -= zerofill; 165 | } 166 | else if (zerofill > 0) 167 | { 168 | std::fill_n(std::back_inserter(digits), zerofill, 0); 169 | exp -= zerofill; 170 | } 171 | } 172 | } 173 | 174 | assert(!digits.empty()); 175 | 176 | n.digits = digits; 177 | n.exp = exp; 178 | return n; 179 | } 180 | 181 | template 182 | void echo(std::string col, numeric const& n, T value) 183 | { 184 | std::cout << col << ":\tsign=" << n.sign << "\texp=" << n.exp << "\tdigits="; 185 | std::for_each(n.digits.cbegin(), n.digits.cend(), [](short d) { std::cout << d; }); 186 | std::cout << "\tvalue=" << value << std::endl; 187 | } 188 | 189 | int main() 190 | { 191 | try 192 | { 193 | std::string conninfo = "dbname=mloskot user=mloskot"; 194 | auto qconn = tinypq::connect(conninfo); 195 | 196 | tinypq::command(qconn, "DROP TABLE IF EXISTS testn CASCADE"); 197 | tinypq::command(qconn, "CREATE TABLE testn (n1 NUMERIC, n2 NUMERIC(10, 10), n3 NUMERIC(10, 5), n4 NUMERIC(10, 0))"); 198 | tinypq::command(qconn, "INSERT INTO testn (n1, n2, n3, n4) VALUES (random()::NUMERIC*1000, random(), random()::NUMERIC*1000, 1230456078)"); 199 | 200 | auto qres = tinypq::query(qconn, "SELECT n1, n2, n3, n4 FROM testn LIMIT 1"); 201 | std::array cols = { "n1", "n2", "n3", "n4" }; 202 | std::for_each(cols.cbegin(), cols.cend(), [&qres](char const* col) 203 | { 204 | char* val = tinypq::fetch(qres, 0, col); 205 | auto n = read_numeric(val); 206 | 207 | // make a decimal value 208 | std::string scoeff; 209 | scoeff.reserve(n.digits.size()); 210 | std::for_each(n.digits.cbegin(), n.digits.cend(), [&scoeff](short c) { scoeff += std::to_string(c); }); 211 | long long const coeff = std::stoll(scoeff); 212 | double const value = std::pow(-1.0, n.sign) * coeff * std::pow(10, n.exp); 213 | 214 | echo(col, n, value); 215 | }); 216 | 217 | return EXIT_SUCCESS; 218 | 219 | } 220 | catch (std::exception const& e) 221 | { 222 | std::clog << e.what() << std::endl; 223 | return EXIT_FAILURE; 224 | } 225 | } 226 | -------------------------------------------------------------------------------- /python/.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | \#*\# 3 | .\#* 4 | *.bak 5 | *.dll 6 | *.dll.manifest 7 | *.embed.manifest 8 | *.exe 9 | *.exe.manifest 10 | *.exp 11 | *.idb 12 | *.ilk 13 | *.intermediate.manifest 14 | *.ipch 15 | *.kdev4 16 | *.la 17 | *.lib 18 | *.lo 19 | *.ncb 20 | *.o 21 | *.obj 22 | *.opensdf 23 | *.orig 24 | *.pdb 25 | *.pyc 26 | *.res 27 | *.sdf 28 | *.sln 29 | *.sln.docstates 30 | *.suo 31 | *.swp 32 | *.tlog 33 | *.user 34 | *.vcproj 35 | *.vcxproj 36 | *.vcxproj.filters 37 | build 38 | cmake_install.cmake 39 | CMakeCache.txt 40 | CMakeFiles 41 | CTestTestfile.cmake 42 | CPackConfig.cmake 43 | CPackSourceConfig.cmake 44 | Debug 45 | Makefile 46 | Makefile.in 47 | Release 48 | -------------------------------------------------------------------------------- /python/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 2.8) 2 | project(workshop-python) 3 | 4 | find_package(PythonLibs) 5 | if (PYTHONLIBS_FOUND) 6 | include_directories(${PYTHON_INCLUDE_DIRS}) 7 | 8 | if (PYTHON_DEBUG_LIBRARIES) 9 | message(STATUS "Found Python Debug libraries: ${PYTHON_DEBUG_LIBRARIES}") 10 | endif() 11 | else() 12 | message(FATAL_ERROR "Python not found but required") 13 | endif() 14 | 15 | if(MSVC) 16 | add_definitions(-D_CRT_SECURE_NO_WARNINGS) 17 | add_definitions(-D_CRT_NONSTDC_NO_DEPRECATE) 18 | add_definitions(-D_CRT_SECURE_NO_DEPRECATE) 19 | add_definitions(-D_SCL_SECURE_NO_DEPRECATE) 20 | endif() 21 | 22 | set(MY_PYTHON_LIBRARIES ${PYTHON_LIBRARIES}) 23 | if (PYTHON_DEBUG_LIBRARIES) 24 | set(MY_PYTHON_LIBRARIES ${PYTHON_DEBUG_LIBRARIES}) 25 | endif() 26 | 27 | add_executable(simple simple.cpp) 28 | target_link_libraries(simple ${MY_PYTHON_LIBRARIES}) 29 | 30 | add_executable(tkinter tkinter.cpp) 31 | target_link_libraries(tkinter ${MY_PYTHON_LIBRARIES}) 32 | 33 | add_executable(faq_incomplete_input faq_incomplete_input.cpp) 34 | target_link_libraries(faq_incomplete_input ${MY_PYTHON_LIBRARIES}) 35 | 36 | add_subdirectory(emb) -------------------------------------------------------------------------------- /python/README: -------------------------------------------------------------------------------- 1 | Examples of extending and embedding Python 3.x in C++ using Python C API 2 | 3 | Author: Mateusz Loskot -------------------------------------------------------------------------------- /python/build.bat: -------------------------------------------------------------------------------- 1 | @echo off 2 | rem Runs CMake to configure for Visual Studio 2015. 3 | rem Runs MSBuild to build the generated solution. 4 | ## 5 | 6 | if not defined VS140COMNTOOLS goto :NoVS 7 | if [%1]==[] goto :Usage 8 | if [%1]==[32] goto :32 9 | if [%1]==[64] goto :64 10 | goto :Usage 11 | 12 | :32 13 | set BUILDP=32 14 | set MSBUILDP=Win32 15 | set GENERATOR="Visual Studio 14 2015" 16 | goto :Build 17 | 18 | :64 19 | set BUILDP=64 20 | set MSBUILDP=x64 21 | set GENERATOR="Visual Studio 14 2015 Win64" 22 | goto :Build 23 | 24 | :Build 25 | set BUILDDIR=_build%BUILDP% 26 | mkdir %BUILDDIR% 27 | pushd %BUILDDIR% 28 | "C:\Program Files\CMake\bin\cmake.exe" ^ 29 | -G %GENERATOR% ^ 30 | .. 31 | move workshop-python.sln workshop-python%BUILDP%.sln 32 | msbuild.exe workshop-python%BUILDP%.sln /p:Configuration=Release /p:Platform=%MSBUILDP% 33 | popd 34 | goto :EOF 35 | 36 | :NoVS 37 | @echo build.bat 38 | @echo Visual Studio 2015 not found 39 | @echo "%%VS140COMNTOOLS%%" environment variable not defined 40 | exit /B 1 41 | 42 | :Usage 43 | @echo build.bat 44 | @echo Usage: build.bat [32 or 64] 45 | exit /B 1 46 | -------------------------------------------------------------------------------- /python/emb/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(MY_PYTHON_LIBRARIES ${PYTHON_LIBRARIES}) 2 | if (PYTHON_DEBUG_LIBRARIES) 3 | set(MY_PYTHON_LIBRARIES ${PYTHON_DEBUG_LIBRARIES}) 4 | endif() 5 | 6 | add_executable(emb emb.cpp) 7 | target_link_libraries(emb ${MY_PYTHON_LIBRARIES}) -------------------------------------------------------------------------------- /python/emb/emb.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2011 Mateusz Loskot 3 | // Distributed under the Boost Software License, Version 1.0. 4 | // (See accompanying file LICENSE_1_0.txt or copy at 5 | // http://www.boost.org/LICENSE_1_0.txt) 6 | // 7 | // Blog article: http://mateusz.loskot.net/?p=2819 8 | 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | namespace emb 15 | { 16 | 17 | typedef std::function stdout_write_type; 18 | 19 | struct Stdout 20 | { 21 | PyObject_HEAD 22 | stdout_write_type write; 23 | }; 24 | 25 | PyObject* Stdout_write(PyObject* self, PyObject* args) 26 | { 27 | std::size_t written(0); 28 | Stdout* selfimpl = reinterpret_cast(self); 29 | if (selfimpl->write) 30 | { 31 | char* data; 32 | if (!PyArg_ParseTuple(args, "s", &data)) 33 | return 0; 34 | 35 | std::string str(data); 36 | selfimpl->write(str); 37 | written = str.size(); 38 | } 39 | return PyLong_FromSize_t(written); 40 | } 41 | 42 | PyObject* Stdout_flush(PyObject* self, PyObject* args) 43 | { 44 | // no-op 45 | return Py_BuildValue(""); 46 | } 47 | 48 | PyMethodDef Stdout_methods[] = 49 | { 50 | {"write", Stdout_write, METH_VARARGS, "sys.stdout.write"}, 51 | {"flush", Stdout_flush, METH_VARARGS, "sys.stdout.write"}, 52 | {0, 0, 0, 0} // sentinel 53 | }; 54 | 55 | PyTypeObject StdoutType = 56 | { 57 | PyVarObject_HEAD_INIT(0, 0) 58 | "emb.StdoutType", /* tp_name */ 59 | sizeof(Stdout), /* tp_basicsize */ 60 | 0, /* tp_itemsize */ 61 | 0, /* tp_dealloc */ 62 | 0, /* tp_print */ 63 | 0, /* tp_getattr */ 64 | 0, /* tp_setattr */ 65 | 0, /* tp_reserved */ 66 | 0, /* tp_repr */ 67 | 0, /* tp_as_number */ 68 | 0, /* tp_as_sequence */ 69 | 0, /* tp_as_mapping */ 70 | 0, /* tp_hash */ 71 | 0, /* tp_call */ 72 | 0, /* tp_str */ 73 | 0, /* tp_getattro */ 74 | 0, /* tp_setattro */ 75 | 0, /* tp_as_buffer */ 76 | Py_TPFLAGS_DEFAULT, /* tp_flags */ 77 | "emb.Stdout objects", /* tp_doc */ 78 | 0, /* tp_traverse */ 79 | 0, /* tp_clear */ 80 | 0, /* tp_richcompare */ 81 | 0, /* tp_weaklistoffset */ 82 | 0, /* tp_iter */ 83 | 0, /* tp_iternext */ 84 | Stdout_methods, /* tp_methods */ 85 | 0, /* tp_members */ 86 | 0, /* tp_getset */ 87 | 0, /* tp_base */ 88 | 0, /* tp_dict */ 89 | 0, /* tp_descr_get */ 90 | 0, /* tp_descr_set */ 91 | 0, /* tp_dictoffset */ 92 | 0, /* tp_init */ 93 | 0, /* tp_alloc */ 94 | 0, /* tp_new */ 95 | }; 96 | 97 | PyModuleDef embmodule = 98 | { 99 | PyModuleDef_HEAD_INIT, 100 | "emb", 0, -1, 0, 101 | }; 102 | 103 | // Internal state 104 | PyObject* g_stdout; 105 | PyObject* g_stdout_saved; 106 | 107 | PyMODINIT_FUNC PyInit_emb(void) 108 | { 109 | g_stdout = 0; 110 | g_stdout_saved = 0; 111 | 112 | StdoutType.tp_new = PyType_GenericNew; 113 | if (PyType_Ready(&StdoutType) < 0) 114 | return 0; 115 | 116 | PyObject* m = PyModule_Create(&embmodule); 117 | if (m) 118 | { 119 | Py_INCREF(&StdoutType); 120 | PyModule_AddObject(m, "Stdout", reinterpret_cast(&StdoutType)); 121 | } 122 | return m; 123 | } 124 | 125 | void set_stdout(stdout_write_type write) 126 | { 127 | if (!g_stdout) 128 | { 129 | g_stdout_saved = PySys_GetObject("stdout"); // borrowed 130 | g_stdout = StdoutType.tp_new(&StdoutType, 0, 0); 131 | } 132 | 133 | Stdout* impl = reinterpret_cast(g_stdout); 134 | impl->write = write; 135 | PySys_SetObject("stdout", g_stdout); 136 | } 137 | 138 | void reset_stdout() 139 | { 140 | if (g_stdout_saved) 141 | PySys_SetObject("stdout", g_stdout_saved); 142 | 143 | Py_XDECREF(g_stdout); 144 | g_stdout = 0; 145 | } 146 | 147 | } // namespace emb 148 | 149 | int main() 150 | { 151 | PyImport_AppendInittab("emb", emb::PyInit_emb); 152 | Py_Initialize(); 153 | PyImport_ImportModule("emb"); 154 | 155 | PyRun_SimpleString("print(\'hello to console\')"); 156 | 157 | // here comes the ***magic*** 158 | std::string buffer; 159 | { 160 | // switch sys.stdout to custom handler 161 | emb::stdout_write_type write = [&buffer] (std::string s) { buffer += s; }; 162 | emb::set_stdout(write); 163 | PyRun_SimpleString("print(\'hello to buffer\')"); 164 | PyRun_SimpleString("print(3.14)"); 165 | PyRun_SimpleString("print(\'still talking to buffer\')"); 166 | emb::reset_stdout(); 167 | } 168 | 169 | PyRun_SimpleString("print(\'hello to console again\')"); 170 | Py_Finalize(); 171 | 172 | // output what was written to buffer object 173 | std::clog << buffer << std::endl; 174 | } 175 | -------------------------------------------------------------------------------- /python/faq_incomplete_input.cpp: -------------------------------------------------------------------------------- 1 | // Posted to python-list 2 | // http://mail.python.org/pipermail/python-list/2012-January/1286379.html 3 | // 4 | // A quick and dirty C++ version of the C program presented in Python FAQ: 5 | // http://docs.python.org/py3k/faq/extending.html#how-do-i-tell-incomplete-input-from-invalid-input 6 | // Modifications: 7 | // - do not use readline library, but 8 | // 9 | // Tested using Visual C++ 2010 (10.0) and Python 3.2 (custom Debug build) 10 | // 11 | // The "incomplete input" solution presented in the FAQ is incomplete and it does 12 | // not allow multi-line scripts with more than 2 lines of flow control statements: 13 | // 14 | //>>> n = 10 15 | //>>> if n > 0: 16 | //... if n < 100: 17 | // File "", line 2 18 | // if n < 100: 19 | // ^ 20 | //IndentationError: expected an indented block 21 | //>>> 22 | // 23 | //>>> for n in range(0, 5): 24 | //... if n > 2: 25 | // File "", line 2 26 | // if n > 2: 27 | // ^ 28 | //IndentationError: expected an indented block 29 | //>>> 30 | // 31 | #include 32 | #include 33 | #include 34 | 35 | #include 36 | #include 37 | #include 38 | #include 39 | 40 | int main (int argc, char* argv[]) 41 | { 42 | int i, j, done = 0; /* lengths of line, code */ 43 | char ps1[] = ">>> "; 44 | char ps2[] = "... "; 45 | char *prompt = ps1; 46 | char *msg, *code = NULL; 47 | char const* line(NULL); 48 | PyObject *src, *glb, *loc; 49 | PyObject *exc, *val, *trb, *obj, *dum; 50 | std::string input_line; 51 | 52 | Py_Initialize (); 53 | loc = PyDict_New (); 54 | glb = PyDict_New (); 55 | PyDict_SetItemString (glb, "__builtins__", PyEval_GetBuiltins ()); 56 | 57 | while (!done) 58 | { 59 | std::cout << prompt; 60 | std::getline(std::cin, input_line); 61 | 62 | if (input_line.empty() || input_line[0] == 4 /*EOT*/) /* CTRL-Z or CTRL-D pressed */ 63 | { 64 | done = 1; 65 | } 66 | else 67 | { 68 | line = input_line.c_str(); 69 | i = strlen (line); 70 | if (NULL == code) /* nothing in code yet */ 71 | j = 0; 72 | else 73 | j = strlen (code); 74 | 75 | code = (char*)realloc (code, i + j + 2); 76 | if (NULL == code) /* out of memory */ 77 | exit (1); 78 | 79 | if (0 == j) /* code was empty, so */ 80 | code[0] = '\0'; /* keep strncat happy */ 81 | 82 | strncat (code, line, i); /* append line to code */ 83 | code[i + j] = '\n'; /* append '\n' to code */ 84 | code[i + j + 1] = '\0'; 85 | 86 | src = Py_CompileString (code, "", Py_single_input); 87 | 88 | if (NULL != src) /* compiled just fine - */ 89 | { 90 | if (ps1 == prompt || /* ">>> " or */ 91 | /* "... " and double '\n' */ 92 | ('\r' == code[i + j - 1] && /* Windows-specific fix */ 93 | '\n' == code[i + j - 2])) 94 | { /* so execute it */ 95 | dum = PyEval_EvalCode (src, glb, loc); 96 | Py_XDECREF (dum); 97 | Py_XDECREF (src); 98 | free (code); 99 | code = NULL; 100 | if (PyErr_Occurred ()) 101 | PyErr_Print (); 102 | prompt = ps1; 103 | } 104 | } /* syntax error or E_EOF? */ 105 | else if (PyErr_ExceptionMatches (PyExc_SyntaxError)) 106 | { 107 | // mloskot also proposes FIX 1 (above): 108 | // || PyErr_ExceptionMatches (PyExc_IndentationError) 109 | 110 | // mloskot also proposes FIX 2 (below): 111 | // || strcmp (msg, "expected an indented block") && ps2 == prompt 112 | 113 | PyErr_Fetch (&exc, &val, &trb); /* clears exception! */ 114 | 115 | if (PyArg_ParseTuple (val, "sO", &msg, &obj) && 0 == strcmp (msg, "unexpected EOF while parsing")) /* E_EOF */ 116 | { 117 | Py_XDECREF (exc); 118 | Py_XDECREF (val); 119 | Py_XDECREF (trb); 120 | prompt = ps2; 121 | } 122 | else /* some other syntax error */ 123 | { 124 | PyErr_Restore (exc, val, trb); 125 | PyErr_Print (); 126 | free (code); 127 | code = NULL; 128 | prompt = ps1; 129 | } 130 | } 131 | else /* some non-syntax error */ 132 | { 133 | PyErr_Print (); 134 | free (code); 135 | code = NULL; 136 | prompt = ps1; 137 | } 138 | } 139 | } 140 | 141 | Py_XDECREF(glb); 142 | Py_XDECREF(loc); 143 | Py_Finalize(); 144 | exit(0); 145 | } -------------------------------------------------------------------------------- /python/serial_subinterpreters.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2011 Mateusz Loskot 3 | // Distributed under the Boost Software License, Version 1.0. 4 | // (See accompanying file LICENSE_1_0.txt or copy at 5 | // http://www.boost.org/LICENSE_1_0.txt) 6 | // 7 | 8 | // Simple example of using Python sub-interpreters to achieve (almost) 9 | // totally separate environment for the execution of Python code. 10 | // Constraints: 11 | // * Serial use of sub-interpreters 12 | // * Single process 13 | // * No Python threads 14 | // * No non-Python threads 15 | 16 | #ifdef WITH_VISUAL_LEAK_DETECTOR 17 | #include // Py_InitializeEx gives hundreds of leaks 18 | #endif 19 | #include 20 | #include 21 | #include 22 | 23 | int main() 24 | { 25 | wchar_t* PYTHONHOME = L"c:\\Python32_d"; 26 | Py_SetPythonHome(PYTHONHOME); 27 | 28 | if (!Py_IsInitialized()) 29 | Py_Initialize(); //Py_InitializeEx(0); 30 | 31 | // See http://docs.python.org/py3k/c-api/init.html#non-python-created-threads 32 | // Python supports the creation of additional interpreters (using Py_NewInterpreter()), 33 | // but mixing multiple interpreters and the PyGILState_*() API is unsupported. 34 | assert(0 == PyEval_ThreadsInitialized()); 35 | PyRun_SimpleString("import dummy_threading"); // indicates limitations 36 | 37 | PyThreadState* interp_main = PyThreadState_Get(); 38 | PyThreadState* interp_sub1 = Py_NewInterpreter(); // this thread state is made in current (implicit swap) 39 | PyThreadState* interp_sub2 = Py_NewInterpreter(); // this thread state is made in current 40 | PyThreadState_Swap(nullptr); // make no interprter active, allow use of sub-interpreters only 41 | 42 | // Switch to sub1 43 | { 44 | PyThreadState_Swap(interp_sub1); 45 | PyRun_SimpleString("print(\'interp_sub1\')"); 46 | PyRun_SimpleString("import sys"); 47 | PyRun_SimpleString("print(dir())"); 48 | PyRun_SimpleString("so = open(\'interp_sub1.log\', mode=\'w\')"); // log in getcwd() 49 | PyRun_SimpleString("sys.stdout = so"); 50 | PyRun_SimpleString("print(sys.stdout)"); 51 | PyRun_SimpleString("print(dir())"); 52 | PyThreadState_Swap(nullptr); 53 | //PyRun_SimpleString("print(\'pif paf! dead!\')"); // uncomment to see yourself 54 | } 55 | 56 | // Switch to sub2 57 | { 58 | PyThreadState_Swap(interp_sub2); 59 | PyRun_SimpleString("print(\'interp_sub2\')"); 60 | PyRun_SimpleString("import os"); 61 | PyRun_SimpleString("print(dir())"); 62 | PyRun_SimpleString("import sys"); 63 | PyThreadState_Swap(nullptr); 64 | } 65 | 66 | // Switch to sub1 again 67 | { 68 | PyThreadState_Swap(interp_sub1); 69 | PyRun_SimpleString("print(\'interp_sub1 continued\')"); // still into log 70 | PyRun_SimpleString("so.close()"); 71 | PyThreadState_Swap(nullptr); 72 | } 73 | 74 | // Clean sub1 75 | PyThreadState_Swap(interp_sub1); 76 | Py_EndInterpreter(interp_sub1); 77 | PyThreadState_Swap(nullptr); 78 | // Clean sub2 79 | PyThreadState_Swap(interp_sub2); 80 | Py_EndInterpreter(interp_sub2); 81 | PyThreadState_Swap(nullptr); 82 | // Clean switch to main before finalizing 83 | PyThreadState_Swap(interp_main); 84 | 85 | Py_Finalize(); 86 | 87 | return 0; 88 | } 89 | -------------------------------------------------------------------------------- /python/simple.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2011 Mateusz Loskot 3 | // Distributed under the Boost Software License, Version 1.0. 4 | // (See accompanying file LICENSE_1_0.txt or copy at 5 | // http://www.boost.org/LICENSE_1_0.txt) 6 | // 7 | 8 | #include 9 | #include 10 | #include 11 | 12 | int main() 13 | { 14 | try 15 | { 16 | Py_Initialize(); 17 | PyRun_SimpleString("print(\'import tkinter\')"); 18 | PyRun_SimpleString("import sys, tkinter"); 19 | PyRun_SimpleString("tkinter.Tk()"); 20 | return EXIT_SUCCESS; 21 | } 22 | catch (std::exception const& e) 23 | { 24 | std::cerr << e.what() << std::endl; 25 | } 26 | catch (...) 27 | { 28 | std::cerr << "unknown error\n"; 29 | } 30 | return EXIT_FAILURE; 31 | } -------------------------------------------------------------------------------- /python/tkinter.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // Copyright (C) 2011 Mateusz Loskot 3 | // Distributed under the Boost Software License, Version 1.0. 4 | // (See accompanying file LICENSE_1_0.txt or copy at 5 | // http://www.boost.org/LICENSE_1_0.txt) 6 | // 7 | 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | #if PY_VERSION_HEX < 0x03030000 14 | #define PyMem_RawFree free 15 | wchar_t *Py_DecodeLocale(char *raw, size_t* s) 16 | { 17 | int size_needed = MultiByteToWideChar(CP_UTF8, 0, raw, -1, NULL, 0); 18 | wchar_t *rtn = (wchar_t *) calloc(1, size_needed * sizeof(wchar_t)); 19 | MultiByteToWideChar(CP_UTF8, 0, raw, -1, rtn, size_needed); 20 | return rtn; 21 | } 22 | #endif 23 | 24 | int main(int argc, char** argv) 25 | { 26 | try 27 | { 28 | wchar_t* arg0W = Py_DecodeLocale(argv[0], NULL); 29 | wchar_t** argvW = new wchar_t *[argc]; 30 | for (int i = 0; i < argc; i++) 31 | { 32 | auto arg = Py_DecodeLocale(argv[i], NULL); 33 | if (!arg) throw std::runtime_error("Fatal error: cannot decode argv[0]\n"); 34 | argvW[i] = arg; 35 | } 36 | Py_SetProgramName(argvW[0]); 37 | 38 | Py_Initialize(); 39 | 40 | PySys_SetArgv(argc, argvW); 41 | 42 | PyRun_SimpleString("import tkinter\n"); 43 | PyRun_SimpleString("print(tkinter)\n"); 44 | PyRun_SimpleString("t=tkinter.Tk()\n"); 45 | PyRun_SimpleString("t.mainloop()\n"); 46 | 47 | Py_Finalize(); 48 | PyMem_RawFree(arg0W); 49 | for (int i = 0; i < argc; i++) 50 | PyMem_RawFree(argvW[i]); 51 | delete [] argvW; 52 | 53 | return EXIT_SUCCESS; 54 | } 55 | catch (std::exception const& e) 56 | { 57 | std::cerr << e.what() << std::endl; 58 | } 59 | catch (...) 60 | { 61 | std::cerr << "unknown error\n"; 62 | } 63 | return EXIT_FAILURE; 64 | } -------------------------------------------------------------------------------- /scripts/create_postgis_template.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | ### CONFIGURATION ### 3 | POSTGIS=/usr/pkg/share/postgresql/contrib/postgis-1.5 4 | ##################### 5 | 6 | # Creating the template spatial database 7 | createdb -T template0 template_postgis 8 | # and add PLPGSQL language support. 9 | createlang -d template_postgis plpgsql 10 | 11 | # Loading the PostGIS SQL routines. 12 | psql -d template_postgis -f $POSTGIS/postgis.sql 13 | psql -d template_postgis -f $POSTGIS/spatial_ref_sys.sql 14 | 15 | # Enabling users to alter spatial tables. 16 | psql -d template_postgis -c "GRANT ALL ON geometry_columns TO PUBLIC;" 17 | psql -d template_postgis -c "GRANT ALL ON geography_columns TO PUBLIC;" 18 | psql -d template_postgis -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;" 19 | 20 | # Garbage-collect and freeze. 21 | psql -d template_postgis -c "VACUUM FULL;" 22 | psql -d template_postgis -c "VACUUM FREEZE;" 23 | 24 | # Allows non-superusers the ability to create from this template. 25 | psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';" 26 | psql -d postgres -c "UPDATE pg_database SET datallowconn='false' WHERE datname='template_postgis';" 27 | -------------------------------------------------------------------------------- /scripts/gdalbb.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python 2 | """Request building GDAL on the remote Buildbot instance. 3 | 4 | Usage: gdalbb.py [option] 5 | 6 | Options: 7 | -h Print this message and exit. 8 | -l List builders available to run 9 | -f Run all 'full' builders in a batch 10 | -q Run all 'quick' builders in a batch 11 | -b Run selected builder 12 | 13 | Mateusz Loskot 14 | """ 15 | ### BEGIN CONFIGURATION BLOCK ### 16 | BBURL = 'http://buildbot.osgeo.org:8500/' 17 | BBUSER = 'mloskot' 18 | BBNOTE = 'Routine build' 19 | #### END CONFIGURATION BLOCK #### 20 | import sys 21 | import getopt 22 | import urllib 23 | import urllib2 24 | import xml.parsers.expat 25 | 26 | def find(f, seq): 27 | """Return first item in sequence where f(item) == True.""" 28 | for item in seq: 29 | if f(item): 30 | return item 31 | 32 | class BuildersFinder: 33 | 34 | pxml = None 35 | found = False 36 | quick = [] 37 | full = [] 38 | other = [] 39 | 40 | def __init__(self): 41 | self.pxml = xml.parsers.expat.ParserCreate() 42 | self.pxml.StartElementHandler = self.handleStartElement 43 | self.pxml.EndElementHandler = self.handleEndElement 44 | self.pxml.CharacterDataHandler = self.handleCharData 45 | self.found = False 46 | 47 | def handleStartElement(self, name, attrs): 48 | if name == 'td': 49 | a = find(lambda k: k == 'class', attrs.keys()) 50 | if a is not None: 51 | if attrs[a] == 'Builder': 52 | self.found = True 53 | 54 | if self.found is True and name == 'a': 55 | bldr = attrs['href'] 56 | type = bldr[-4:] 57 | if type == 'uick': 58 | self.quick.append(bldr) 59 | elif type == 'full': 60 | self.full.append(bldr) 61 | else: 62 | self.other.append(bldr) 63 | self.found = False; 64 | 65 | def handleEndElement(self, name): 66 | pass 67 | 68 | def handleCharData(self, data): 69 | pass 70 | 71 | def collect(self, html): 72 | assert(self.pxml is not None) 73 | self.pxml.Parse(html); 74 | 75 | class Usage(Exception): 76 | def __init__(self, msg): 77 | self.msg = msg 78 | 79 | def usage(code): 80 | print >> sys.stderr, __doc__ 81 | return code 82 | 83 | def list(builders, msg): 84 | print msg,':' 85 | i = 0 86 | for b in builders: 87 | i = i + 1 88 | print '%d. %s' % (i, b) 89 | 90 | def build(builders, data, msg): 91 | print msg,':' 92 | tmp = urllib.urlencode(data) 93 | url = BBURL + '%s/force' 94 | i = 0 95 | for b in builders: 96 | i = i + 1 97 | addr = url % b 98 | print " %d. %s" % (i, b) 99 | request = urllib2.Request(addr, tmp) 100 | response = urllib2.urlopen(request) 101 | 102 | def main(argv): 103 | 104 | url = BBURL 105 | values = { 'username' : BBUSER, 'comments' : BBNOTE } 106 | 107 | try: 108 | try: 109 | opts, args = getopt.getopt(argv, 'hlfqb:', []) 110 | if len(opts) == 0: 111 | sys.exit(usage(0)) 112 | except getopt.GetoptError, msg: 113 | raise Usage(msg) 114 | 115 | for opt, arg in opts: 116 | if opt == '-h': 117 | usage(0) 118 | break 119 | else: 120 | print "Connecting to GDAL Buildbot instance: %s" % BBURL 121 | response = urllib2.urlopen(url) 122 | html = response.read() 123 | finder = BuildersFinder() 124 | finder.collect(html) 125 | 126 | if opt == '-l' or opt == '-b': 127 | tmp = finder.quick 128 | tmp += finder.full 129 | 130 | if opt == '-l': 131 | list(tmp, 'List of available builders') 132 | break 133 | 134 | for b in tmp: 135 | if b == arg: 136 | build(tmp, values, 'Building GDAL on') 137 | break 138 | raise ValueError, 'Requested builder \'%s\' is unavailable' % arg 139 | elif opt == '-f': 140 | build(finder.full, values, 'Building GDAL on') 141 | break 142 | elif opt == '-q': 143 | build(finder.quick, values, 'Building GDAL on') 144 | break 145 | return 0 146 | 147 | except urllib2.HTTPError, err: 148 | print >> sys.stderr, 'Error: HTTP', err.code, '-', err.msg 149 | except urllib2.URLError, err: 150 | print >> sys.stderr, 'Error: ', err.reason 151 | except Usage, err: 152 | print >> sys.stderr, 'Error: ', err.msg 153 | print >> sys.stderr, 'Use -h to get usage options' 154 | except Exception, err: 155 | print >> sys.stderr, 'Error: ', err 156 | 157 | return 2 158 | 159 | # end of main() 160 | 161 | if __name__ == "__main__": 162 | sys.exit(main(sys.argv[1:])) 163 | 164 | -------------------------------------------------------------------------------- /scripts/svnkeywords.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Author: Mateusz Loskot, mateusz@loskot 4 | # Revision: $Revision$ 5 | # Date: $Date$ 6 | # 7 | # The script purpose is to reset svn:keywords property for files 8 | # identified as using keywords. 9 | # 10 | # Set to 1 to print detailed listing of touched files and set keywords 11 | VERBOSE=0 12 | 13 | if test ! $# = 1; then 14 | echo "Usage: `basename $0` " 15 | exit 1 16 | fi 17 | 18 | SVNSRCPATH=${1} 19 | if test ! -d ${SVNSRCPATH}; then 20 | echo "*** Given source location is not valid directory '${SVNSRCPATH}'" 21 | exit 1 22 | fi 23 | 24 | SVN=`which svn` 25 | FIND=`which find` 26 | GREP=`which grep` 27 | 28 | if test ! -x ${SVN}; then 29 | echo "Can not find Subversion program" 30 | exit 1 31 | fi 32 | 33 | if test ! -x ${FIND}; then 34 | echo "Can not locate find program" 35 | exit 1 36 | fi 37 | 38 | if test ! -x ${GREP}; then 39 | echo "Can not locate grep program" 40 | exit 1 41 | fi 42 | 43 | echo "Entering '${SVNSRCPATH}'" 44 | echo "Setting svn:keywords property\c" 45 | 46 | ${FIND} ${SVNSRCPATH} \( -path '*/.svn' \) -prune -o -type f -print | while read file ; do 47 | 48 | plist="" 49 | for p in Author Date Id Rev ; do 50 | if ${GREP} -q '\$'${p}'' "${file}" ; then 51 | plist="${p} $plist" 52 | fi 53 | done 54 | 55 | if [ "$plist" != "" ] ; then 56 | ${SVN} propset svn:keywords "${plist%% }" ${file} > /dev/null 2>&1 57 | if test ${VERBOSE} = 1; then 58 | echo "${SVN} propset svn:keywords "${plist%% }" ${file} " 59 | else 60 | echo ".\c" 61 | fi 62 | fi 63 | done 64 | 65 | echo "done." 66 | -------------------------------------------------------------------------------- /soci/examples/example1.cpp: -------------------------------------------------------------------------------- 1 | #ifdef _MSC_VER 2 | #pragma comment(lib, "libsoci_core-vc80-d-3_0.lib") 3 | #pragma comment(lib, "libsoci_postgresql-vc80-d-3_0.lib") 4 | #endif 5 | 6 | // soci 7 | #include 8 | #include 9 | // std 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | 16 | using namespace std; 17 | 18 | bool getName(string &name) 19 | { 20 | cout << "Enter name: "; 21 | return !!(cin >> name); 22 | } 23 | 24 | int main() 25 | { 26 | try 27 | { 28 | soci::session sql(soci::postgresql, "dbname=soci_test user=mloskot"); 29 | 30 | int count(0); 31 | sql << "select count(*) from phonebook", soci::into(count); 32 | 33 | cout << "We have " << count << " entries in the phonebook.\n"; 34 | 35 | string name; 36 | while (getName(name)) 37 | { 38 | string phone; 39 | soci::indicator ind; 40 | sql << "select phone from phonebook where name = :name", soci::into(phone, ind), soci::use(name); 41 | 42 | std::clog << sql.get_last_query() << std::endl; 43 | 44 | if (ind == soci::i_ok) 45 | { 46 | cout << "The phone number is " << phone << '\n'; 47 | } 48 | else 49 | { 50 | cout << "There is no phone for " << name << '\n'; 51 | } 52 | } 53 | } 54 | catch (exception const &e) 55 | { 56 | cerr << "Error: " << e.what() << '\n'; 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /soci/examples/example1.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 26 | 29 | 32 | 35 | 38 | 41 | 53 | 56 | 59 | 62 | 69 | 72 | 75 | 78 | 81 | 84 | 87 | 90 | 93 | 94 | 103 | 106 | 109 | 112 | 115 | 118 | 127 | 130 | 133 | 136 | 145 | 148 | 151 | 154 | 157 | 160 | 163 | 166 | 169 | 170 | 171 | 172 | 173 | 174 | 177 | 178 | 179 | 180 | 181 | 182 | -------------------------------------------------------------------------------- /soci/examples/example2.cpp: -------------------------------------------------------------------------------- 1 | #ifdef _MSC_VER 2 | #pragma comment(lib, "libsoci_core-vc80-d-3_0.lib") 3 | #pragma comment(lib, "libsoci_postgresql-vc80-d-3_0.lib") 4 | #endif 5 | 6 | // soci 7 | #include 8 | #include 9 | // std 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | using namespace std; 16 | 17 | struct PhonebookEntry 18 | { 19 | std::string name; 20 | std::string phone; 21 | }; 22 | 23 | namespace soci 24 | { 25 | 26 | template 27 | struct soci_data_type_tag { typedef std::string value; }; 28 | template <> 29 | struct soci_data_type_tag { typedef std::string value; }; 30 | template <> 31 | struct soci_data_type_tag { typedef double value; }; 32 | template <> 33 | struct soci_data_type_tag { typedef int value; }; 34 | template <> 35 | struct soci_data_type_tag { typedef unsigned long value; }; 36 | template <> 37 | struct soci_data_type_tag { typedef long long value; }; 38 | 39 | // basic type conversion on many values (ORM) 40 | template<> 41 | struct type_conversion 42 | { 43 | typedef soci::values base_type; 44 | 45 | static void from_base(values const &v, indicator /* ind */, PhonebookEntry &pe) 46 | { 47 | if (dt_string == v.get_properties("name").get_data_type()) 48 | { 49 | pe.name = v.get::value>("name"); 50 | } 51 | 52 | if (dt_string == v.get_properties("phone").get_data_type()) 53 | { 54 | pe.phone = v.get::value>("phone", ""); 55 | } 56 | } 57 | 58 | static void to_base(PhonebookEntry const &pe, values &v, indicator &ind) 59 | { 60 | v.set("name", pe.name); 61 | v.set("phone", pe.phone, pe.phone.empty() ? i_null : i_ok); 62 | ind = i_ok; 63 | } 64 | }; 65 | } // namespace soci 66 | 67 | int main() 68 | { 69 | try 70 | { 71 | soci::session sql(soci::postgresql, "dbname=soci_test user=mloskot"); 72 | 73 | PhonebookEntry p1; 74 | 75 | int id(1); 76 | sql << "select * from phonebook where id = :id", soci::into(p1), soci::use(id); 77 | cout << p1.name << ": " << p1.phone << endl; 78 | } 79 | catch (exception const &e) 80 | { 81 | cerr << "Error: " << e.what() << '\n'; 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /soci/examples/example2.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 26 | 29 | 32 | 35 | 38 | 41 | 53 | 56 | 59 | 62 | 69 | 72 | 75 | 78 | 81 | 84 | 87 | 90 | 93 | 94 | 103 | 106 | 109 | 112 | 115 | 118 | 127 | 130 | 133 | 136 | 145 | 148 | 151 | 154 | 157 | 160 | 163 | 166 | 169 | 170 | 171 | 172 | 173 | 174 | 177 | 178 | 179 | 180 | 181 | 182 | -------------------------------------------------------------------------------- /soci/examples/examples.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 9.00 3 | # Visual Studio 2005 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example1", "example1.vcproj", "{4161B28F-7B8F-42CA-9A96-A4ECBB7BA0E7}" 5 | EndProject 6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example2", "example2.vcproj", "{4161B28F-7B8F-42CA-9A96-A4ECCB7BA0E7}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Win32 = Debug|Win32 11 | Release|Win32 = Release|Win32 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {4161B28F-7B8F-42CA-9A96-A4ECBB7BA0E7}.Debug|Win32.ActiveCfg = Debug|Win32 15 | {4161B28F-7B8F-42CA-9A96-A4ECBB7BA0E7}.Debug|Win32.Build.0 = Debug|Win32 16 | {4161B28F-7B8F-42CA-9A96-A4ECBB7BA0E7}.Release|Win32.ActiveCfg = Release|Win32 17 | {4161B28F-7B8F-42CA-9A96-A4ECBB7BA0E7}.Release|Win32.Build.0 = Release|Win32 18 | {4161B28F-7B8F-42CA-9A96-A4ECCB7BA0E7}.Debug|Win32.ActiveCfg = Debug|Win32 19 | {4161B28F-7B8F-42CA-9A96-A4ECCB7BA0E7}.Debug|Win32.Build.0 = Debug|Win32 20 | {4161B28F-7B8F-42CA-9A96-A4ECCB7BA0E7}.Release|Win32.ActiveCfg = Release|Win32 21 | {4161B28F-7B8F-42CA-9A96-A4ECCB7BA0E7}.Release|Win32.Build.0 = Release|Win32 22 | EndGlobalSection 23 | GlobalSection(SolutionProperties) = preSolution 24 | HideSolutionNode = FALSE 25 | EndGlobalSection 26 | EndGlobal 27 | -------------------------------------------------------------------------------- /soci/soci.vsprops: -------------------------------------------------------------------------------- 1 | 2 | 7 | 11 | 15 | 19 | 23 | 24 | -------------------------------------------------------------------------------- /wkt/Makefile: -------------------------------------------------------------------------------- 1 | CPP = g++ 2 | LDFLAGS = -L/usr/local/lib -lboost_unit_test_framework-gcc 3 | CFLAGS = -g -I/usr/local/include/boost-1_34 -I../src $(CPPFLAGS) 4 | PROGS = ogc_wkt_grammar_test ogc_wkt_validator_test error_handling_test 5 | 6 | all: $(PROGS) 7 | 8 | ogc_wkt_grammar_test: ogc_wkt_grammar_test.cpp 9 | $(CPP) $(CFLAGS) $< -o $@ $(LDFLAGS) 10 | 11 | ogc_wkt_validator_test: ogc_wkt_validator_test.cpp 12 | $(CPP) $(CFLAGS) $< -o $@ $(LDFLAGS) 13 | 14 | error_handling_test: error_handling_test.cpp 15 | $(CPP) $(CFLAGS) $< -o $@ $(LDFLAGS) 16 | 17 | test: all 18 | $(PROGS) 19 | 20 | clean: 21 | $(RM) $(PROGS) *.o *.a *~ *.out 22 | 23 | -------------------------------------------------------------------------------- /wkt/README: -------------------------------------------------------------------------------- 1 | OGC Well-Known-Text hacks 2 | -------------------------------------------------------------------------------- /wkt/error_handling_test.cpp: -------------------------------------------------------------------------------- 1 | // $Id: error_handling_test.cpp 89 2008-07-25 20:50:50Z mateusz@loskot.net $ 2 | #include "ogc_wkt_grammar.hpp" 3 | #include 4 | #include 5 | #include 6 | 7 | 8 | int main() 9 | { 10 | std::string wkt("POINT 10 20)"); 11 | ogc::wkt::grammar gram; 12 | 13 | 14 | if (parse(wkt.c_str(), gram).full) 15 | std::cout << "OK\n"; 16 | else 17 | std::cout << "DUPA\n"; 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /wkt/ogc_wkt_grammar.hpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////// 2 | // $Id: ogc_wkt_grammar.hpp 89 2008-07-25 20:50:50Z mateusz@loskot.net $ 3 | // 4 | // Implementation of Grammar Rules for Well-Known-Text (WKT) format. 5 | // 6 | // This implementation is based on definition of WKT format available 7 | // in the "OpenGIS Simple Features Specification For SQL (Revision 1.1)" 8 | // by Open Geospatial Consortium, Inc. 9 | // 10 | /////////////////////////////////////////////////////////////////////////////// 11 | #ifndef OGC_WKT_GRAMMAR_HPP_INCLUDED 12 | #define OGC_WKT_GRAMMAR_HPP_INCLUDED 13 | 14 | #include 15 | #include 16 | #include // std::ptrdiff_t 17 | #include 18 | #include 19 | 20 | /////////////////////////////////////////////////////////////////////////////// 21 | 22 | namespace ogc { 23 | namespace wkt { 24 | 25 | namespace sp = boost::spirit; 26 | 27 | struct error_report_parser 28 | { 29 | error_report_parser(const char *msg) 30 | : _msg(msg) 31 | {} 32 | 33 | typedef sp::nil_t result_t; 34 | 35 | template 36 | int operator()(ScannerT const& scan, result_t& /*result*/) const 37 | { 38 | std::cerr << _msg << std::endl; 39 | return 0; 40 | } 41 | 42 | private: 43 | std::string _msg; 44 | }; 45 | 46 | typedef sp::functor_parser error_report_p; 47 | 48 | error_report_p my_grammar::error_missing_semicolon("missing semicolon"); 49 | 50 | struct grammar : public sp::grammar 51 | { 52 | error_report_p error_point_tag_expected; 53 | 54 | grammar() 55 | : error_point_tag_expected("POINT tag expected") 56 | {} 57 | 58 | template 59 | struct definition 60 | { 61 | definition(grammar const& self) 62 | : empty_txt_p("EMPTY") 63 | { 64 | // 65 | // Definition of Highest Level Rules 66 | // 67 | 68 | // Geometry Tagged Text 69 | geometry_tag_r 70 | = point_tag_r 71 | | linestring_tag_r 72 | | linearring_tag_r 73 | | polygon_tag_r 74 | | multipoint_tag_r 75 | | multilinestring_tag_r 76 | | multipolygon_tag_r 77 | | geometrycollection_tag_r 78 | ; 79 | 80 | // Point Tagged Text 81 | point_tag_r 82 | = ("POINT" | error_point_tag_expected) 83 | >> *(sp::blank_p) >> point_txt_r 84 | ; 85 | 86 | // LineString Tagged Text 87 | linestring_tag_r 88 | = "LINESTRING" 89 | >> *(sp::blank_p) >> linestring_txt_r 90 | ; 91 | 92 | // LinearRing Tagged Text 93 | linearring_tag_r 94 | = "LINEARRING" 95 | >> *(sp::blank_p) >> linestring_txt_r 96 | ; 97 | 98 | // Polygon Tagged Text 99 | polygon_tag_r 100 | = "POLYGON" 101 | >> *(sp::blank_p) >> polygon_txt_r 102 | ; 103 | 104 | // MultiPoint Tagged Text 105 | multipoint_tag_r 106 | = "MULTIPOINT" 107 | >> *(sp::blank_p) >> multipoint_txt_r 108 | ; 109 | 110 | // MultiLineString Tagged Text 111 | multilinestring_tag_r 112 | = "MULTILINESTRING" 113 | >> *(sp::blank_p) >> multilinestring_txt_r 114 | ; 115 | 116 | // MultiPolygon Tagged Text 117 | multipolygon_tag_r 118 | = "MULTIPOLYGON" 119 | >> *(sp::blank_p) >> multipolygon_txt_r 120 | ; 121 | 122 | // GeometryCollection Tagged Text 123 | geometrycollection_tag_r 124 | = "GEOMETRYCOLLECTION" 125 | >> *(sp::blank_p) >> geometrycollection_txt_r 126 | ; 127 | 128 | // 129 | // Definition of Single Geometry Level Rules 130 | // 131 | 132 | // Point Text 133 | point_txt_r 134 | = empty_txt_p 135 | | '(' >> point_r >> ')' 136 | ; 137 | 138 | point_r 139 | = *(sp::blank_p) 140 | >> x_p 141 | >> *(sp::blank_p) 142 | >> y_p 143 | >> *(sp::blank_p) 144 | ; 145 | 146 | x_p = sp::real_p // Double precision literal 147 | ; 148 | 149 | y_p = sp::real_p // Double precision literal 150 | ; 151 | 152 | // LineString Text 153 | linestring_txt_r 154 | = empty_txt_p 155 | | *(sp::blank_p) 156 | >> '(' 157 | >> point_r 158 | >> *(',' >> point_r) 159 | >> ')' 160 | >> *(sp::blank_p) 161 | ; 162 | 163 | // Polygon Text 164 | polygon_txt_r 165 | = empty_txt_p 166 | | '(' >> linestring_txt_r 167 | >> *(',' >> linestring_txt_r) 168 | >> ')' 169 | ; 170 | 171 | // MultiPoint Text 172 | multipoint_txt_r 173 | = empty_txt_p 174 | | '(' >> *(sp::blank_p) 175 | >> '(' 176 | >> *(sp::blank_p) 177 | >> point_r 178 | >> *(sp::blank_p) 179 | >> ')' 180 | >> *( *(sp::blank_p) 181 | >> ',' 182 | >> *(sp::blank_p) 183 | >> '(' 184 | >> point_r 185 | >> ')') 186 | >> *(sp::blank_p) 187 | >> ')' 188 | ; 189 | 190 | // MultiLineString Text 191 | multilinestring_txt_r 192 | = empty_txt_p 193 | | '(' 194 | >> linestring_txt_r 195 | >> *(',' >> linestring_txt_r) 196 | >> ')' 197 | ; 198 | 199 | // MultiPolygon Text 200 | multipolygon_txt_r 201 | = empty_txt_p 202 | | '(' >> polygon_txt_r >> *(',' >> polygon_txt_r) >> ')' 203 | ; 204 | 205 | geometrycollection_txt_r 206 | = empty_txt_p 207 | | '(' >> geometry_tag_r >> *(',' >> geometry_tag_r) >> ')' 208 | ; 209 | 210 | // 211 | // Definition of Common Helper Rules 212 | // 213 | 214 | empty_txt_p = "EMPTY" 215 | ; 216 | } 217 | 218 | // Highest Level Rules 219 | sp::rule geometry_tag_r; 220 | sp::rule point_tag_r; 221 | sp::rule linestring_tag_r; 222 | sp::rule linearring_tag_r; 223 | sp::rule polygon_tag_r; 224 | sp::rule multipoint_tag_r; 225 | sp::rule multilinestring_tag_r; 226 | sp::rule multipolygon_tag_r; 227 | sp::rule geometrycollection_tag_r; 228 | 229 | // Lowest level components 230 | sp::rule point_r; 231 | sp::rule x_p; 232 | sp::rule y_p; 233 | 234 | // Single Geometry Level Rules 235 | sp::rule point_txt_r; 236 | sp::rule linestring_txt_r; 237 | sp::rule polygon_txt_r; 238 | sp::rule multipoint_txt_r; 239 | sp::rule multilinestring_txt_r; 240 | sp::rule multipolygon_txt_r; 241 | sp::rule geometrycollection_txt_r; 242 | 243 | // Common Helper Rules 244 | sp::strlit<> empty_txt_p; // "EMPTY" 245 | 246 | // Public interface 247 | sp::rule const& start() const 248 | { 249 | return geometry_tag_r; 250 | } 251 | }; 252 | }; 253 | 254 | } // namespace wkt 255 | } // namespace ogc 256 | 257 | #endif // #ifndef OGC_WKT_GRAMMAR_HPP_INCLUDED 258 | 259 | // EOF 260 | -------------------------------------------------------------------------------- /wkt/ogc_wkt_grammar_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mloskot/workshop/78333ee94931dd6fd78aab5819f54c58d5fd47df/wkt/ogc_wkt_grammar_test.cpp -------------------------------------------------------------------------------- /wkt/ogc_wkt_validator.hpp: -------------------------------------------------------------------------------- 1 | /////////////////////////////////////////////////////////////////////////////// 2 | // $Id: ogc_wkt_validator.hpp 89 2008-07-25 20:50:50Z mateusz@loskot.net $ 3 | // 4 | // Implementation of Grammar Rules for Well-Known-Text (WKT) format. 5 | // 6 | // This implementation is based on definition of WKT format available 7 | // in the "OpenGIS Simple Features Specification For SQL (Revision 1.1)" 8 | // by Open Geospatial Consortium, Inc. 9 | // 10 | /////////////////////////////////////////////////////////////////////////////// 11 | #ifndef OGC_WKT_VALIDATOR_HPP_INCLUDED 12 | #define OGC_WKT_VALIDATOR_HPP_INCLUDED 13 | 14 | #include 15 | #include 16 | #include "ogc_wkt_grammar.hpp" // ogc::wkt::grammar 17 | 18 | /////////////////////////////////////////////////////////////////////////////// 19 | 20 | namespace ogc { 21 | namespace wkt { 22 | 23 | template 24 | class simple_validator 25 | { 26 | public: 27 | 28 | simple_validator() {} 29 | 30 | bool check(std::string const& wkt) 31 | { 32 | return boost::spirit::parse(wkt.c_str(), grammar_).full; 33 | } 34 | 35 | private: 36 | 37 | G grammar_; 38 | 39 | // NonCopyable type 40 | simple_validator(simple_validator const&); 41 | simple_validator& operator=(simple_validator const&); 42 | }; 43 | 44 | } // namespace wkt 45 | } // namespace ogc 46 | 47 | #endif // #ifndef OGC_WKT_VALIDATOR_HPP_INCLUDED 48 | 49 | // EOF 50 | 51 | -------------------------------------------------------------------------------- /wkt/ogc_wkt_validator_test.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mloskot/workshop/78333ee94931dd6fd78aab5819f54c58d5fd47df/wkt/ogc_wkt_validator_test.cpp -------------------------------------------------------------------------------- /yatbinrw/msvc80/.gitignore: -------------------------------------------------------------------------------- 1 | *.ncb 2 | *.suo 3 | *.vcproj.*.user 4 | Debug 5 | Release 6 | -------------------------------------------------------------------------------- /yatbinrw/msvc80/yatbinrw.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 9.00 3 | # Visual Studio 2005 4 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "yatbinrw", "yatbinrw.vcproj", "{4D07473F-1E7F-463B-B68A-7044B67883D9}" 5 | EndProject 6 | Global 7 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 8 | Debug|Win32 = Debug|Win32 9 | Release|Win32 = Release|Win32 10 | EndGlobalSection 11 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 12 | {4D07473F-1E7F-463B-B68A-7044B67883D9}.Debug|Win32.ActiveCfg = Debug|Win32 13 | {4D07473F-1E7F-463B-B68A-7044B67883D9}.Debug|Win32.Build.0 = Debug|Win32 14 | {4D07473F-1E7F-463B-B68A-7044B67883D9}.Release|Win32.ActiveCfg = Release|Win32 15 | {4D07473F-1E7F-463B-B68A-7044B67883D9}.Release|Win32.Build.0 = Release|Win32 16 | EndGlobalSection 17 | GlobalSection(SolutionProperties) = preSolution 18 | HideSolutionNode = FALSE 19 | EndGlobalSection 20 | EndGlobal 21 | -------------------------------------------------------------------------------- /yatbinrw/msvc80/yatbinrw.vcproj: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 14 | 15 | 16 | 17 | 18 | 26 | 29 | 32 | 35 | 38 | 41 | 54 | 57 | 60 | 63 | 70 | 73 | 76 | 79 | 82 | 85 | 88 | 91 | 94 | 95 | 104 | 107 | 110 | 113 | 116 | 119 | 129 | 132 | 135 | 138 | 147 | 150 | 153 | 156 | 159 | 162 | 165 | 168 | 171 | 172 | 173 | 174 | 175 | 176 | 179 | 180 | 183 | 184 | 185 | 186 | 187 | 188 | -------------------------------------------------------------------------------- /yatbinrw/msvc80/yatbinrw.vsprops: -------------------------------------------------------------------------------- 1 | 2 | 7 | 12 | 16 | 17 | -------------------------------------------------------------------------------- /yatbinrw/yatbinrw.hpp: -------------------------------------------------------------------------------- 1 | // Yet Another Tiny Binary Reader/Writer 2 | // 3 | // Copyright Mateusz Loskot 2009 4 | // Copyright Darin Adler 2000 5 | // Copyright Beman Dawes 2006, 2009 6 | // Use, modification and distribution is subject to the Boost Software License, 7 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 8 | // http://www.boost.org/LICENSE_1_0.txt) 9 | // 10 | // Original design of unrolled_byte_loops templates taken from Boost C++ Libraries 11 | // (boost/spirit/home/support/detail/integer/endian.hpp) 12 | // 13 | 14 | #ifndef YATBINRW_HPP_INCLUDED 15 | #define YATBINRW_HPP_INCLUDED 16 | 17 | #include 18 | #include 19 | #include 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #include 27 | 28 | #if CHAR_BIT != 8 29 | #error Platforms with CHAR_BIT != 8 are not supported 30 | #endif 31 | 32 | namespace yatbinrw 33 | { 34 | 35 | // Unrolled loops for loading and storing streams of bytes. 36 | 37 | BOOST_STATIC_ASSERT(CHAR_BIT == 8); 38 | 39 | template ::value> 40 | struct unrolled_byte_loops 41 | { 42 | typedef unrolled_byte_loops next; 43 | 44 | static T load_big(const unsigned char* bytes) 45 | { 46 | return *(bytes - 1) | (next::load_big(bytes - 1) << 8); 47 | } 48 | 49 | template 50 | static T load_big(Iterator& bytes) 51 | { 52 | return *(bytes - 1) | (next::load_big(--bytes) << 8); 53 | } 54 | 55 | static T load_little(const unsigned char* bytes) 56 | { 57 | return *bytes | (next::load_little(bytes + 1) << 8); 58 | } 59 | 60 | template 61 | static T load_little(Iterator& bytes) 62 | { 63 | return *bytes++ | (next::load_little(bytes) << 8); 64 | } 65 | 66 | static void store_big(char* bytes, T value) 67 | { 68 | *(bytes - 1) = static_cast(value); 69 | next::store_big(bytes - 1, value >> 8); 70 | } 71 | 72 | template 73 | static void store_big(Iterator& bytes, T value) 74 | { 75 | *(bytes - 1) = static_cast(value); 76 | next::store_big(--bytes, value >> 8); 77 | } 78 | 79 | static void store_little(char* bytes, T value) 80 | { 81 | *bytes = static_cast(value); 82 | next::store_little(bytes + 1, value >> 8); 83 | } 84 | 85 | template 86 | static void store_little(Iterator& bytes, T value) 87 | { 88 | *bytes = static_cast(value); 89 | next::store_little(++bytes, value >> 8); 90 | } 91 | }; 92 | 93 | template 94 | struct unrolled_byte_loops 95 | { 96 | static T load_big(const unsigned char* bytes) 97 | { 98 | return *(bytes - 1); 99 | } 100 | 101 | template 102 | static T load_big(Iterator& bytes) 103 | { 104 | return *(bytes - 1); 105 | } 106 | 107 | static T load_little(const unsigned char* bytes) 108 | { 109 | return *bytes; 110 | } 111 | 112 | template 113 | static T load_little(Iterator& bytes) 114 | { 115 | return *bytes; 116 | } 117 | 118 | static void store_big(char* bytes, T value) 119 | { 120 | *(bytes - 1) = static_cast(value); 121 | } 122 | 123 | template 124 | static void store_big(Iterator& bytes, T value) 125 | { 126 | *(bytes - 1) = static_cast(value); 127 | } 128 | 129 | static void store_little(char* bytes, T value) 130 | { 131 | *bytes = static_cast(value); 132 | } 133 | 134 | template 135 | static void store_little(Iterator& bytes, T value) 136 | { 137 | // typename Iterator::value_type 138 | *bytes = static_cast(value); 139 | } 140 | }; 141 | 142 | template 143 | struct unrolled_byte_loops 144 | { 145 | static T load_big(const unsigned char* bytes) 146 | { 147 | return *reinterpret_cast(bytes - 1); 148 | } 149 | 150 | template 151 | static T load_big(Iterator& bytes) 152 | { 153 | return *reinterpret_cast(&*(bytes - 1)); 154 | } 155 | 156 | static T load_little(const unsigned char* bytes) 157 | { 158 | return *reinterpret_cast(bytes); 159 | } 160 | 161 | template 162 | static T load_little(Iterator& bytes) 163 | { 164 | return *reinterpret_cast(&*bytes); 165 | } 166 | 167 | static void store_big(char* bytes, T value) 168 | { 169 | *(bytes - 1) = static_cast(value); 170 | } 171 | 172 | template 173 | static void store_big(Iterator& bytes, T value) 174 | { 175 | BOOST_STATIC_ASSERT(boost::is_signed::value); 176 | 177 | *(bytes - 1) = static_cast(value); 178 | } 179 | 180 | static void store_little(char* bytes, T value) 181 | { 182 | *bytes = static_cast(value); 183 | } 184 | 185 | template 186 | static void store_little(Iterator& bytes, T value) 187 | { 188 | BOOST_STATIC_ASSERT(boost::is_signed::value); 189 | 190 | *bytes = static_cast(value); 191 | } 192 | }; 193 | 194 | template 195 | inline T load_big_endian(const void* bytes) 196 | { 197 | const unsigned char* ptr = static_cast(bytes); 198 | return unrolled_byte_loops::load_big(ptr + N); 199 | } 200 | 201 | template 202 | inline T load_big_endian(Iterator bytes) 203 | { 204 | std::advance(bytes, N); 205 | return unrolled_byte_loops::load_big(bytes); 206 | } 207 | 208 | template 209 | inline T load_little_endian(const void* bytes) 210 | { 211 | const unsigned char* ptr = static_cast(bytes); 212 | return unrolled_byte_loops::load_little(ptr); 213 | } 214 | 215 | template 216 | inline T load_little_endian(Iterator bytes) 217 | { 218 | return unrolled_byte_loops::load_little(bytes); 219 | } 220 | 221 | template 222 | inline void store_big_endian(void* bytes, T value) 223 | { 224 | char* ptr = static_cast(bytes); 225 | unrolled_byte_loops::store_big(ptr + N, value); 226 | } 227 | 228 | template 229 | inline void store_big_endian(Iterator bytes, T value) 230 | { 231 | std::advance(bytes, N); 232 | unrolled_byte_loops::store_big(bytes, value); 233 | } 234 | 235 | template 236 | inline void store_little_endian(void* bytes, T value) 237 | { 238 | char* ptr = static_cast(bytes); 239 | unrolled_byte_loops::store_little(ptr, value); 240 | } 241 | 242 | template 243 | inline void store_little_endian(Iterator bytes, T value) 244 | { 245 | unrolled_byte_loops::store_little(bytes, value); 246 | } 247 | 248 | } // namespace yatbinrw 249 | 250 | #endif // YATBINRW_HPP_INCLUDED 251 | -------------------------------------------------------------------------------- /yatbinrw/yatbinrw_example.cpp: -------------------------------------------------------------------------------- 1 | // Yet Another Tiny Binary Reader/Writer 2 | // 3 | // Simple test and usage example 4 | // 5 | // Copyright Mateusz Loskot 2009 6 | // Use, modification and distribution is subject to the Boost Software License, 7 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 8 | // http://www.boost.org/LICENSE_1_0.txt) 9 | // 10 | #include "yatbinrw.hpp" 11 | 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include 23 | #include 24 | 25 | #ifndef BOOST_LITTLE_ENDIAN 26 | #error yabinrw example runs on little-endian machines only - no bytes swapping provided 27 | #endif 28 | 29 | namespace { 30 | 31 | template 32 | void hex_to_bytes(std::string const& hex, T& bytes) 33 | { 34 | if (0 != hex.size() % 2) 35 | { 36 | throw std::runtime_error("invalid lenght of hex string"); 37 | } 38 | 39 | std::string::size_type const nchars = 2; 40 | std::string::size_type const nbytes = hex.size() / nchars; 41 | 42 | T(nbytes).swap(bytes); 43 | 44 | for (std::string::size_type i = 0; i < nbytes; ++i) 45 | { 46 | std::istringstream iss(hex.substr(i * nchars, nchars)); 47 | unsigned int n(0); 48 | if (!(iss >> std::hex >> n)) 49 | { 50 | throw std::runtime_error("hex to binary failed"); 51 | } 52 | bytes[i] = static_cast(n); 53 | } 54 | } 55 | 56 | template 57 | void bytes_to_hex(T const& bytes, std::string& hexstr) 58 | { 59 | typedef typename T::size_type size_type; 60 | size_type const nbytes = bytes.size(); 61 | 62 | const char hexchrs[] = "0123456789ABCDEF"; 63 | char hexbyte[3] = { 0 }; 64 | 65 | std::ostringstream oss; 66 | for (size_type i = 0; i < nbytes; ++i) 67 | { 68 | boost::uint8_t byte = static_cast(bytes[i]); 69 | hexbyte[0] = hexchrs[(byte >> 4) & 0xf]; 70 | hexbyte[1] = hexchrs[byte & 0xf]; 71 | hexbyte[2] = '\0'; 72 | oss << std::setw(2) << hexbyte; 73 | } 74 | hexstr = oss.str(); 75 | 76 | if (hexstr.size() != nbytes * 2) 77 | { 78 | throw std::runtime_error("binary to hex failed"); 79 | } 80 | } 81 | 82 | typedef std::vector byte_array; 83 | 84 | } // anonymous namespace 85 | 86 | int main() 87 | { 88 | try 89 | { 90 | std::cout << "load:\t"; 91 | { 92 | std::cout << "1, 12, 1.234, 5.678" << std::endl; 93 | 94 | boost::uint8_t n8(0); 95 | boost::uint32_t n32(0); 96 | boost::uint64_t n64(0); 97 | double ad64(0); 98 | double bd64(0); 99 | 100 | std::cout << "\tfrom little-endian:\t"; 101 | { 102 | std::string const hex_little("010C0000005839B4C876BEF33F83C0CAA145B61640"); 103 | std::cout << hex_little << std::endl; 104 | 105 | byte_array bytes; 106 | hex_to_bytes(hex_little, bytes); 107 | 108 | n8 = yatbinrw::load_little_endian(bytes.begin()); 109 | std::cout << "\t\tn8 = " << (int)n8 << std::endl; 110 | 111 | n32 = yatbinrw::load_little_endian(bytes.begin() + 1); 112 | std::cout << "\t\tn32 = " << n32 << std::endl; 113 | 114 | n64 = yatbinrw::load_little_endian(bytes.begin() + 1 + 4); 115 | std::memcpy(&ad64, &n64, sizeof(double)); 116 | std::cout << "\t\tad64 = " << ad64 << " (" << n64 << " )" << std::endl; 117 | 118 | n64 = yatbinrw::load_little_endian(bytes.begin() + 1 + 4 + 8); 119 | std::memcpy(&bd64, &n64, sizeof(double)); 120 | std::cout << "\t\tbd64 = " << bd64 << " (" << n64 << " )" << std::endl; 121 | } 122 | 123 | std::cout << "\tfrom big-endian:\t"; 124 | { 125 | std::string const hex_big("010000000C3FF3BE76C8B439584016B645A1CAC083"); 126 | std::cout << hex_big << std::endl; 127 | 128 | byte_array bytes; 129 | hex_to_bytes(hex_big, bytes); 130 | 131 | n8 = yatbinrw::load_big_endian(bytes.begin()); 132 | std::cout << "\t\tn8 = " << (int)n8 << std::endl; 133 | 134 | n32 = yatbinrw::load_big_endian(bytes.begin() + 1); 135 | std::cout << "\t\tn32 = " << n32 << std::endl; 136 | 137 | n64 = yatbinrw::load_big_endian(bytes.begin() + 1 + 4); 138 | std::memcpy(&ad64, &n64, sizeof(double)); 139 | std::cout << "\t\tad64 = " << ad64 << " (" << n64 << " )" << std::endl; 140 | 141 | n64 = yatbinrw::load_big_endian(bytes.begin() + 1 + 4 + 8); 142 | std::memcpy(&bd64, &n64, sizeof(double)); 143 | std::cout << "\t\tbd64 = " << bd64 << " (" << n64 << " )" << std::endl; 144 | } 145 | } // load 146 | 147 | std::cout << "store:\t"; 148 | { 149 | boost::uint8_t const n8(1); 150 | boost::uint32_t const n32(12); 151 | boost::uint64_t n64(0); 152 | double const ad64(1.234); 153 | double const bd64(5.678); 154 | 155 | std::cout << (int)n8 << ", " << n32 << ", " << ad64 << ", " << bd64 << std::endl; 156 | 157 | std::cout << "\tto little-endian:\t"; 158 | { 159 | byte_array bytes; //empty 160 | std::back_insert_iterator it(std::back_inserter(bytes)); 161 | 162 | yatbinrw::store_little_endian(it, n8); 163 | yatbinrw::store_little_endian(it, n32); 164 | 165 | std::memcpy(&n64, &ad64, sizeof(boost::uint64_t)); 166 | yatbinrw::store_little_endian(it, n64); 167 | 168 | std::memcpy(&n64, &bd64, sizeof(boost::uint64_t)); 169 | yatbinrw::store_little_endian(it, n64); 170 | 171 | std::string hex; 172 | bytes_to_hex(bytes, hex); 173 | std::cout << hex << std::endl; 174 | } 175 | 176 | std::cout << "\tto big-endian:"; 177 | { 178 | byte_array bytes(1 + 4 + 8 + 8); // non-empty 179 | 180 | yatbinrw::store_big_endian(bytes.begin(), n8); 181 | yatbinrw::store_big_endian(bytes.begin() + 1, n32); 182 | 183 | std::memcpy(&n64, &ad64, sizeof(boost::uint64_t)); 184 | yatbinrw::store_big_endian(bytes.begin() + 1 + 4, n64); 185 | 186 | std::memcpy(&n64, &bd64, sizeof(boost::uint64_t)); 187 | yatbinrw::store_big_endian(bytes.begin() + 1 + 4 + 8, n64); 188 | 189 | std::string hex; 190 | bytes_to_hex(bytes, hex); 191 | std::cout << '\t' << hex << std::endl; 192 | } 193 | 194 | } // store 195 | } 196 | catch (std::exception const& e) 197 | { 198 | std::cerr << e.what() << std::endl; 199 | } 200 | 201 | return 0; 202 | } 203 | --------------------------------------------------------------------------------