Skip to content

Commit

Permalink
Added files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
SaraEl-Metwally committed Feb 24, 2016
1 parent 1e89768 commit 081038f
Show file tree
Hide file tree
Showing 23 changed files with 4,496 additions and 0 deletions.
59 changes: 59 additions & 0 deletions BinaryStore.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "BinaryStore.hpp"

binary_store::binary_store(const std::string file_name,int given_element_size, bool flag):element_size(given_element_size)
{
open(file_name,flag);
}
void binary_store::open(const std::string file_name,bool flag)
{
const char * f_name = file_name.c_str();
file_ptr = fopen(f_name,flag?"wb":"rb");
if( file_ptr == NULL )
{
std::cerr << "error during fopen" << std::endl;
exit(1);
}
}
void binary_store::write_element(void *element)
{

if (!fwrite(element, element_size, 1, file_ptr))
{
std::cerr <<"error: can't fwrite (disk full?)" << std::endl;
exit(1);
}

}
size_t binary_store::read_element( void *element)
{
return fread(element, element_size,1, file_ptr);
}

size_t binary_store::read_element_buffer(void *element,size_t nb_elements)
{
return fread(element, element_size,nb_elements, file_ptr);
}

void binary_store::write( void *element, int given_element_size)
{
if (!fwrite(element, given_element_size, 1, file_ptr))
{
std::cerr <<"error: can't fwrite (disk full?)" << std::endl;
exit(1);
}
}

size_t binary_store::read( void *element, int given_element_size)
{
return fread(element, given_element_size,1, file_ptr);
}

void binary_store::rewind_all()
{
rewind(file_ptr);
}

void binary_store::close()
{
fclose(file_ptr);
}
26 changes: 26 additions & 0 deletions BinaryStore.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#ifndef BINARYSTORE_H
#define BINARYSTORE_H
#include <string>
#include <stdio.h>
#include <iostream>
#include <algorithm>

class binary_store
{
public:
binary_store(const std::string file_name,int given_element_size, bool flag);
void write_element(void *element);
size_t read_element(void *element);
void write( void *element, int given_element_size);
size_t read( void *element, int given_element_size);
size_t read_element_buffer(void *element,size_t nb_elements);
void rewind_all();
void close();
void open(const std::string file_name,bool write);
private:
FILE * file_ptr;
const int element_size;
};


#endif // BINARYSTORE_H
Loading

0 comments on commit 081038f

Please sign in to comment.