-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfile.cpp
More file actions
164 lines (151 loc) · 2.77 KB
/
file.cpp
File metadata and controls
164 lines (151 loc) · 2.77 KB
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
160
161
162
163
#include "file.h"
File::File()
:fptr(NULL)
{}
File::File(char * f_name, const char *mode)
{
char *path=get_path(f_name);
fptr=fopen(path, mode);
if(fptr==NULL)
fptr=fopen(path,"w");
free(path);
}
File::File(const char * table_name, const char * attr_name,const char *mode)
{
char *path=get_path_(table_name, attr_name);
fptr = fopen(path, mode);
if(fptr==NULL){
fptr=fopen(path,"w+");
}
free(path);
}
File::~File()
{
//std::cout<<"File desctructor, closing the file!\n";
if(fptr!=NULL)
fclose(fptr);
}
void File::create_file(char* name)
{
char* path=get_path(name);
fptr=fopen(path,"w");
free(path);
}
FILE* File::get_block(const int block_num)
{
if(fseek(fptr, block_num * BLOCK_SIZE, SEEK_SET)==0)
return fptr;
else
return NULL;
}
int File::disk2buff(FILE* fp, char * buf_ptr)
{
if(fread(buf_ptr,BLOCK_SIZE,1,fp)!=1)
return 0;
else
return 1;
}
int File::buff2disk(FILE* fp, char * buf_ptr)
{
if(fwrite(buf_ptr,BLOCK_SIZE,1,fp)!=1)
return 0;
else
return 1;
}
int File::buff2disk_apd(char *buf_ptr)
{
if(fwrite(buf_ptr,BLOCK_SIZE,1,fptr)!=1)
return 0;
else
return 1;
}
int File::disk2buff_(const int block_num, char * buf_ptr)
{
if(fseek(fptr, block_num*BLOCK_SIZE, SEEK_SET)==0){
if(fread(buf_ptr,BLOCK_SIZE,1,fptr)!=1){
printf("read error\n");
return 0;
}
else
return 1;
}
printf("seek error\n");
return 0;
}
int File::buff2disk_(const int block_num, char* buf_ptr)
{
if(fseek(fptr, block_num*BLOCK_SIZE, SEEK_SET)==0){
if(fwrite(buf_ptr,BLOCK_SIZE,1,fptr)!=1){
printf("write error\n");
return 0;
}
else
return 1;
}
printf("seek error\n");
return 0;
}
int File::get_file_size()
{
rewind(fptr);
fseek(fptr, 0L, SEEK_END);
int size = ftell(fptr);
rewind(fptr);
return size;
}
void File::direct_print(int num_attrs)
{
using namespace std;
int buf[12];
while(fread(buf,BLOCK_SIZE,1,fptr)==1){
for(int i=0;i<num_attrs;i++){
cout<<setw(13)<<left<<buf[i];
}
cout<<"\n";
}
}
char * File::get_path(char * f_name)
{
char *path = (char*)malloc(strlen(f_name)+6);
strcpy(path, DATA);
strcat(path, f_name);
return path;
}
char* File::get_path_(const char * table,const char* attr)
{
char suffix[]=".index";
int len=strlen(table)+strlen(attr)+strlen(suffix)+strlen(DATA);
char *path = (char*)calloc(1,len+2);
strcat(path,DATA);
strcat(path, table);
strcat(path,"_");
strcat(path, attr);
strcat(path, suffix);
path[len+1]='\0';
//printf("open file: %s\n",path);
return path;
}
int File::dele_file(const char* table,const char* attr)
{
char *path=get_path_(table,attr);
if(remove(path)==0)
{
free(path);
return 1;
}else{
free(path);
return 0;
}
}
int File::dele_file(char * f_name)
{
char *path = get_path(f_name);
if(remove(path)==0)
{
free(path);
return 1;
}else{
free(path);
return 0;
}
}