Coder Social home page Coder Social logo

libpmmf's Introduction

Portable Memory-Mapped File library

libpmmf - Portable Memory-Mapped File library for C++

Build:

Build pmmf library for Linux (required GCC, CMake, Make):

git clone [email protected]:gbytegear/libpmmf.git
cd ./libpmmf/build
cmake . -G "Unix Makefiles" . -DCMAKE_BUILD_TYPE=Release
make pmmf

Result - libpmmf/build/libpmmf.a

Build pmmf library for Windows (required MSys2 step 1-5, MinGW step 6-7, CMake, Make):

git clone [email protected]:gbytegear/libpmmf.git
cd ./libpmmf/build
cmake . -G "MSYS Makefiles" . -DCMAKE_BUILD_TYPE=Release
make pmmf

Result - libpmmf/build/libpmmf.a

Usage:

Link with your project:

g++ <your_project_source_file> -L<path_to_libpmmf.a> -I<path_to_libpmmf/include> -lpmmf

Simple programm example:

#include "include/mmf.hxx"
#include <iostream>

#pragma pack(push, 1)

struct Data {
  char cstr[50];
};

#pragma pack(pop)

auto main(int argc, char* argv[]) -> int {

  using namespace pmmf;

  if (argc < 2) {
    std::cerr << "Required 1 file paths!" << std::endl;
    return -1;
  }

  MappedFile mapped_file(argv[1], ProtectionMode::rw, MapFlag::shared);
  if(!mapped_file.isFileOpen()) {
    std::cerr << "Can't open \"" << argv[1] << "\" file" << std::endl;
    return -1;
  }
  std::cout << "File \"" << argv[1] << "\" is open" << std::endl
            << "| File size: " << mapped_file.getFileSize() << " bytes" << std::endl;



  MappedData<Data> second_mapped_array = mapped_file.getMappedData<Data>(sizeof(Data), 2);
  if(!second_mapped_array.isMapped()) {
    std::cerr << "Can't map second area of \"" << argv[1] << "\" file" << std::endl;
    return -1;
  }
  std::cout << "Second area of \"" << argv[1] << "\" file is mapped" << std::endl;



  MappedData<Data> first_mapped_array = mapped_file.getMappedData<Data>(0);
  if(!second_mapped_array.isMapped()) {
    std::cerr << "Can't map first area of \"" << argv[1] << "\" file" << std::endl;
    return -1;
  }
  std::cout << "First area of \"" << argv[1] << "\" file is mapped" << std::endl;



  Data& page_1_data_1 = *first_mapped_array;
  Data& page_2_data_1 = *second_mapped_array;
  Data& page_2_data_2 = second_mapped_array[1];



  new(&page_1_data_1) Data{"Lorem ipsum dolor sit amet, consectetur efficitur"};
  if(!first_mapped_array.flush()) {
    std::cerr << "Can't flush first area of \"" << argv[1] << "\" file" << std::endl;
    return -1;
  }
  std::cout << "First area of \"" << argv[1] << "\" file is flushed" << std::endl
            << "| Recorded data: " << first_mapped_array->cstr << std::endl;



  new(&page_2_data_1) Data{"Lorem ipsum dolor sit amet, consectetur tincidunt"};
  new(&page_2_data_2) Data{"Lorem ipsum dolor sit amet, consectetur porttitor"};
  if(!second_mapped_array.flush()) {
    std::cerr << "Can't flush second area of \"" << argv[1] << "\" file" << std::endl;
    return -1;
  }
  std::cout << "Second area of \"" << argv[1] << "\" file is flushed" << std::endl;


  for(auto& data : second_mapped_array)
    std::cout << "| Recorded data: " << data.cstr << std::endl;



  std::cout << "File size: " << mapped_file.getFileSize() << " bytes" << std::endl;

  return 0;
}

libpmmf's People

Contributors

gbytegear avatar

Watchers

 avatar

Forkers

webstorage119

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.