Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
NP_TMcode
Manage
Activity
Members
Plan
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container Registry
Analyze
Contributor analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Giacomo Mulas
NP_TMcode
Commits
a43c3245
Commit
a43c3245
authored
10 months ago
by
Giovanni La Mura
Browse files
Options
Downloads
Patches
Plain Diff
Implement VirtualAsciiFile local interface
parent
d8b92e8a
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/include/file_io.h
+46
-11
46 additions, 11 deletions
src/include/file_io.h
src/libnptm/file_io.cpp
+49
-11
49 additions, 11 deletions
src/libnptm/file_io.cpp
with
95 additions
and
22 deletions
src/include/file_io.h
+
46
−
11
View file @
a43c3245
...
@@ -165,41 +165,76 @@ class HDFFile {
...
@@ -165,41 +165,76 @@ class HDFFile {
*/
*/
class
VirtualAsciiFile
{
class
VirtualAsciiFile
{
protected:
protected:
//! \brief The number of lines.
int32_t
_num_lines
;
//! \brief A vector of strings representing the file lines.
//! \brief A vector of strings representing the file lines.
std
::
vector
<
std
::
string
>
*
file_lines
;
std
::
vector
<
std
::
string
>
*
_file_lines
;
//! \brief The name of the file.
std
::
string
_file_name
;
public:
public:
const
std
::
string
&
file_name
=
_file_name
;
const
int32_t
&
num_lines
=
_num_lines
;
/*! \brief VirtualAsciiFile instance constructor.
/*! \brief VirtualAsciiFile instance constructor.
*
*
* \param
name: `const string&` Reference to a string for the file name
.
* \param
lines: `int32_t` Number of lines, if known in advance (optional, default is 0)
.
*/
*/
VirtualAsciiFile
(
const
std
::
string
&
name
);
VirtualAsciiFile
(
int32_t
lines
=
0
);
/*! \brief VirtualAsciiFile copy constructor.
/*! \brief VirtualAsciiFile copy constructor.
*
*
* \param rhs: `const VirtualAsciiFile&` Reference to a VirtualAsciiFile instance.
* \param rhs: `const VirtualAsciiFile&` Reference to a VirtualAsciiFile instance.
* \param name: `const string&` Name of the copy (optional, default is the same as original).
*/
*/
VirtualAsciiFile
(
const
VirtualAsciiFile
&
rhs
,
const
std
::
string
&
name
=
""
);
VirtualAsciiFile
(
const
VirtualAsciiFile
&
rhs
);
/*! \brief VirtualAsciiFile instance destroyer.
/*! \brief VirtualAsciiFile instance destroyer.
*/
*/
~
VirtualAsciiFile
();
~
VirtualAsciiFile
();
/*! \brief Append another VirtualAsciiFile at the end of the current instance.
*
* \param rhs: `const VirtualAsciiFile&` Reference to the VirtualAsciiFile to be appended.
*/
void
append
(
const
VirtualAsciiFile
&
rhs
);
/*! \brief Append a line at the end of the file.
/*! \brief Append a line at the end of the file.
*
*
* \param line: `const string&` Reference to a string representing the line.
* \param line: `const string&` Reference to a string representing the line.
*/
*/
void
append
(
const
std
::
string
&
line
);
void
append_line
(
const
std
::
string
&
line
);
/*! \brief Append the contents of the VirtualAsciiFile to a physical file on disk.
*
* \param file_name: `const string&` Name of the file to append contents to.
* \return result: `int` A result code (0 if successful).
*/
int
append_to_disk
(
const
std
::
string
&
file_name
);
/*! \brief Insert another VirtualAsciiFile at a given position.
*
* This function inserts a target VirtualAsciiFile in the current one at the given
* position. Optionally, a range of lines to be inserted can be specified, otherwise
* the full content of the target file is inserted. This function DOES NOT increase
* the size of the inner storage and it can only be used if the inner storage has
* already been adjusted to contain the insertion target.
*
* \param position: `int32_t` The position at which the other file is inserted in this one.
* \param rhs: `const VirtualAsciiFile&` The refence to the VirtualAsciiFile to be inserted.
* \param start: `int32_t` The first line to be inserted (optional, default is 0).
* \param end: `int32_t` The last line to be inserted (optional, default is 0 to read all).
* \param line: `const string&` Reference to a string representing the line.
* \return result: `int` A result code (0 if successful).
*/
int
insert
(
int32_t
position
,
VirtualAsciiFile
&
rhs
,
int32_t
start
=
0
,
int32_t
end
=
0
);
/*! \brief Get the number of lines in the current instance.
*
* \return size: `int32_t` The number of lines in the VirtualAsciiFile instance.
*/
int32_t
number_of_lines
()
{
return
_file_lines
->
size
();
}
/*! \brief Write virtual file contents to a real file on disk.
/*! \brief Write virtual file contents to a real file on disk.
*
*
* \param file_name: `const string&` Name of the file to append contents to.
* \return result: `int` A result code (0 if successful).
* \return result: `int` A result code (0 if successful).
*/
*/
int
write_to_disk
();
int
write_to_disk
(
const
std
::
string
&
file_name
);
};
};
#endif
#endif
This diff is collapsed.
Click to expand it.
src/libnptm/file_io.cpp
+
49
−
11
View file @
a43c3245
...
@@ -229,17 +229,15 @@ herr_t HDFFile::write(
...
@@ -229,17 +229,15 @@ herr_t HDFFile::write(
/* >>> End of HDFFile class implementation <<< */
/* >>> End of HDFFile class implementation <<< */
/* >>> VirtualAsciiFile class implementation <<< */
/* >>> VirtualAsciiFile class implementation <<< */
VirtualAsciiFile
::
VirtualAsciiFile
(
const
std
::
string
&
name
)
{
VirtualAsciiFile
::
VirtualAsciiFile
(
int32_t
lines
)
{
_file_name
=
name
;
_file_lines
=
new
vector
<
string
>
();
_file_lines
=
new
vector
<
string
>
();
for
(
int32_t
li
=
0
;
li
<
lines
;
li
++
)
{
_file_lines
->
push_back
(
""
);
}
}
}
VirtualAsciiFile
::
VirtualAsciiFile
(
const
VirtualAsciiFile
&
rhs
,
const
std
::
string
&
name
)
{
VirtualAsciiFile
::
VirtualAsciiFile
(
const
VirtualAsciiFile
&
rhs
)
{
if
(
name
.
compare
(
""
)
==
0
)
{
_num_lines
=
rhs
.
_num_lines
;
_file_name
=
rhs
.
_file_name
;
}
else
{
_file_name
=
name
;
}
_file_lines
=
new
vector
<
string
>
();
_file_lines
=
new
vector
<
string
>
();
for
(
vector
<
string
>::
iterator
it
=
rhs
.
_file_lines
->
begin
();
it
!=
rhs
.
_file_lines
->
end
();
++
it
)
{
for
(
vector
<
string
>::
iterator
it
=
rhs
.
_file_lines
->
begin
();
it
!=
rhs
.
_file_lines
->
end
();
++
it
)
{
_file_lines
->
push_back
(
*
it
);
_file_lines
->
push_back
(
*
it
);
...
@@ -247,14 +245,54 @@ VirtualAsciiFile::VirtualAsciiFile(const VirtualAsciiFile& rhs, const std::strin
...
@@ -247,14 +245,54 @@ VirtualAsciiFile::VirtualAsciiFile(const VirtualAsciiFile& rhs, const std::strin
}
}
VirtualAsciiFile
::~
VirtualAsciiFile
()
{
VirtualAsciiFile
::~
VirtualAsciiFile
()
{
while
(
!
_file_lines
->
size
()
>
0
)
{
_file_lines
->
pop_back
();
}
if
(
_file_lines
!=
NULL
)
delete
_file_lines
;
if
(
_file_lines
!=
NULL
)
delete
_file_lines
;
}
}
void
VirtualAsciiFile
::
append
(
const
string
&
line
)
{
void
VirtualAsciiFile
::
append
(
const
VirtualAsciiFile
&
rhs
)
{
_file_lines
.
push_back
(
line
);
for
(
vector
<
string
>::
iterator
it
=
rhs
.
_file_lines
->
begin
();
it
!=
rhs
.
_file_lines
->
end
();
++
it
)
{
_file_lines
->
push_back
(
*
it
);
}
}
void
VirtualAsciiFile
::
append_line
(
const
string
&
line
)
{
_file_lines
->
push_back
(
line
);
}
int
VirtualAsciiFile
::
append_to_disk
(
const
std
::
string
&
file_name
)
{
int
result
=
0
;
fstream
output_file
;
output_file
.
open
(
file_name
,
ios
::
app
);
if
(
output_file
.
is_open
())
{
for
(
vector
<
string
>::
iterator
it
=
_file_lines
->
begin
();
it
!=
_file_lines
->
end
();
++
it
)
{
output_file
<<
*
it
;
}
}
else
{
result
=
1
;
}
return
result
;
}
int
VirtualAsciiFile
::
insert
(
int32_t
position
,
VirtualAsciiFile
&
rhs
,
int32_t
start
,
int32_t
end
)
{
int
result
=
0
;
if
(
start
==
0
&&
end
==
0
)
{
end
=
rhs
.
number_of_lines
();
}
int32_t
final_index
=
position
+
end
-
start
;
if
(
final_index
<=
number_of_lines
())
{
for
(
int32_t
li
=
start
;
li
<
end
;
li
++
)
{
_file_lines
->
at
(
position
++
)
=
rhs
.
_file_lines
->
at
(
li
);
}
}
else
{
// ERROR: target file is too long;
result
=
1
;
}
return
result
;
}
}
int
VirtualAsciiFile
::
write_to_disk
()
{
int
VirtualAsciiFile
::
write_to_disk
(
const
std
::
string
&
file_name
)
{
int
result
=
0
;
int
result
=
0
;
fstream
output_file
;
fstream
output_file
;
output_file
.
open
(
file_name
,
ios
::
out
);
output_file
.
open
(
file_name
,
ios
::
out
);
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment