00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00031
00032 #ifdef HAVE_CONFIG_H
00033 #include "autoconfig.h"
00034 #endif
00035
00036 #include "server.h"
00037 #include <sys/types.h>
00038 #include <sys/stat.h>
00039 #include <unistd.h>
00040 #include <string.h>
00041 #include <stdio.h>
00042 #include "common.h"
00043 #include "storage.h"
00044 #include "cds_objects.h"
00045 #include "process.h"
00046 #include "update_manager.h"
00047 #include "ixml.h"
00048 #include "file_io_handler.h"
00049 #include "dictionary.h"
00050
00051 using namespace zmm;
00052 using namespace mxml;
00053
00054 FileIOHandler::FileIOHandler(String filename) : IOHandler()
00055 {
00056 this->filename = filename;
00057 }
00058
00059 void FileIOHandler::open(IN enum UpnpOpenFileMode mode)
00060 {
00061 if (mode == UPNP_READ)
00062 {
00063 f = fopen(filename.c_str(), "rb");
00064 }
00065 else if (mode == UPNP_WRITE)
00066 {
00067 f = fopen(filename.c_str(), "wb");
00068 }
00069 else
00070 {
00071 throw _Exception(_("FileIOHandler::open: invdalid read/write mode"));
00072 }
00073
00074 if (f == NULL)
00075 {
00076 throw _Exception(_("FileIOHandler::open: failed to open: ") + filename.c_str());
00077 }
00078
00079 }
00080
00081 int FileIOHandler::read(OUT char *buf, IN size_t length)
00082 {
00083 int ret = 0;
00084
00085 ret = fread(buf, sizeof(char), length, f);
00086
00087 if (ret <= 0)
00088 {
00089 if (feof(f)) return 0;
00090 if (ferror(f)) return -1;
00091 }
00092
00093 return ret;
00094 }
00095
00096 int FileIOHandler::write(IN char *buf, IN size_t length)
00097 {
00098 int ret = 0;
00099
00100 ret = fwrite(buf, sizeof(char), length, f);
00101
00102 return ret;
00103 }
00104
00105 void FileIOHandler::seek(IN off_t offset, IN int whence)
00106 {
00107 if (fseeko(f, offset, whence) != 0)
00108 {
00109 throw _Exception(_("fseek failed"));
00110 }
00111 }
00112
00113 void FileIOHandler::close()
00114 {
00115 if (fclose(f) != 0)
00116 {
00117 throw _Exception(_("fclose failed"));
00118 }
00119 f = NULL;
00120 }