filefinderbyname_web_logo

FileFinderByName C++ library

v3.0.2

Table of contents

Overview

FileFinderByName is C++ library which help to find files by name part(s). It used in different projects to find data files of device files. The library doesn’t have third-party dependencies. The library contains FileFinderByName C++ class and test application. It is CMake project. The library uses C++17 standard (for std::filesystem modules) and compatible with Linux and Windows.

Versions

Table 1 - Library versions.

Version Release date What’s new
1.0.0 06.04.2023 First version
2.0.0 17.05.2023 - Added new method to find device by several parts of name.
- Documentation updated.
3.0.0 16.04.2024 - Change class name to FileFinderByName.
- Documentation updated.
3.0.1 17.04.2024 - Documentation updated.
3.0.1 06.07.2024 - CMake updated.
- Fixed path format in Windows.

Library files

The library is supplied only by source code. The user is given a set of files in the form of a CMake project (repository). The repository structure is shown below:

CMakeLists.txt -------------------- Main CMake file of the library.
src ------------------------------- Folder with library source code.
    FileFinderByName.cpp ---------- C++ implementation file.
    FileFinderByName.h ------------ Library main header file.
    FileFinderByNameVersion.h ----- Header file with library version.
    FileFinderByNameVersion.h.in -- CMake service file to generate version header.
test ------------------------------ Folder of test application.
    CMakeLists.txt ---------------- CMake file of test application.
    main.cpp ---------------------- Source C++ file of test application.

FileFinderByName class description

Class declaration

FileFinderByName interface class declared in FileFinderByName.h file. Class declaration:

namespace cr
{
namespace utils
{
/// File finder class.
class FileFinderByName
{
public:

   /// Get current library version.
   static std::string getVersion();

   /// Find file by name part.
   std::string find(std::string namePart,
                    std::string folder = "/dev/serial/by-id");

   /// Find file by two or more parts of name.
   std::string find(std::vector<std::string> nameParts,
                    std::string folder = "/dev/serial/by-id");

};
}
}

getVersion method

The getVersion() method return string of current version of FileFinderByName class. Method declaration:

static std::string getVersion();

Method can be used without Lens class instance:

cout << "FileFinderByName class version: " << FileFinderByName::getVersion() << endl;

Console output:

FileFinderByName class version: 3.0.2

find method

The find(…) intended to find full name of file. The method searches file in particular folder. The method accept file name part (e.g. “Hitachi”, “Fuju” etc.). The method has two overloads. Method declaration:

std::string find(std::string namePart, std::string folder = "/dev/serial/by-id");
Parameter Value
namePart Part of file name.
folder Folder to find file.

Returns: the method returns full file name if it was found or returns ”“ if no files which include “namePart” or if folder doesn’t exists. The method will return only first file found which satisfied by search criteria.

std::string find(std::vector<std::string> nameParts, std::string folder = "/dev/serial/by-id");
Parameter Value
nameParts List of name parts. To be found file must have all parts in it’s name.
folder Folder to find file.

Returns: the method returns full file name if it was found or returns ”“ if no files which include “namePart” or if folder doesn’t exists. The method will return only first file found which satisfied by search criteria.

Example

Example shows how to use file finder by both ways: by one part of name or by several parts of name.

#include <iostream>
#include "FileFinderByName.h"

using namespace std;
using namespace cr::utils;

int main(void)
{
    cout << "File finder v" << FileFinderByName::getVersion() << " test" << endl;

    // Set name part.
    string namePart = "";
    cout << "Set file name part: ";
    cin >> namePart;

    // Set folder.
    string folder = "/dev";
    cout << "Set folder: ";
    cin >> folder;

    // Find file.
    FileFinderByName finder;
    string file = finder.find(namePart, folder);
    if (file == "")
        cout << "Can't find file" << endl;
    else
        cout << "File: " << file << endl;

    // Find file by two parts of name (can be different).
    file = finder.find(vector<string>{namePart, namePart}, folder);
    if (file == "")
        cout << "Can't find file" << endl;
    else
        cout << "File: " << file << endl;

    return 1;
}