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 #ifdef HAVE_CONFIG_H
00032 #include "autoconfig.h"
00033 #endif
00034
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 "ixml.h"
00043 #include <time.h>
00044 #include "common.h"
00045 #include "storage.h"
00046 #include "cds_objects.h"
00047 #include "process.h"
00048 #include "update_manager.h"
00049 #include "mem_io_handler.h"
00050 #include "dictionary.h"
00051
00052 using namespace zmm;
00053 using namespace mxml;
00054
00055 MemIOHandler::MemIOHandler(void *buffer, int length) : IOHandler()
00056 {
00057 this->buffer = (char *)MALLOC(length);
00058 this->length = length;
00059 memcpy(this->buffer, buffer, length);
00060 }
00061
00062 MemIOHandler::MemIOHandler(String str) : IOHandler()
00063 {
00064 this->length = str.length();
00065 this->buffer = (char *)MALLOC(length);
00066 memcpy(this->buffer, str.c_str(), length);
00067 }
00068
00069 MemIOHandler::~MemIOHandler()
00070 {
00071 FREE(buffer);
00072 }
00073
00074 void MemIOHandler::open(IN enum UpnpOpenFileMode mode)
00075 {
00076 pos = 0;
00077 }
00078
00079 int MemIOHandler::read(OUT char *buf, IN size_t length)
00080 {
00081 int ret = 0;
00082
00083
00084 if (pos == -1)
00085 {
00086 return 0;
00087 }
00088
00089 int rest = this->length - pos;
00090 if (length > (size_t)rest)
00091 length = rest;
00092
00093 memcpy(buf, buffer + pos, length);
00094 pos = pos + length;
00095 ret = (int) length;
00096
00097 if (pos >= this->length)
00098 {
00099 pos = -1;
00100 }
00101
00102 return ret;
00103 }
00104
00105 void MemIOHandler::seek(IN off_t offset, IN int whence)
00106 {
00107 if (whence == SEEK_SET)
00108 {
00109
00110 if (offset < 0)
00111 {
00112 throw _Exception(_("MemIOHandler seek failed: SEEK_SET used with negative offset"));
00113 }
00114
00115 if (offset > length)
00116 {
00117 throw _Exception(_("MemIOHandler seek failed: trying to seek past the end of file"));
00118 }
00119
00120 pos = offset;
00121 }
00122 else if (whence == SEEK_CUR)
00123 {
00124 long temp;
00125
00126 if (pos == -1)
00127 {
00128 temp = length;
00129 }
00130 else
00131 {
00132 temp = pos;
00133 }
00134
00135 if (((temp + offset) > length) ||
00136 ((temp + offset) < 0))
00137 {
00138 throw _Exception(_("MemIOHandler seek failed: trying to seek before the beginning/past end of file"));
00139 }
00140
00141 pos = temp + offset;
00142 }
00143 else if (whence == SEEK_END)
00144 {
00145 long temp = length;
00146 if (((temp + offset) > length) ||
00147 ((temp + offset) < 0))
00148 {
00149 throw _Exception(_("MemIOHandler seek failed: trying to seek before the beginning/past end of file"));
00150 }
00151
00152 pos = temp + offset;
00153 }
00154 else
00155 {
00156 throw _Exception(_("MemIOHandler seek failed: unrecognized whence"));
00157 }
00158 }