summaryrefslogtreecommitdiff
path: root/ndb/include/util/File.hpp
blob: 3ed0ad7a6f919cbc545eecbff5993a23e8e39f42 (plain)
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifndef FILE_H
#define FILE_H

#include <ndb_global.h>

/**
 * This class provides a file abstraction . It has operations
 * to create, read, write and delete a file.
 *
 * @version #@ $Id: File.hpp,v 1.5 2002/04/26 13:15:38 ejonore Exp $
 */
class File_class
{
public:
  /**
   * Returns true if the file exist.
   *
   * @param aFileName a filename to check.
   * @return true if the file exists.
   */
  static bool exists(const char* aFileName);      

  /**
   * Returns the size of a file.
   *
   * @param f a pointer to a FILE descriptor.
   * @return the size of the file.
   */
  static long size(FILE* f);

  /**
   * Renames a file.
   *
   * @param currFileName the current name of the file.
   * @param newFileName the new name of the file.
   * @return true if successful.
   */
  static bool rename(const char* currFileName, const char* newFileName);

  /**
   * Removes/deletes a file.
   *
   * @param aFileName the file to remove.
   * @return true if successful.
   */
  static bool remove(const char* aFileName);      
  
  /**
   * Default constructor.
   */
  File_class();

  /**
   * Creates a new File  with the specified filename and file mode. 
   * The real file itself will not be created unless open() is called!
   *
   * To see the available file modes use 'man 3 fopen'.
   *
   * @param aFileName a filename.
   * @param mode the mode which the file should be opened/created with, default "r".
   */
  File_class(const char* aFileName, const char* mode = "r");

  /**
   * Destructor.
   */
  ~File_class();

  /**
   * Opens/creates the file. If open() fails then 'errno' and perror() 
   * should be used to determine the exact failure cause.
   *
   * @return true if successful. Check errno if unsuccessful.
   */
  bool open();

  /**
   * Opens/creates the file with the specified name and mode.
   * If open() fails then 'errno' and perror() should be used to 
   * determine the exact failure cause.
   *
   * @param aFileName the file to open.
   * @param mode the file mode to use.
   * @return true if successful. Check errno if unsuccessful.   
   */
  bool open(const char* aFileName, const char* mode);

  /**
   * Removes the file.
   *
   * @return true if successful.
   */
  bool remove();

  /**
   * Closes the file, i.e., the FILE descriptor is closed.
   */
  bool close();

  /**
   * Read from the file. See fread() for more info.
   *
   * @param buf the buffer to read into.
   * @param itemSize the size of each item. 
   * @param nitems read max n number of items.
   * @return 0 if successful.
   */
  int read(void* buf, size_t itemSize, size_t nitems) const;

  /**
   * Read char from the file. See fread() for more info.
   *
   * @param buf the buffer to read into.
   * @param start the start index of the buf.
   * @param length the length of the buffer.
   * @return 0 if successful.
   */
  int readChar(char* buf, long start, long length) const;  

  /**
   * Read chars from the file. See fread() for more info.
   *
   * @param buf the buffer to read into.
   * @return 0 if successful.
   */
  int readChar(char* buf);  

  /**
   * Write to file. See fwrite() for more info.
   *
   * @param buf the buffer to read from.
   * @param itemSize the size of each item. 
   * @param nitems write max n number of items.
   * @return 0 if successful.
   */
  int write(const void* buf, size_t itemSize, size_t nitems);     

  /**
   * Write chars to file. See fwrite() for more info.
   *
   * @param buf the buffer to read from.
   * @param start the start index of the buf.
   * @param length the length of the buffer.
   * @return 0 if successful.
   */
  int writeChar(const char* buf, long start, long length);   

  /**
   * Write chars to file. See fwrite() for more info.
   *
   * @param buf the buffer to read from.
   * @return 0 if successful.
   */
  int writeChar(const char* buf); 

  /**
   * Returns the file size.
   *
   * @return the file size.
   */
  long size() const;

  /**
   * Returns the filename.
   *
   * @return the filename.
   */
  const char* getName() const;

  /**
   * Flush the buffer.
   *
   * @return 0 if successful.
   */
  int flush() const;

private:
  STATIC_CONST( MAX_FILE_NAME_SIZE = 128 );

  FILE* m_file;
  char m_fileName[MAX_FILE_NAME_SIZE];
  const char* m_fileMode;
  /* Prohibit */
  File_class (const File_class& aCopy);
  File_class operator = (const File_class&); 
  bool operator == (const File_class&);
};
#endif