[Assorted-commits] SF.net SVN: assorted:[1348] cpp-commons/trunk/src/commons/files.h
Brought to you by:
yangzhang
From: <yan...@us...> - 2009-04-29 02:08:37
|
Revision: 1348 http://assorted.svn.sourceforge.net/assorted/?rev=1348&view=rev Author: yangzhang Date: 2009-04-29 02:08:29 +0000 (Wed, 29 Apr 2009) Log Message: ----------- added file_size, read_file_as_array; tweaks to use other commons abstractions Modified Paths: -------------- cpp-commons/trunk/src/commons/files.h Modified: cpp-commons/trunk/src/commons/files.h =================================================================== --- cpp-commons/trunk/src/commons/files.h 2009-04-29 02:07:36 UTC (rev 1347) +++ cpp-commons/trunk/src/commons/files.h 2009-04-29 02:08:29 UTC (rev 1348) @@ -13,6 +13,7 @@ #include <fcntl.h> #include <commons/check.h> +#include <commons/closing.h> namespace commons { @@ -45,6 +46,28 @@ out = vector<char> ( istreambuf_iterator<char> ( in ), istreambuf_iterator<char>() ); } + off_t file_size(const char *path) + { + struct stat s; + check0x(stat(path, &s)); + return s.st_size; + } + + off_t file_size(int fd) + { + struct stat s; + check0x(fstat(fd, &s)); + return s.st_size; + } + + array<char> read_file_as_array(const char *path) + { + closingfd fd(checknnegerr(open(path, O_RDONLY))); + array<char> buf(file_size(fd)); + checkeqnneg(read(fd, buf, buf.size()), static_cast<ssize_t>(buf.size())); + return buf; + } + /** * Load an entire file directly into buf and also give us the length of the * buffer (size of the file). @@ -55,7 +78,7 @@ load_file(const char *path, size_t & len, unsigned int ncpus) { struct stat sb; - int fd = checkpass(open(path, 0)); + closingfd fd(checknnegerr(open(path, O_RDONLY))); check0x(fstat(fd, &sb)); check(sb.st_size <= 0xffffffff); @@ -78,8 +101,6 @@ } } - check0x(close(fd)); - buf[len] = '\0'; // don't let strcmp() run off the end return buf; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |