Skip to content

Commit

Permalink
fixed README and added decompress exe
Browse files Browse the repository at this point in the history
  • Loading branch information
sstirlin committed Jan 19, 2015
1 parent a2e78f7 commit 55746c6
Show file tree
Hide file tree
Showing 6 changed files with 407 additions and 38 deletions.
39 changes: 38 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ message (STATUS "CBLOSC SHARED LIB: ${AGS_EXAMPLE_CPP_LIB_LIBRARIES}")
set (DLLS_to_install ${DLLS_to_install} ${AGS_EXAMPLE_CPP_LIB_DLLS})


# find c-blosc (set hint CBLOSC_ROOT)
find_package(CBLOSC REQUIRED)
include_directories(${CBLOSC_INCLUDE_DIR})
message (STATUS "CBLOSC DLLS: ${CBLOSC_DLLS}")
message (STATUS "CBLOSC SHARED LIB: ${CBLOSC_LIBRARIES}")
set (DLLS_to_install ${DLLS_to_install} ${CBLOSC_DLLS})


# find Boost (set hint BOOST_ROOT)
set (Boost_USE_STATIC_LIBS OFF)
set (Boost_USE_STATIC_RUNTIME OFF) # this should always be set to OFF
Expand Down Expand Up @@ -74,10 +82,39 @@ set (WORKING_SOURCES ${WORKING_HPP_SOURCES} ${WORKING_H_SOURCES} ${WORKING_CP

# create the target
add_executable (ags_blosc_compress ${WORKING_SOURCES})
target_link_libraries (ags_blosc_compress ${AGS_EXAMPLE_CPP_LIB_LIBRARIES}
target_link_libraries (ags_blosc_compress ${AGS_EXAMPLE_CPP_LIB_LIBRARIES} ${CBLOSC_LIBRARIES}
${Boost_LIBRARIES} )

# install the target
install (TARGETS ags_blosc_compress
DESTINATION bin
PERMISSIONS OWNER_EXECUTE OWNER_READ OWNER_WRITE)


###########################
#
# ags_blosc_decompress target
# THIS IS AN EXECUTABLE TARGET
#
###########################

# add includes for current target
set (WORKING_SOURCE_DIR ${CMAKE_SOURCE_DIR}/ags_blosc_decompress)
include_directories(${WORKING_SOURCE_DIR}/inc)

# add .cpp, .c, .hpp, .h files
file (GLOB_RECURSE WORKING_HPP_SOURCES ${WORKING_SOURCE_DIR}/inc/*.hpp)
file (GLOB_RECURSE WORKING_H_SOURCES ${WORKING_SOURCE_DIR}/inc/*.h)
file (GLOB_RECURSE WORKING_CPP_SOURCES ${WORKING_SOURCE_DIR}/src/*.cpp)
file (GLOB_RECURSE WORKING_C_SOURCES ${WORKING_SOURCE_DIR}/src/*.c)
set (WORKING_SOURCES ${WORKING_HPP_SOURCES} ${WORKING_H_SOURCES} ${WORKING_CPP_SOURCES} ${WORKING_C_SOURCES})

# create the target
add_executable (ags_blosc_decompress ${WORKING_SOURCES})
target_link_libraries (ags_blosc_decompress ${AGS_EXAMPLE_CPP_LIB_LIBRARIES} ${CBLOSC_LIBRARIES}
${Boost_LIBRARIES} )

# install the target
install (TARGETS ags_blosc_decompress
DESTINATION bin
PERMISSIONS OWNER_EXECUTE OWNER_READ OWNER_WRITE)
48 changes: 15 additions & 33 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,15 @@ Introduction
This repo is part of the tutorial found in
https://github.com/ActivisionGameScience/ags_conda_recipes.git

It contains a single C++ library, ``ags_blosc_wrapper``, that
is a dumb wrapper around the popular ``c-blosc`` compression
library (https://github.com/Blosc/c-blosc).
It contains two C++ executables, ``ags_blosc_compress`` and
``ags_blosc_decompress``, that link against the ``ags_blosc_wrapper`` library
found in the repo https://github.com/ActivisionGameScience/ags_example_cpp_lib.git.

The purpose is to demonstrate several techniques:
The purpose is to demonstrate:

- How to manage external dependencies using ``conda``,
- How to manage both in-house and third-party dependencies using ``conda``,
and build using ``cmake``

- How to build both static and dynamic binaries

- How to expose both a C++ and C API using
the *opaque pointer* technique

The purpose of the C API (which is a wrapper around
the C++ code) is to allow other languages to bind
to this library easily.

In turn, this project serves as a dependency for
two other projects described in the tutorial:

- https://github.com/ActivisionGameScience/ags_example_py_wrapper.git
(a python wrapper around this library)

- https://github.com/ActivisionGameScience/ags_example_cpp_app.git
(a C++ application that builds against this library)


How to build
============
Expand All @@ -41,26 +23,26 @@ You can use it to build, publish, and install
the ``conda`` way.

However, you can also build and install this library by hand.
Assuming that ``c-blosc`` is installed in the following location::
Assume that ``ags_example_cpp_lib`` is installed in the following location::

/some/path/include/blosc.h
/some/path/lib/libblosc.so
/some/path/include/activision_game_science/*.h
/some/path/lib/libags_blosc_wrapper.so

you can build and install with the following commands::
Assume that ``boost`` and ``c-blosc`` are similarly installed in ``/some/path``.
Then you can build and install with the following commands::

git clone https://github.com/ActivisionGameScience/ags_example_cpp_lib.git
cd ags_example_cpp_lib
git clone https://github.com/ActivisionGameScience/ags_example_cpp_app.git
cd ags_example_cpp_app
mkdir build
cd build
cmake -G "Unix Makefiles" ../ -DCBLOSC_ROOT=/some/path -DCMAKE_INSTALL_PREFIX=/some/path
cmake -G "Unix Makefiles" ../ -DBOOST_ROOT=/some/path -DAGS_EXAMPLE_CPP_LIB_ROOT=/some/path -DCMAKE_INSTALL_PREFIX=/some/path
make
make install

You will now have the following files installed::

/some/path/include/activision_game_science/*.h
/some/path/lib/libags_blosc_wrapper.so
/some/path/lib/libags_blosc_wrapper_static.a
/some/path/bin/ags_blosc_compress
/some/path/bin/ags_blosc_decompress


License
Expand Down
58 changes: 55 additions & 3 deletions ags_blosc_compress/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#include <activision_game_science/ags_blosc_wrapper.h>
#include <activision_game_science/blosc_wrapper.h>

#include <string>
#include <iostream>
#include <fstream>

#include <boost/filesystem.hpp>


using namespace std;


// for storing command-line options
struct ProgOpts
Expand Down Expand Up @@ -116,7 +120,7 @@ int main(int argc, char * argv[])
{
std::cout << "Usage: " << argv[0] << std::endl;
std::cout << std::endl;
std::cout << " -h Displays this message" << std::endl << std::endl;
std::cout << " -h Displays this message" << std::endl;
std::cout << " -i Input file" << std::endl;
std::cout << " -o Output file" << std::endl;
if(good_opts)
Expand All @@ -130,7 +134,55 @@ int main(int argc, char * argv[])
// Work starts here
//*****************

std::cout << opts.infile << " " << opts.outfile << std::endl;
// read file in
size_t srcsize;
std::vector<char> src;

ifstream ifile(opts.infile.c_str(), ios::in|ios::binary|ios::ate);
if (ifile.is_open()) {

srcsize = ifile.tellg(); // this works because ios::ate starts at end of file
ifile.seekg (0, ios::beg);

src.resize(srcsize);
ifile.read(&src[0], srcsize); // should do better error handling here
ifile.close();

} else {

std::cout << "Cannot read infile " << opts.infile << std::endl;
return -1;
}
std::cout << "Read in " << srcsize << " bytes" << std::endl;


// get BloscWrapper object
activision_game_science::BloscWrapper b = activision_game_science::BloscWrapper();


// allocate enough memory for compressed buffer
size_t dstsize = b.reserveNeededToCompress(srcsize);
std::cout << "Reserving " << dstsize << " bytes" << std::endl;
std::vector<char> dst = std::vector<char>(dstsize);


// compress and resize
dstsize = b.compress(&src[0], srcsize, &dst[0], dstsize);
dst.resize(dstsize);
std::cout << "Compressed to " << dstsize << " bytes" << std::endl;


// write compressed stream out
ofstream ofile(opts.outfile.c_str(), ios::out|ios::binary|ios::trunc);
if (ofile.is_open()) {

ofile.write(&dst[0], dstsize); // should do better error handling here
ofile.close();
} else {

std::cout << "Cannot open file " << opts.infile << " for writing" << std::endl;
return -1;
}

return 0;
}
Loading

0 comments on commit 55746c6

Please sign in to comment.