-
Notifications
You must be signed in to change notification settings - Fork 2
/
space.h
159 lines (155 loc) · 5.39 KB
/
space.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#ifndef _SPACE_H_
#define _SPACE_H_
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
namespace mystd{
int group;
namespace myfstream{
class ifstream {
private:
int user;
char path[32];
int pos; //position of the character to be extracted from filebuf
char filebuf[32];
public:
ifstream(char* src){
//std::cout << "[mystd::myfstream::ifstream::ifstream(char*)]: strlen(amo):" << strlen("amo") << std::endl;
if(strlen(src) <= sizeof(path) )strncpy(path, src, strlen(src)+1);
pos = 0;
memset(filebuf, 'X', sizeof(filebuf));
std::cout << "[mystd::myfstream::ifstream::ifstream(char*)]: >>> ifstream file path:" << path << std::endl;
}
void read(char *src, int length){
std::cout << "[mystd::myfstream::ifstream::read(char*,int)]: >>> pos:" << pos << ", length:" << length << std::endl;
if(!path[0]){
std::cout << "[mystd::myfstream::ifstream::read(char*,int)]: !!! file path is null" << std::endl;
return;
}
if(length <= sizeof(filebuf)-pos){
std::cout << "[mystd::myfstream::ifstream::read(char*,int)]: going to memcpy() for " << length << "bytes" << std::endl;
memcpy(filebuf+pos, src, length);
std::cout << "[mystd::myfstream::ifstream::read(char*,int)]: data extracted from filebuf:" << std::endl;
for(int i=pos; i<length; i++) std::cout << filebuf[i] << std::endl;
std::cout << std::endl;
}else{
std::cout << "[mystd::myfstream::ifstream::read(char*,int)]: going to memcpy() for only " << sizeof(filebuf)-pos << "bytes" << std::endl;
memcpy(filebuf+pos, src, sizeof(filebuf)-pos);
std::cout << "[mystd::myfstream::ifstream::read(char*,int)]: data extracted from filebuf:" << std::endl;
for(int i=pos; i<sizeof(filebuf); i++) std::cout << filebuf[i] << std::endl;
std::cout << std::endl;
}
}
void seekg(int offset, int mode){
if(offset >= sizeof(filebuf)){
std::cout << "offset is beyond file buffer size" << std::endl;
return;
}
switch(mode){
case 0: { //current
pos = offset;
break;
}
case 1: { //begin
pos = 0;
break;
}
case 2: {
pos = sizeof(filebuf)-1; //end
break;
}
}
}
int tellg(){
return pos;
}
void setUser(int user_id){
user = user_id;
}
int getUser(){
return user;
}
/**
* The rule is that, when looking for a suitable function overload
* , both the current namespace and all namespaces of the argument type definitions are considered.
* This is called Argument Dependent Lookup (ADL).
*/
friend std::istream& operator>>(std::istream &stream, ifstream &fis);
friend std::ostream& operator<<(std::ostream &stream, ifstream &fis);
};
class ofstream {
private:
int user;
char path[32];
int pos;
char filebuf[32];
public:
ofstream(char* src){
if(strlen(src) <= sizeof(path) )strncpy(path, src, strlen(src)+1);
pos = 0;
memset(filebuf, 'Y', sizeof(filebuf));
std::cout << "[mystd::myfstream::ofstream::ofstream(char*)]: >>> ofstream file path:" << path << std::endl;
}
void write(char *src, int length){
std::cout << "[mystd::myfstream::ofstream::write(char*,int)]: >>> pos:" << pos << ", length:" << length << std::endl;
if(!path[0]){
std::cout << "[mystd::myfstream::ofstream::write(char*,int)]: !!! file path is null" << std::endl;
return;
}
if(length <= sizeof(filebuf)-pos){
std::cout << "[mystd::myfstream::ofstream::write(char*,int)]: going to memcpy() for " << length << "bytes" << std::endl;
memcpy(filebuf+pos, src, length);
std::cout << "[mystd::myfstream::ifstream::read(char*,int)]: data written to filebuf:" << std::endl;
for(int i=pos;i < length; i++) std::cout << filebuf[i] << std::endl;
std::cout << std::endl;
}else{
std::cout << "[mystd::myfstream::ofstream::write(char*,int)]: going to memcpy() for only " << sizeof(filebuf)-pos << "bytes" << std::endl;
memcpy(filebuf+pos, src, sizeof(filebuf)-pos);
std::cout << "[mystd::myfstream::ifstream::read(char*,int)]: data written to filebuf:" << std::endl;
for(int i=pos;i < sizeof(filebuf); i++) std::cout << filebuf[i] << std::endl;
std::cout << std::endl;
}
}
void seekp(int offset, int mode){
if(offset >= sizeof(filebuf)){
std::cout << "offset is beyond file buffer size" << std::endl;
return;
}
switch(mode){
case 0: { //current
pos = offset;
break;
}
case 1: { //begin
pos = 0;
break;
}
case 2: {
pos = sizeof(filebuf)-1; //end
break;
}
}
}
int tellp(){
return pos;
}
void setUser(int user_id){
user = user_id;
}
int getUser(){
return user;
}
friend std::ostream& operator<<(std::ostream &stream, mystd::myfstream::ofstream &fos);
friend std::istream& operator>>(std::istream &stream, mystd::myfstream::ofstream &fos);
};
/*
std::istream& operator>>(std::istream &stream, mystd::myfstream::ifstream &fis);
std::ostream& operator<<(std::ostream &stream, mystd::myfstream::ifstream &fis);
std::ostream& operator<<(std::ostream &stream, mystd::myfstream::ofstream &fos);
std::istream& operator>>(std::istream &stream, mystd::myfstream::ofstream &fos);
*/
};
};
#endif