Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F81405421
file_manager.h
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Thu, Sep 5, 21:18
Size
5 KB
Mime Type
text/x-c
Expires
Sat, Sep 7, 21:18 (2 d)
Engine
blob
Format
Raw Data
Handle
20563669
Attached To
rIOHELPER iohelper
file_manager.h
View Options
/*
Copyright 2008 Guillaume ANCIAUX (guillaume.anciaux@epfl.ch)
This file is part of ParaViewHelper.
ParaViewHelper 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 3 of the License, or
(at your option) any later version.
ParaViewHelper 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 ParaViewHelper. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef LM_FILE_MANAGER
#define LM_FILE_MANAGER
#include <zlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string>
#include <iostream>
#include "common.h"
class
File
{
public:
File
(){
compressed
=
0
;
_file
=
NULL
;
fd
=
-
1
;
#ifdef USING_ZLIB
gzfile
=
NULL
;
#endif
opened
=
0
;
};
File
(
const
char
*
fname
,
const
char
*
mode
=
NULL
,
int
compr
=
false
){
compressed
=
compr
;
_file
=
NULL
;
fd
=
-
1
;
#ifdef USING_ZLIB
gzfile
=
NULL
;
#endif
opened
=
0
;
open
(
fname
,
mode
,
compressed
);
};
void
printf
(
const
char
*
,...);
void
close
();
void
open
(
const
char
*
,
const
char
*
mode
=
NULL
,
int
compr
=
false
);
void
flush
();
int
tell
();
int
seek
(
int
offset
,
int
set
);
int
dumpchar
(
int
c
);
int
read
(
void
*
buffer
,
int
size
,
int
number
);
int
write
(
void
*
buffer
,
int
size
,
int
number
);
char
*
gets
(
char
*
buf
,
int
len
);
private:
FILE
*
_file
;
int
fd
;
#ifdef USING_ZLIB
gzFile
gzfile
;
#endif
int
opened
;
int
compressed
;
std
::
string
name
;
};
inline
void
File
::
printf
(
const
char
*
formated
,
...){
if
(
!
opened
)
{
FATAL
(
"Warning : file not opened "
<<
name
);
}
// if (!opened) return;
if
(
_file
==
NULL
)
FATAL
(
"fichier non ouvert mais ce n'est pas normal"
);
char
buf
[
512
];
va_list
list
;
va_start
(
list
,
formated
);
int
len
=
vsprintf
(
buf
,
formated
,
list
);
va_end
(
list
);
#ifdef USING_ZLIB
if
(
compressed
){
gzwrite
(
gzfile
,
buf
,
len
);
return
;
}
else
#endif
fwrite
(
buf
,
len
,
1
,
_file
);
}
inline
char
*
File
::
gets
(
char
*
buf
,
int
len
){
char
*
ret
;
if
(
!
opened
)
{
FATAL
(
"Warning : file not opened "
<<
name
);
}
if
(
_file
==
NULL
)
FATAL
(
"fichier non ouvert mais ce n'est pas normal"
);
DUMP
(
"read to buf ("
<<
buf
<<
") at most "
<<
len
<<
" characters"
);
#ifdef USING_ZLIB
if
(
compressed
){
ret
=
gzgets
(
gzfile
,
buf
,
len
);
}
else
#endif
ret
=
fgets
(
buf
,
len
,
_file
);
DUMP
(
"read to buf ("
<<
buf
<<
") at most "
<<
len
<<
" characters"
);
return
ret
;
}
inline
void
File
::
close
(){
// if (!opened) FATAL("file not opened");
if
(
!
opened
)
{
FATAL
(
"Warning : file not opened "
<<
name
);
}
#ifdef USING_ZLIB
if
(
compressed
){
gzclose
(
gzfile
);
gzfile
=
NULL
;
}
else
#endif
fclose
(
_file
);
_file
=
NULL
;
fd
=
-
1
;
opened
=
false
;
DUMP
(
"file "
<<
name
<<
" closed"
);
}
inline
void
File
::
open
(
const
char
*
fname
,
const
char
*
mode
,
int
compr
){
char
_mode
[
4
]
=
"wb+"
;
if
(
mode
==
NULL
)
mode
=
_mode
;
if
(
opened
)
{
close
();
}
compressed
=
compr
;
if
(
compressed
){
char
buffer
[
256
];
sprintf
(
buffer
,
"%s.gz"
,
fname
);
_file
=
fopen
(
buffer
,
mode
);
if
(
_file
==
NULL
)
FATAL
(
"Could not open file "
<<
buffer
);
name
=
buffer
;
//DUMP("file opened in compressed form " << name);
}
else
{
_file
=
fopen
(
fname
,
mode
);
if
(
_file
==
NULL
)
FATAL
(
"Could not open file "
<<
fname
);
name
=
fname
;
}
opened
=
true
;
fd
=
fileno
(
_file
);
#ifdef USING_ZLIB
if
(
compressed
){
char
temp
[
10
];
sprintf
(
temp
,
"%s%d"
,
mode
,
9
);
gzfile
=
gzdopen
(
fd
,
temp
);
}
#endif
DUMP
(
"file "
<<
name
<<
" opened , compressed = "
<<
compressed
);
}
inline
int
File
::
dumpchar
(
int
c
){
if
(
!
opened
)
{
FATAL
(
"Warning : file not opened "
<<
name
);
}
// if (!opened) return EOF;
#ifdef USING_ZLIB
if
(
compressed
){
int
res
=
gzputc
(
gzfile
,
c
);
//DUMP("file opened in compressed form " << name);
if
(
c
!=
res
)
FATAL
(
"j'ai pas ecrit ce que je voulais (compressed) "
<<
res
);
return
res
;
}
else
#endif
if
(
c
!=
putc
(
c
,
_file
))
FATAL
(
"j'ai pas ecrit ce que je voulais"
);
return
c
;
}
inline
int
File
::
seek
(
int
offset
,
int
set
){
if
(
!
opened
)
{
FATAL
(
"Warning : file not opened "
<<
name
);
}
#ifdef USING_ZLIB
if
(
compressed
){
return
gzseek
(
gzfile
,
offset
,
set
);
}
else
#endif
return
fseek
(
_file
,
offset
,
set
);
}
inline
int
File
::
tell
(){
if
(
!
opened
)
{
FATAL
(
"Warning : file not opened "
<<
name
);
}
#ifdef USING_ZLIB
if
(
compressed
){
return
gztell
(
gzfile
);
}
else
#endif
return
ftell
(
_file
);
}
inline
int
File
::
read
(
void
*
buffer
,
int
size
,
int
number
){
if
(
!
opened
)
{
FATAL
(
"Warning : file not opened "
<<
name
);
}
#ifdef USING_ZLIB
if
(
compressed
){
return
gzread
(
gzfile
,
buffer
,
size
*
number
);
}
else
#endif
return
fread
(
buffer
,
size
,
number
,
_file
);
}
inline
int
File
::
write
(
void
*
buffer
,
int
size
,
int
number
){
if
(
!
opened
)
{
FATAL
(
"Warning : file not opened "
<<
name
);
}
#ifdef USING_ZLIB
if
(
compressed
){
return
gzwrite
(
gzfile
,
buffer
,
size
*
number
);
}
else
#endif
return
fwrite
(
buffer
,
size
,
number
,
_file
);
}
inline
void
File
::
flush
(){
if
(
!
opened
)
{
FATAL
(
"Warning : file not opened "
<<
name
);
}
#ifdef USING_ZLIB
if
(
compressed
){
gzflush
(
gzfile
,
Z_SYNC_FLUSH
);
}
else
#endif
fflush
(
_file
);
}
#endif
Event Timeline
Log In to Comment