freecplus framework - directory operation

1, Source code description

Freecplus is a C/C + + open source framework under Linux system. Please go to the C language technology network (www.freecplus.net) to download the source code.

This article introduces the directory operation functions and classes of freecplus framework.

The declaration file for functions and classes is freecplus / 65124; freecplus. H.

The definition file for functions and classes is freecplus / 65124; freecplus.cpp.

The sample program is located in the freecplus/demo directory.

The compile rule file is freecplus/demo/makefile.

2, Create directory

Under Linux, if you want to create a file of "/ tmp/aaa/bbb/ccc/ddd/data.xml", you must first create "/ tmp/aaa/bbb/ccc/ddd". The steps are as follows:

1) If the '/ tmp' directory does not exist, create '/ tmp'.

2) If the "/ tmp/aaa" directory does not exist, create "/ tmp/aaa".

3) If the "/ tmp/aaa/bbb" directory does not exist, create "/ tmp/aaa/bbb".

4) If the "/ tmp/aaa/bbb/ccc" directory does not exist, create "/ tmp/aaa/bbb/ccc".

5) If the "/ tmp/aaa/bbb/ccc/ddd" directory does not exist, create "/ tmp/aaa/bbb/ccc/ddd".

6) Create the file "/ tmp/aaa/bbb/ccc/ddd/data.xml".

Although these operations are not very technical, they are also very annoying.

MKDIR function creates a directory step by step according to the file name or directory name of the absolute path.

Function declaration:

bool MKDIR(const char *pathorfilename,bool bisfilename=true);

Parameter Description:

pathorfilename: the filename or directory name of the absolute path.

bisfilename: indicates the type of pathorfilename. True pathorfilename is the file name. Otherwise, it is the directory name. The default value is true.

Return value: true - creation succeeded, false - creation failed. If the return fails, there are about three reasons: 1) insufficient permissions; 2) the pathorfilename parameter is not a legal file name or directory name; 3) insufficient disk space.

Example (demo30.cpp)

/*
 *  Program name: demo30.cpp, this program demonstrates the use of MKDIR function to create directories step by step according to the absolute path file name or directory name in freecplus framework.
 *  Author: C language technology network (www.freecplus.net) date: 20190525
*/
#include "../_freecplus.h"

int main()
{
  MKDIR("/tmp/aaa/bbb/ccc/ddd",false);   // Create the "/ tmp/aaa/bbb/ccc/ddd" directory.

  MKDIR("/tmp/111/222/333/444/data.xml",true);   // Create the "/ tmp/111/222/333/444" directory.
}

3, Get file information under directory

The freecplus framework encapsulates the function of obtaining the file list information in a directory and its subdirectories into a CDir class.

Class declaration:

// Gets the file list information in a directory and its subdirectories.
class CDir
{
public:
  char m_DirName[301];        // Directory name, for example, / tmp/root.
  char m_FileName[301];       // The file name, excluding the directory name, for example: data.xml.
  char m_FullFileName[301];   // The full name of the file, including the directory name, for example, / tmp/root/data.xml.
  int  m_FileSize;              // The size of the file in bytes.
  char m_ModifyTime[21];      // The last time the file was modified, that is, the st [mtime] member of the stat structure.
  char m_CreateTime[21];      // The time the file was generated, that is, the St CTime member of the stat structure.
  char m_AccessTime[21];      // The last time the file was accessed, that is, the st atime member of the stat structure.
  char m_DateFMT[21];         // The file time display format is set by SetDateFMT method.

  vector<string> m_vFileName; // Stores the list of file names (full file names, including directory names) obtained by the OpenDir method.
  int m_pos;                  // The location of the m_vFileName container has been read. Add 1 to m_pos of the ReadDir method every time it is called.

  CDir();  // Constructor.

  void initdata(); // Initialize member variables.

  // Set the format of file time, support "yyyy MM DD hh24: Mi: SS" and "yyyymmddh24miss", the default is the former.
  void SetDateFMT(const char *in_DateFMT);

  // Open the directory, obtain the file list information in the directory, and store it in the m ﹣ vfilename container.
  // in_DirName, the name of the directory to be opened, using an absolute path, such as / tmp/root.
  // In ﹣ matchstr, the matching rule of the file name to be obtained, and the unmatched file is ignored.
  // In ﹐ maxcount, get the maximum number of files. The default value is 10000.
  // Baddchild, whether to open subdirectories at all levels. The default value is false - do not open subdirectories.
  // bSort, whether to sort the obtained file list (i.e. the content in the m ﹤ vfilename container). The default value is false - no sorting.
  // Return value: if the directory specified in the in dirname parameter does not exist, the OpenDir method will create the directory. If the creation fails, it will return false. In addition, if the current user does not have read permission to the subdirectory under the in dirname directory, it will also return false. In other normal cases, it will return true.
  bool OpenDir(const char *in_DirName,const char *in_MatchStr,const unsigned int in_MaxCount=10000,const bool bAndChild=false,bool bSort=false);

  // This is a recursive function for OpenDir() calls that do not need to be called outside the CDir class.
  bool _OpenDir(const char *in_DirName,const char *in_MatchStr,const unsigned int in_MaxCount,const bool bAndChild);

  // Get a record (file name) from the m ﹣ vfilename container, and get the file size, modification time and other information.
  // When the OpenDir method is called, the m ﹣ vfilename container is emptied, and m ﹣ POS is set to zero. Every time the ReadDir method m ﹣ POS is called, 1 is added.
  // When m_pos is less than m_vFileName.size(), return true, otherwise return false.
  bool ReadDir();

  ~CDir();  // Destructor.
};

The member variables and function descriptions of the CDir class are described in detail in the class declaration.

We demonstrate the use of the CDir class through an application scenario.

First execute the following script to generate the test directory and file.

mkdir /tmp/root
mkdir /tmp/root/aaa
mkdir /tmp/root/bbb
cd freecplus
cp freecplus.* /tmp/root/.
cp demo/demo1* /tmp/root/aaa/.
cp demo/demo2* /tmp/root/bbb/.
cp demo/makefile /tmp/root/bbb/.

After executing the above script, the directory and file list in the / tmp/root directory are as follows:

Example (demo32.cpp)

/*
 *  Program name: demo32.cpp. This program demonstrates that the CDir class is used to obtain the file list information in a directory and its subdirectories in the freecplus framework.
 *  Author: C language technology network (www.freecplus.net) date: 20190525
*/
#include "../_freecplus.h"

int main()
{
  CDir Dir;

  if (Dir.OpenDir("/tmp/root","*.h,*cpp",100,true,true)==false)
  {
    printf("Dir.OpenDir(/tmp/root) failed.\n"); return -1;
  }

  while(Dir.ReadDir()==true)
  {
    printf("filename=%s,mtime=%s,size=%d\n",Dir.m_FullFileName,Dir.m_ModifyTime,Dir.m_FileSize);
  }
}

Operation effect

matters needing attention:

1) Every time the ReadDir method is called, the values of other member variables of the CDir class are output to the screen to observe the operation effect.

2) The in ﹣ matchstr parameter of OpenDir method is very important and widely used in practical development.

3) If the filename begins with a dot ".", the OpenDir method will not read these files. If you want to read these files, you can modify the source code in the OpenDir method.

4) The in_MaxCount parameter of OpendDir method sets the number of files in the directory to be scanned each time. It is recommended not to exceed 10000. If the value is too large, the time to open the directory will be longer and more memory will be consumed.

5) The bSort parameter of OpenDir sets whether to sort the m ﹣ vfilename container. Sorting will consume resources and time, and can be done without sorting.

4, Copyright notice

C language technology net original article, reprint please explain the article source, the author and the original link.
Source: C language technology network (www.freecplus.net)
Author: Manon Youdao

If this article is helpful to you, please like support or forward my article in your blog, thank you!!!
If there is a mistake in the article, or there is a mistake in the content, or other suggestions and opinions, please leave a message for correction, thank you very much!!!

Keywords: C++ xml C network Linux

Added by GrexP on Mon, 20 Apr 2020 19:45:17 +0300