Skip to content

Instantly share code, notes, and snippets.

View rajkumar-p's full-sized avatar

Rajkumar rajkumar-p

View GitHub Profile
@rajkumar-p
rajkumar-p / directory_file.cpp
Last active August 28, 2019 18:52
DirectoryFile Implementation - Composite Pattern
DirectoryFile::DirectoryFile(const std::string &name, int permissions)
: File(name, permissions)
{
}
DirectoryFile::~DirectoryFile()
{
}
@rajkumar-p
rajkumar-p / regular_file.cpp
Last active August 28, 2019 18:53
RegularFile Implementation - Composite Pattern
RegularFile::RegularFile(const std::string &name, int permissions)
: File(name, permissions), _content("")
{
}
RegularFile::~RegularFile()
{
}
@rajkumar-p
rajkumar-p / directory_file.hpp
Last active August 28, 2019 18:54
DirectoryFile Interface - Composite Pattern
class DirectoryFile : public File {
public:
DirectoryFile(const std::string &name, int permissions);
~DirectoryFile();
int get_file_type() const;
std::string get_name() const;
size_t get_size() const;
int get_permissions() const;
@rajkumar-p
rajkumar-p / regular_file.hpp
Last active August 28, 2019 18:54
RegularFile Interface - Composite Pattern
class RegularFile : public File {
public:
RegularFile(const std::string &name, int permissions);
~RegularFile();
int get_file_type() const;
std::string get_name() const;
size_t get_size() const;
int get_permissions() const;
@rajkumar-p
rajkumar-p / file.hpp
Last active August 28, 2019 18:55
File Interface - Composite Pattern
enum ReadWriteError { Invalid = -1 };
enum FileType { Regular, Directory };
class File {
public:
File(const std::string &name, const int permissions);
virtual ~File();
virtual int get_file_type() const = 0;
@rajkumar-p
rajkumar-p / read_cont_blocks_raii.cpp
Created August 16, 2019 06:30
Read Contiguous Blocks using a RAII Wrapper
void read_cont_blocks(char *filename)
{
struct s1 one;
struct s2 two;
int three;
float four;
int num_read;
FileHandle file_handle(filename, O_RDONLY | O_CREAT, S_IWUSR | S_IRUSR);
@rajkumar-p
rajkumar-p / file_handle.cpp
Created August 16, 2019 06:21
RAII Wrapper for C file API
class FileHandle {
private:
int fd;
public:
FileHandle(const char *file_name, int flags, mode_t mode);
~FileHandle();
int read(void *buffer, size_t count);
int write(void *buffer, size_t count);
};
@rajkumar-p
rajkumar-p / write_cont_blocks.cpp
Last active August 12, 2019 13:22
Write Contiguous Blocks
void write_cont_blocks(char *filename)
{
struct iovec iov[4];
struct s1 one;
struct s2 two;
int three;
float four;
int num_written;
@rajkumar-p
rajkumar-p / read_cont_blocks.cpp
Last active August 12, 2019 13:21
Read Contiguous Blocks
void read_cont_blocks(char *filename)
{
struct iovec iov[4];
struct s1 one;
struct s2 two;
int three;
float four;
int num_read;
@rajkumar-p
rajkumar-p / read_cont_blocks_wrong.cpp
Last active August 12, 2019 13:20
Read Contiguous Blocks
void read_cont_blocks(char *filename)
{
struct iovec iov[4];
struct s1 one;
struct s2 two;
int three;
float four;
int num_read;