STTNet
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Public Member Functions | Protected Attributes | Static Protected Attributes | List of all members
stt::file::File Class Reference

A class for reading and writing disk files. More...

#include <sttnet_English.h>

Inheritance diagram for stt::file::File:
stt::file::FileTool stt::file::LogFile

Public Member Functions

bool openFile (const std::string &fileName, const bool &create=true, const int &multiple=0, const size_t &size=0, const mode_t &mode=0666)
 Open a file. More...
 
bool closeFile (const bool &del=false)
 Close the opened file. More...
 
 ~File ()
 Destructor. More...
 
bool isOpen ()
 Check if the object has opened a file. More...
 
bool isBinary ()
 Check if the object has opened the file in binary mode. More...
 
std::string getFileName ()
 Get the name of the opened file. More...
 
uint64_t getFileLine ()
 Get the number of lines in the opened file. More...
 
size_t getFileSize ()
 Get the size of the file opened in binary mode. More...
 
size_t getSize1 ()
 Get the size of the file opened in binary mode in memory. More...
 
bool lockMemory ()
 Read data from disk into memory. More...
 
bool unlockMemory (const bool &rec=false)
 Write data from memory to disk. More...
 
Functions for operating on data in memory - Text mode
int findC (const std::string &targetString, const int linePos=1)
 Find a line. More...
 
bool appendLineC (const std::string &data, const int &linePos=0)
 Insert a line. More...
 
bool deleteLineC (const int &linePos=0)
 Delete a line. More...
 
bool deleteAllC ()
 Delete all lines. More...
 
bool chgLineC (const std::string &data, const int &linePos=0)
 Modify a line. More...
 
bool readLineC (std::string &data, const int linePos)
 Read a single line. More...
 
std::string & readC (std::string &data, const int &linePos, const int &num)
 Read multiple lines. More...
 
std::string & readAllC (std::string &data)
 Read all lines. More...
 
Functions for operating on data in memory - Binary mode
bool readC (char *data, const size_t &pos, const size_t &size)
 Read a data block. More...
 
bool writeC (const char *data, const size_t &pos, const size_t &size)
 Write a data block. More...
 
void formatC ()
 Format the data. More...
 
Functions for directly operating on disk data - Text mode
int find (const std::string &targetString, const int linePos=1)
 Find a line. More...
 
bool appendLine (const std::string &data, const int &linePos=0)
 Insert a line. More...
 
bool deleteLine (const int &linePos=0)
 Delete a line. More...
 
bool deleteAll ()
 Delete all lines. More...
 
bool chgLine (const std::string &data, const int &linePos=0)
 Modify a line. More...
 
bool readLine (std::string &data, const int linePos)
 Read a single line. More...
 
std::string & read (std::string &data, const int &linePos, const int &num)
 Read multiple lines. More...
 
std::string & readAll (std::string &data)
 Read all lines. More...
 
Functions for directly operating on disk data - Binary mode
bool read (char *data, const size_t &pos, const size_t &size)
 Read a data block. More...
 
bool write (const char *data, const size_t &pos, const size_t &size)
 Write a data block. More...
 
void format ()
 Format the data. More...
 

Protected Attributes

std::mutex che
 

Static Protected Attributes

static std::mutex l1
 
static std::unordered_map
< std::string, FileThreadLock
fl2
 

Detailed Description

A class for reading and writing disk files.

Note
1. The same object of this class can ensure synchronization and thread safety.
2. If the same file is operated by objects of this class, synchronization can also be ensured. However, note that when opening the file, use either an absolute path or a relative path consistently.
3. There are two modes when operating files with this class: 1. Read from disk + operate on data in memory + save to disk; these three steps are done separately, suitable for scenarios with complex custom operations. 2. Combine the three operations into one step.

Constructor & Destructor Documentation

stt::file::File::~File ( )
inline

Destructor.

Note
By default, the file is closed when the object's lifecycle ends, and the file is not deleted.

Member Function Documentation

bool stt::file::File::appendLine ( const std::string &  data,
const int &  linePos = 0 
)

Insert a line.

Note
1. Insert a line into the file.
2. Line numbers start from 1.
Parameters
dataThe data to be inserted.
linePosInsert at the specified line (default is to insert at the end).
Returns
true if the insertion is successful.
false if the insertion fails.
bool stt::file::File::appendLineC ( const std::string &  data,
const int &  linePos = 0 
)

Insert a line.

Note
1. Insert a line into the data imported from the file into memory.
2. Line numbers start from 1.
Parameters
dataThe data to be inserted.
linePosInsert at the specified line (default is to insert at the end).
Returns
true if the insertion is successful.
false if the insertion fails.
bool stt::file::File::chgLine ( const std::string &  data,
const int &  linePos = 0 
)

Modify a line.

Note
1. Modify a line in the file.
2. Line numbers start from 1.
Parameters
dataThe data to overwrite the specified line.
linePosThe line to be overwritten (default is the last line).
Returns
true if the modification is successful.
false if the modification fails.
bool stt::file::File::chgLineC ( const std::string &  data,
const int &  linePos = 0 
)

Modify a line.

Note
1. Modify a line in the data imported from the file into memory.
2. Line numbers start from 1.
Parameters
dataThe data to overwrite the specified line.
linePosThe line to be overwritten (default is the last line).
Returns
true if the modification is successful.
false if the modification fails.
bool stt::file::File::closeFile ( const bool &  del = false)

Close the opened file.

Parameters
deltrue: Delete the file. false: Do not delete the file (default is false).
Returns
true: Operation successful. false: Operation failed.
bool stt::file::File::deleteAll ( )

Delete all lines.

Note
Delete all lines from the file.
Returns
true if the deletion is successful.
false if the deletion fails.
bool stt::file::File::deleteAllC ( )

Delete all lines.

Note
Delete all lines from the data imported from the file into memory.
Returns
true if the deletion is successful.
false if the deletion fails.
bool stt::file::File::deleteLine ( const int &  linePos = 0)

Delete a line.

Note
1. Delete a line from the file.
2. Line numbers start from 1.
Parameters
linePosThe line to be deleted.
Returns
true if the deletion is successful.
false if the deletion fails.
bool stt::file::File::deleteLineC ( const int &  linePos = 0)

Delete a line.

Note
1. Delete a line from the data imported from the file into memory.
2. Line numbers start from 1.
Parameters
linePosThe line to be deleted.
Returns
true if the deletion is successful.
false if the deletion fails.
int stt::file::File::find ( const std::string &  targetString,
const int  linePos = 1 
)

Find a line.

Note
1. Find the first line in the file that contains the target string.
2. Line numbers start from 1.
Parameters
targetStringThe string to be found.
linePosStart searching from the specified line (default is the first line).
Returns
>=1 Returns the line number of the match.
-1 Search failed.
int stt::file::File::findC ( const std::string &  targetString,
const int  linePos = 1 
)

Find a line.

Note
1. Find the first line in the data imported from the file into memory that contains the target string.
2. Line numbers start from 1.
Parameters
targetStringThe string to be found.
linePosStart searching from the specified line (default is the first line).
Returns
>=1 Returns the line number of the match.
-1 Search failed.
void stt::file::File::format ( )

Format the data.

Note
Delete all the data in the file.
void stt::file::File::formatC ( )

Format the data.

Note
Delete all the data imported from the file into memory.
uint64_t stt::file::File::getFileLine ( )
inline

Get the number of lines in the opened file.

Returns
Returns the number of lines in the opened file.
Warning
1. Only when the file is opened in text mode can the correct value be returned.
2. The number of lines obtained is the number of lines when the file was last read into memory.
3. If the file has never been read into memory, the returned value will not be correct.
Note
This function may only be useful when operating on data in memory.
std::string stt::file::File::getFileName ( )
inline

Get the name of the opened file.

Returns
Returns a string representing the name of the opened file.
size_t stt::file::File::getFileSize ( )
inline

Get the size of the file opened in binary mode.

Returns
Returns the size of the file opened in binary mode.
Warning
1. Only when the file is opened in binary mode can the correct value be returned.
2. The size obtained is the size of the file when it was last read into memory.
3. If the file has never been read into memory, the returned value will not be correct.
Note
This function may only be useful when operating on data in memory.
size_t stt::file::File::getSize1 ( )
inline

Get the size of the file opened in binary mode in memory.

Returns
Returns the size of the file opened in binary mode in memory.
Warning
1. Only when the file is opened in binary mode can the correct value be returned.
2. The size obtained is the size of the file currently being operated on in memory.
3. If the file has never been read into memory, the returned value will not be correct.
Note
This function is only useful when operating on data in memory.
bool stt::file::File::isBinary ( )
inline

Check if the object has opened the file in binary mode.

Returns
true: The object has opened the file in binary mode. false: The object has not opened the file in binary mode.
bool stt::file::File::isOpen ( )
inline

Check if the object has opened a file.

Returns
true: The object has opened a file. false: The object has not opened a file.
bool stt::file::File::lockMemory ( )

Read data from disk into memory.

Returns
true if the operation is successful.
false if the operation fails.
bool stt::file::File::openFile ( const std::string &  fileName,
const bool &  create = true,
const int &  multiple = 0,
const size_t &  size = 0,
const mode_t &  mode = 0666 
)

Open a file.

Parameters
fileNameThe name of the file to open (can be an absolute or relative path).
createtrue: Create the file (and directory) if it does not exist. false: Do not create the file if it does not exist (default is true).
multipleWhen >= 1, open the file in binary mode. This value is the ratio of the预定 file space size required for operating the file to the original file size. When < 1, open the file in text mode (default is 0, open in text mode).
sizeWhen the file size is 0 and the binary mode is enabled by the parameter multiple, since 0 multiplied by any number is 0, the parameter multiple will be invalid. In this case, manually enter the预定 file space size (in bytes) (default is 0).
modeIf create is true and the file does not exist, use a bit mask to represent the permissions of the newly created file (default is 0666, rw- rw- rw-).
Returns
true: File opened successfully.
false: Failed to open the file.
Note
Any operation requires opening the file first.
Warning
If the reserved space in binary mode is too small, it may cause a segmentation fault.

Example code 1: Open a file named "test" in the same directory as the program in text mode.

File f;
f.openFile("test");

Example code 2: Open a file named "test" in the same directory as the program in binary mode, intended for read-only.

File f;
f.openFile("test",true,1); // Read-only means reserving the same space as the original file.

Example code 3: Open a file named "test" in the same directory as the program in binary mode, expecting to write data. The size after writing will not exceed twice the original file size.

File f;
f.openFile("test",true,2); // Reserve twice the space.

Example code 4: Open a file named "test" in the same directory as the program in binary mode, expecting to write data. The written size will not exceed 1024 bytes, and the original file size is 0.

File f;
f.openFile("test",true,1,1024); // Since the file size is 0, the third parameter multiple representing the multiple is invalid. Manually specify the size.
std::string& stt::file::File::read ( std::string &  data,
const int &  linePos,
const int &  num 
)

Read multiple lines.

Note
1. Continuously read n lines from the file.
2. Line numbers start from 1.
Parameters
dataThe string container to receive the data.
linePosThe starting position to read.
numThe number of lines to read.
Returns
A reference to the string parameter data.
bool stt::file::File::read ( char *  data,
const size_t &  pos,
const size_t &  size 
)

Read a data block.

Note
1. Read a block of data from the file.
2. Data byte units start from 0.
Parameters
dataThe container to receive the data.
posThe starting position of the data.
sizeThe size of the data block.
Returns
true if the reading is successful.
false if the reading fails.
std::string& stt::file::File::readAll ( std::string &  data)

Read all lines.

Note
Read all lines from the file.
Parameters
dataThe string container to receive the data.
Returns
A reference to the string parameter data.
std::string& stt::file::File::readAllC ( std::string &  data)

Read all lines.

Note
Read all lines from the data imported from the file into memory.
Parameters
dataThe string container to receive the data.
Returns
A reference to the string parameter data.
std::string& stt::file::File::readC ( std::string &  data,
const int &  linePos,
const int &  num 
)

Read multiple lines.

Note
1. Continuously read n lines from the data imported from the file into memory.
2. Line numbers start from 1.
Parameters
dataThe string container to receive the data.
linePosThe starting position to read.
numThe number of lines to read.
Returns
A reference to the string parameter data.
bool stt::file::File::readC ( char *  data,
const size_t &  pos,
const size_t &  size 
)

Read a data block.

Note
1. Read a block of data from the data imported from the file into memory.
2. Data byte units start from 0.
Parameters
dataThe container to receive the data.
posThe starting position of the data.
sizeThe size of the data block.
Returns
true if the reading is successful.
false if the reading fails.
bool stt::file::File::readLine ( std::string &  data,
const int  linePos 
)

Read a single line.

Note
1. Read a single line from the file.
2. Line numbers start from 1.
Parameters
dataThe string container to receive the data.
linePosThe line to be read.
Returns
true if the reading is successful.
false if the reading fails.
bool stt::file::File::readLineC ( std::string &  data,
const int  linePos 
)

Read a single line.

Note
1. Read a single line from the data imported from the file into memory.
2. Line numbers start from 1.
Parameters
dataThe string container to receive the data.
linePosThe line to be read.
Returns
true if the reading is successful.
false if the reading fails.
bool stt::file::File::unlockMemory ( const bool &  rec = false)

Write data from memory to disk.

Parameters
recfalse: Do not roll back the operation and save the operation result to disk. true: Roll back the operation and do not save. (Default is false, no rollback required).
Returns
true if the operation is successful.
false if the operation fails.
Note
If writing to disk fails, the operation will be automatically rolled back.
bool stt::file::File::write ( const char *  data,
const size_t &  pos,
const size_t &  size 
)

Write a data block.

Note
1. Write a block of data into the file.
2. Data byte units start from 0.
Parameters
dataThe container holding the data to be written.
posThe writing position.
sizeThe size of the data block to be written.
Returns
true if the writing is successful.
false if the writing fails.
bool stt::file::File::writeC ( const char *  data,
const size_t &  pos,
const size_t &  size 
)

Write a data block.

Note
1. Write a block of data into the data imported from the file into memory.
2. Data byte units start from 0.
Parameters
dataThe container holding the data to be written.
posThe writing position.
sizeThe size of the data block to be written.
Returns
true if the writing is successful.
false if the writing fails.

Member Data Documentation

std::mutex stt::file::File::che
protected
std::unordered_map<std::string,FileThreadLock> stt::file::File::fl2
staticprotected
std::mutex stt::file::File::l1
staticprotected

The documentation for this class was generated from the following file: