diff options
Diffstat (limited to 'src/file.cc')
-rw-r--r-- | src/file.cc | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/src/file.cc b/src/file.cc index 50ed3f9..9188e6e 100644 --- a/src/file.cc +++ b/src/file.cc @@ -33,6 +33,9 @@ #include <string.h> #include <unistd.h> +// For ntoh* +#include <netinet/in.h> + #include <stdlib.h> File::File(char *fn, char* ext, Info *i) @@ -124,6 +127,99 @@ int File::Write(void* data, int size) return w; } +int File::Write(char* data, int size) +{ + return Write((void*)data, size); +} + +int File::Write(unsigned long long int val) +{ + int res; + int written = 0; + unsigned long int *h_u = (unsigned long int *)&val; + unsigned long int *h_l = (unsigned long int *)(((char*)&val) + sizeof(unsigned long int)); + + *h_u = htonl(*h_u); + *h_l = htonl(*h_l); + + if((res = Write((void*)h_l, sizeof(*h_l))) < 0) { + return res; + } + written += res; + + if((res = Write((void*)h_u, sizeof(*h_u))) < 0) { + return res; + } + written += res; + + return written; +} + +int File::Write(long long int val) +{ + int res; + int written = 0; + unsigned long int *h_u = (unsigned long int *)&val; + unsigned long int *h_l = (unsigned long int *)(((char*)&val) + sizeof(unsigned long int)); + + *h_u = htonl(*h_u); + *h_l = htonl(*h_l); + + if((res = Write((void*)h_l, sizeof(*h_l))) < 0) { + return res; + } + written += res; + + if((res = Write((void*)h_u, sizeof(*h_u))) < 0) { + return res; + } + written += res; + + return written; +} + +int File::Write(long int val) +{ + val = htonl(val); + + return Write((char*)&val, sizeof(val)); +} + +int File::Write(unsigned long int val) +{ + val = htonl(val); + + return Write((char*)&val, sizeof(val)); +} + +int File::Write(int val) +{ + val = htonl(val); + + return Write((char*)&val, sizeof(val)); +} + +int File::Write(unsigned int val) +{ + val = htonl(val); + + return Write((char*)&val, sizeof(val)); +} + +int File::Write(short int val) +{ + val = htons(val); + + return Write((char*)&val, sizeof(val)); +} + +int File::Write(unsigned short int val) +{ + val = htons(val); + + return Write((char*)&val, sizeof(val)); +} + int File::createPath(char* path) { // struct stat stats; @@ -148,3 +244,23 @@ int File::createPath(char* path) return 0; } + +#ifdef __TEST_FILE +#include "info_simple.h" + +int main(int argc, char *argv[]) { + if(argc < 3) { + fprintf(stderr, "usage:\n\ttest_file [filename] [extension]\n"); + return 1; + } + + + InfoSimple info; + File file(argv[1], argv[2], &info); + + unsigned int val = 0x01234567; + file.Write(val); + +} + +#endif/* __TEST_FILE*/ |