Skip to content

Commit

Permalink
update docs, use strings where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
heapwolf committed Mar 31, 2015
1 parent 8af5771 commit 2b487e8
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 79 deletions.
Binary file modified .fs.h.swp
Binary file not shown.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
build
util
deps
test/test
test/out.txt
./test/test
./test/out.txt
.DS_Store
*.sw*

62 changes: 35 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# SYNOPSIS
A friendly c++1y wrapper for libuv's filesystem operations
A friendly, nodejs-like file i/o experience for C++

# USAGE

Expand Down Expand Up @@ -33,27 +33,27 @@ Creates a loop for file operations.

## INSTANCE METHODS

### fs.cwd();
### `string` fs.cwd();
Returns a string that represents the current working directory.

### fs.readFile(path, callback);
### `string` fs.readFile(string path, Callback cb);
Stats, Opens, reads whole file, callback provides (`error`, `streambuf`).

### fs.readFileSync(path);
### `Buffer` fs.readFileSync(string path);
Stats, Opens, reads whole file and returns an instance of `Buffer`.

### fs.writeFile(path, buffer, callback);
### `void` fs.writeFile(string path, Buffer buf, Callback cb);
Opens, writes whole buffer, callback provides (`error`).

### fs.writeFileSync(path, buffer);
### `Buffer` fs.writeFileSync(string path, Buffer buf);
Opens, writes whole value from the instance of `Buffer`.

### fs.stat(path, callback);
### `void` fs.stat(string path, Callback cb);
Callback provides (`error`, `stats`), where stats is a struct containing
the following members...

### fs.statSync(path);
Returns a Stats object.
### `Stats` fs.statSync(string path);
Returns a Stats struct.

```cpp
uint64_t dev;
Expand All @@ -74,38 +74,46 @@ uv_timespec_t ctime;
uv_timespec_t birthtime;
```

### fs.mkdirSync(path, [mode]);
Create a directory sync, optionally pass the mode as an octal.
### `Error` fs.mkdirSync(string path, [int mode]);
Create a directory sync, optionally pass the mode as an octal. Returns
an error instance that will have the value of null if the operation was
successful.

### fs.rmdirSync(path);
Remove a direcory sync.
### `Error` fs.rmdirSync(string path);
Remove a direcory sync. Returns an error object that has the value of
null if the operation was a success.

### fs.open(path, callback);
Callback provides (`error`, `fd`) where fd is a file descriptor.
### `void` fs.open(string path, Callback cb);
The callback provides two arguments `Error` which will be null if the
operation was a success and a file descriptor.

### fs.openSync(path, [mode]);
Open a file sync, optionally pass the mode as an octal.
### `int` fs.openSync(string path, [int mode]);
Open a file sync, optionally pass the mode as an octal. Returns the
file descriptor.

### fs.read(fd, bufferSize, offset, callback);
### `void` fs.read(int fd, int bufferSize, int offset, Callback cb);
Callback provides (`error`, `uv_buf_t`).

### fs.readSync(fd, bufferSize, offset);
Callback provides (`error`, `uv_buf_t`).
### `Buffer` fs.readSync(int fd, int bufferSize, int offset);
Returns an instance of `Buffer`.

### fs.write(fd, buffer, offset, callback);
### `void` fs.write(int fd, Buffer buf, int offset, Callback cb);
Callback provides (`error`, `uv_buf_t`).

### fs.writeSync(fd, buffer, offset, length);
Write an instance of `Buffer` to a file, returns the number of bytes written.
### `int` fs.writeSync(int fd, Buffer buf, int offset, int length);
Write an instance of `Buffer` to a file, returns the number of bytes
written.

### fs.close(fd, callback);
Close a file. Callback provides (`error`).
### `void` fs.close(fd, callback);
Close a file. Callback provides an instance of `Error` which is null
if the operation was a success.

### fs.closeSync(fd);
### `Error` fs.closeSync(fd);
Close a file sync.

# Buffers
A little sugar on top of `uv_buf_t`.
A little sugar on top of `uv_buf_t`. This should get moved out to
another module called `nodeuv-buffer`.

## CONSTRUCTOR
### Buffer buf(string);
Expand Down
42 changes: 21 additions & 21 deletions fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ extern "C" {
#include "uv.h"
}

namespace fs {
namespace nodeuv {

using namespace std;

class Readable;
class Writable;
class Error;

// https://github.com/joyent/node/blob/master/lib/fs.js#L256
// https://github.com/iojs/io.js/blob/master/lib/fs.js#L256
const unsigned int MAGIC_BUFFER_SIZE = 8192;

struct Stats {
Expand Down Expand Up @@ -115,16 +115,16 @@ namespace fs {
string cwd();
uv_buf_t createBuffer(string s);

void stat(const char*, Callback<Error, Stats>);
Stats statSync(const char* path);
void stat(string, Callback<Error, Stats>);
Stats statSync(string);

Error mkdirSync(const char* path);
Error mkdirSync(const char* path, int mode);
Error mkdirSync(string);
Error mkdirSync(string, int);

Error rmdirSync(const char* path);
Error rmdirSync(string);

void open(const char*, int, int, Callback<Error, uv_file>);
int openSync(const char*, int, int);
void open(string, int, int, Callback<Error, uv_file>);
int openSync(string, int, int);

void read(uv_file, int64_t, int64_t, Callback<Error, uv_buf_t>);
int readSync(uv_file fd, uv_buf_t* buffer, int64_t offset, int64_t bytes);
Expand All @@ -138,18 +138,18 @@ namespace fs {
//
// Higher level functions
//
void readFile(const char*, Callback<Error, string>);
void readFile(const char*, ReadOptions, Callback<Error, string>);

Buffer readFileSync(const char*, ReadOptions);
Buffer readFileSync(const char*);

void writeFile(const char*, string, Callback<Error>);
void writeFile(const char*, string, WriteOptions, Callback<Error>);
void writeFile(const char*, uv_buf_t, Callback<Error>);
void writeFile(const char*, uv_buf_t, WriteOptions, Callback<Error>);
int writeFileSync(const char* path, Buffer buffer);
int writeFileSync(const char* path, Buffer buffer, WriteOptions opts);
void readFile(string, Callback<Error, string>);
void readFile(string, ReadOptions, Callback<Error, string>);

Buffer readFileSync(string, ReadOptions);
Buffer readFileSync(string);

void writeFile(string, string, Callback<Error>);
void writeFile(string, string, WriteOptions, Callback<Error>);
void writeFile(string, uv_buf_t, Callback<Error>);
void writeFile(string, uv_buf_t, WriteOptions, Callback<Error>);
int writeFileSync(string, Buffer);
int writeFileSync(string, Buffer, WriteOptions);

Filesystem() {
UV_LOOP = uv_default_loop();
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "nodeuv-fs",
"version": "1.0.0",
"version": "2.0.0",
"repo": "hij1nx/nodeuv-fs",
"description": "A simple c++11 style wrapper for libuv's file system calls.",
"description": "A friendly, nodejs-like file i/o experience for C++",
"keywords": ["fs", "libuv", "streams"],
"src": ["fs.h"],
"install": "make"
Expand Down
72 changes: 46 additions & 26 deletions src/fs.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "../fs.h"

namespace fs {
namespace nodeuv {

Buffer& Buffer::operator= (const Buffer &buf) {
data = buf.data;
Expand Down Expand Up @@ -142,7 +142,9 @@ namespace fs {
//
//
//
void Filesystem::open(const char* path, int flags, int mode, Callback<Error, uv_file> cb) {
void Filesystem::open(string p, int flags, int mode, Callback<Error, uv_file> cb) {

const char* path = p.c_str();

uv_fs_t open_req;
static function<void(uv_fs_t* req)> on_open;
Expand Down Expand Up @@ -172,7 +174,9 @@ namespace fs {
//
//
//
int Filesystem::openSync(const char* path, int flags, int mode) {
int Filesystem::openSync(string p, int flags, int mode) {

const char* path = p.c_str();

uv_fs_t open_req;
int result = uv_fs_open(UV_LOOP, &open_req, path, flags, mode, NULL);
Expand Down Expand Up @@ -268,7 +272,9 @@ namespace fs {
//
//
//
Stats Filesystem::statSync(const char* path) {
Stats Filesystem::statSync(string p) {

const char * path = p.c_str();

uv_fs_t stat_req;
int r = uv_fs_stat(UV_LOOP, &stat_req, path, NULL);
Expand All @@ -291,7 +297,9 @@ namespace fs {
//
//
//
void Filesystem::stat(const char* path, Callback<Error, Stats> cb) {
void Filesystem::stat(string p, Callback<Error, Stats> cb) {

const char* path = p.c_str();

uv_fs_t stat_req;
static function<void(uv_fs_t* req)> on_stat;
Expand Down Expand Up @@ -322,12 +330,14 @@ namespace fs {
}


Error Filesystem::mkdirSync(const char* path) {
return mkdirSync(path, 0777);
Error Filesystem::mkdirSync(string p) {
return mkdirSync(p, 0777);
}


Error Filesystem::mkdirSync(const char* path, int mode) {
Error Filesystem::mkdirSync(string p, int mode) {

const char* path = p.c_str();

uv_fs_t mkdir_req;
int result = uv_fs_mkdir(UV_LOOP, &mkdir_req, path, mode, NULL);
Expand All @@ -347,7 +357,9 @@ namespace fs {
}


Error Filesystem::rmdirSync(const char* path) {
Error Filesystem::rmdirSync(string p) {

const char* path = p.c_str();

uv_fs_t rmdir_req;
int result = uv_fs_rmdir(UV_LOOP, &rmdir_req, path, NULL);
Expand All @@ -370,15 +382,18 @@ namespace fs {
//
//
//
Buffer Filesystem::readFileSync(const char* path) {
Buffer Filesystem::readFileSync(string p) {
ReadOptions opts;
return readFileSync(path, opts);
return readFileSync(p, opts);
}

//
//
//
Buffer Filesystem::readFileSync(const char* path, ReadOptions opts) {
Buffer Filesystem::readFileSync(string p, ReadOptions opts) {

const char* path = p.c_str();

Stats st = statSync(path);
int fd = openSync(path, opts.flags, opts.mode);
int size = st.size;
Expand All @@ -396,7 +411,9 @@ namespace fs {
// TODO
// this is lame. should be buffers.
//
void Filesystem::readFile(const char* path, ReadOptions opts, Callback<Error, string> cb) {
void Filesystem::readFile(string p, ReadOptions opts, Callback<Error, string> cb) {

const char* path = p.c_str();

stat(path, [&](auto err, auto stats) {

Expand Down Expand Up @@ -453,15 +470,17 @@ namespace fs {
//
//
//
void Filesystem::readFile(const char* path, Callback<Error, string> cb) {
void Filesystem::readFile(string p, Callback<Error, string> cb) {
ReadOptions opts;
readFile(path, opts, cb);
readFile(p, opts, cb);
}

//
//
//
void Filesystem::writeFile(const char* path, uv_buf_t buffer, WriteOptions opts, Callback<Error> cb) {
void Filesystem::writeFile(string p, uv_buf_t buffer, WriteOptions opts, Callback<Error> cb) {

const char* path = p.c_str();

open(path, opts.flags, opts.mode, [&](auto err, auto fd) {

Expand All @@ -487,17 +506,18 @@ namespace fs {
//
//
//
int Filesystem::writeFileSync(const char* path, Buffer buffer) {
int Filesystem::writeFileSync(string p, Buffer buffer) {
WriteOptions opts;
return writeFileSync(path, buffer, opts);
return writeFileSync(p, buffer, opts);
}


//
//
//
int Filesystem::writeFileSync(const char* path, Buffer buffer, WriteOptions opts) {
int fd = openSync(path, opts.flags, opts.mode);
int Filesystem::writeFileSync(string p, Buffer buffer, WriteOptions opts) {

int fd = openSync(p, opts.flags, opts.mode);
int bytesWritten = writeSync(fd, buffer, 0, buffer.length());
closeSync(fd);
return bytesWritten;
Expand All @@ -507,30 +527,30 @@ namespace fs {
//
//
//
void Filesystem::writeFile(const char* path, uv_buf_t buffer, Callback<Error> cb) {
void Filesystem::writeFile(string p, uv_buf_t buffer, Callback<Error> cb) {
WriteOptions opts;
writeFile(path, buffer, opts, cb);
writeFile(p, buffer, opts, cb);
}

//
//
//
void Filesystem::writeFile(const char* path, string s, Callback<Error> cb) {
void Filesystem::writeFile(string p, string s, Callback<Error> cb) {
WriteOptions opts;
writeFile(path, s, opts, cb);
writeFile(p, s, opts, cb);
}

//
//
//
void Filesystem::writeFile(const char* path, string s, WriteOptions opts, Callback<Error> cb) {
void Filesystem::writeFile(string p, string s, WriteOptions opts, Callback<Error> cb) {

char b[s.size()];
strcpy(b, s.c_str());
uv_buf_t buffer;
buffer = uv_buf_init(b, sizeof(b));

writeFile(path, buffer, opts, cb);
writeFile(p, buffer, opts, cb);
}

} // namespace fs
Expand Down

0 comments on commit 2b487e8

Please sign in to comment.