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 2b487e8 commit 37d5403
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 11 deletions.
Binary file modified .fs.h.swp
Binary file not shown.
41 changes: 39 additions & 2 deletions fs.h
Expand Up @@ -59,6 +59,9 @@ namespace nodeuv {
// TODO
// move out into another lib.
//

#define MIN(a, b) ((a) < (b) ? (a) : (b))

class Buffer {

public:
Expand All @@ -75,6 +78,40 @@ namespace nodeuv {
return data.len;
}

int copy(Buffer b) {
return this->_copy(b, 0, b.length(), 0);
}

int copy(Buffer b, int target_start) {
return this->_copy(b, target_start, b.length(), 0);
}

int copy(Buffer b, int target_start, int source_start) {
return this->_copy(b, target_start, source_start, 0);
}

int _copy(Buffer b, int target_start, int source_start, int source_end) {

size_t obj_length = b.length();
size_t target_length = this->length();
char* target_data = this->data.base;

if (target_start >= target_length || source_start >= source_end) {
return 0;
}

if (source_start > obj_length) {
throw runtime_error("out of range index");
}

uint32_t to_copy = MIN(MIN(source_end - source_start,
target_length - target_start),
obj_length - source_start);

memmove(this->data + target_start, b->data + source_start, to_copy);
return to_copy;
}

Buffer(int size) {
data.base = (char *) malloc(size);
data.len = size;
Expand Down Expand Up @@ -138,8 +175,8 @@ namespace nodeuv {
//
// Higher level functions
//
void readFile(string, Callback<Error, string>);
void readFile(string, ReadOptions, Callback<Error, string>);
void readFile(string, Callback<Error, Buffer>);
void readFile(string, ReadOptions, Callback<Error, Buffer>);

Buffer readFileSync(string, ReadOptions);
Buffer readFileSync(string);
Expand Down
24 changes: 15 additions & 9 deletions src/fs.cc
Expand Up @@ -405,13 +405,7 @@ namespace nodeuv {
return buf;
}



//
// TODO
// this is lame. should be buffers.
//
void Filesystem::readFile(string p, ReadOptions opts, Callback<Error, string> cb) {
void Filesystem::readFile(string p, ReadOptions opts, Callback<Error, Buffer> cb) {

const char* path = p.c_str();

Expand All @@ -432,7 +426,8 @@ namespace nodeuv {
}

int64_t offset = 0;
stringstream ss;
vector<Buffer> bigBuffer;
//stringstream ss;
static function<void()> reader;

reader = [&]() {
Expand All @@ -447,14 +442,25 @@ namespace nodeuv {

read(fd, size, offset, [&](auto err, auto buf) {
offset = offset + buf.len;
ss << string(buf.base);
//ss << string(buf.base);
bigBuffer.push_back(Buffer(buf.base));
free(buf.base);
reader();
});
return;
}
else {
close(fd, [&](auto err) {

int newbuff_size = 0;

for (auto b& : bigBuffer) {
newbuff_size += b.length();
}

Buffer buf(newbuff_size);
// now copy shit in

cb(err, ss.str().substr(0, size));
});
}
Expand Down

0 comments on commit 37d5403

Please sign in to comment.