DataReelDataReelC++ Transition Guide

Examples › General Examples

general_file_utils

C++17 or laterC++ standard library onlyPortable C++ (tested on Linux)

Legacy: examples_general/file_utils/testprog.cpp · Modern: examples_modern/general/file_utils/modern.cpp

Modern equivalent of the file utilities test. C++17 <filesystem> replaces the futils_* functions: directory_iterator for opendir/readdir/rewinddir/closedir, create_directory, exists, is_directory, current_path for getcwd and chdir, remove for rmdir, and path::root_name() for the drive letter on Windows. Errors are reported through std::error_code (or std::filesystem_error exceptions), and a path holds the right character type on each platform.

Legacy calls in this program

LegacyModern C++StandardNotes
futils_*(), df_mkdir(), df_copy() ...std::filesystem (directory_iterator, create_directory, file_size, copy_file, rename, remove, current_path)C++17Errors through std::error_code or std::filesystem_error.

From the transition tables.

Side by side

DataReel 6.4 (legacy)

examples_general/file_utils/testprog.cpp 143 lines
#include "gxdlcode.h"

#if defined (__USE_ANSI_CPP__) // Use the ANSI Standard C++ library
#include <iostream>
using namespace std; // Use unqualified names for Standard C++ library
#else // Use the old iostream library by default
#include <iostream.h>
#endif // __USE_ANSI_CPP__

#include "futils.h"

#ifdef __MSVC_DEBUG__
#include "leaktest.h"
#endif

void PausePrg() 
{
  cout << "\n";
  cout << "Press enter to continue..." << "\n";
  cin.get();
}

void DirectoryFunctionTest()
{
  cout << "Testing general purpose directory functions" 
       << "\n";

  const char *dirname = "testdir"; // Directory to be created

  // Directory immediately above the current directory position
  const char *parentdir = "..";    
  
  cout << "Creating " << dirname << " directory" <<"\n";
  if(futils_mkdir(dirname) != 0) {
    cout << "Error creating the directory" << "\n";
    return;
  }

  if((futils_exists(dirname)) && (futils_isdirectory(dirname))) {
    cout << "The " << dirname << " directory was created" << "\n";
  }

  cout << "Changing directory to " << dirname << " directory" <<"\n";
  if(futils_chdir(dirname) != 0) {
    cout << "Error changing directory" << "\n";
    return;
  }

  char *pwd[futils_MAX_DIR_LENGTH]; char drive_letter;
  if(futils_getcwd((char *)pwd, futils_MAX_DIR_LENGTH) == 0) {
    cout << "Present working directory: " << (char *)pwd << "\n";
    if(futils_hasdriveletter((const char *)pwd, &drive_letter)) {
      cout << "DOS Drive letter = " << drive_letter << "\n";
    }
  }

  cout << "Changing directory to " << parentdir << " directory" <<"\n";
  if(futils_chdir(parentdir) != 0) {
    cout << "Error changing directory" << "\n";
    return;
  }

  if(futils_getcwd((char *)pwd, futils_MAX_DIR_LENGTH) == 0) {
    cout << "Present working directory: " << (char *)pwd << "\n";
    if(futils_hasdriveletter((const char *)pwd, &drive_letter)) {
      cout << "DOS Drive letter = " << drive_letter << "\n";
    }
  }

  cout << "Removing " << dirname << " directory" <<"\n";
  if(futils_rmdir(dirname) != 0) {
    cout << "Error removing directory" << "\n";
    return;
  }
}

int main(int argc, char *argv[])
{
#ifdef __MSVC_DEBUG__
  InitLeakTest();
#endif

  DIR* dirp;
  dirent* direntp;
  char *directory = argv[1]; 

  if(argc < 2) {
    cout << "Usage: " << argv[0] << " directory" << "\n";
#if defined (__WIN32__)
    cout << "Example: " << argv[0] << " C:\\temp" << "\n";
#elif defined (__UNIX__)
    cout << "Example: " << argv[0] << " /tmp" << "\n";
#else
#error You must define a target platform: __WIN32__ or __UNIX__
#endif
    return 1;
  }

  // Test the general-purpose directory stream functions  
  cout << "Testing general purpose directory stream functions" << "\n";
  cout << "Opening the specified directory and list all files" << "\n";
  cout << "\n";
  dirp = futils_opendir(directory);
  if(dirp == NULL) {
    cout << "Cannot open " << directory << "\n";
    return 1;
  } 
  else {
    for(;;) {
      dirent entry;
      direntp = futils_readdir(dirp, &entry);
      if(direntp == NULL) break;
      cout << direntp->d_name << "\n" << flush;
    }
  }
  cout << "\n";

  PausePrg();
  
  cout << "Testing the rewind function" << "\n";
  cout << "\n";
  futils_rewinddir(dirp);
  for(;;) {
    dirent entry;
    direntp = futils_readdir(dirp, &entry);
    if(direntp == NULL) break;
    cout << direntp->d_name << "\n" << flush;
  }
  cout << "\n";

  futils_closedir(dirp);

  PausePrg();
  
  // Test the general-purpose directory functions  
  DirectoryFunctionTest();

  return 0;
}
// ----------------------------------------------------------- //
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //

Modern C++ (C++17)

examples_modern/general/file_utils/modern.cpp 61 lines
#include <filesystem>
#include <iostream>
#include <system_error>

namespace fs = std::filesystem;

void list_directory(const fs::path &dir)
{
  std::error_code ec;
  for(const auto &entry : fs::directory_iterator(dir, ec))
    std::cout << entry.path().filename().string()
              << (entry.is_directory() ? "/" : "") << "\n";
  if(ec) std::cout << "Cannot open " << dir << ": " << ec.message() << "\n";
}

void directory_function_test()
{
  std::cout << "Testing general purpose directory functions\n";
  const fs::path dirname = "testdir";
  std::error_code ec;

  std::cout << "Creating " << dirname << " directory\n";
  if(!fs::create_directory(dirname, ec) && ec) {
    std::cout << "Error creating the directory: " << ec.message() << "\n";
    return;
  }
  if(fs::exists(dirname) && fs::is_directory(dirname))
    std::cout << "The " << dirname << " directory was created\n";

  const fs::path start = fs::current_path();
  fs::current_path(dirname, ec);                      // chdir
  if(ec) { std::cout << "Error changing directory\n"; return; }
  fs::path pwd = fs::current_path();                  // getcwd
  std::cout << "Present working directory: " << pwd.string() << "\n";
  if(pwd.has_root_name())                             // Drive letter
    std::cout << "Root name (drive) = " << pwd.root_name().string() << "\n";

  fs::current_path(start);                            // chdir ..
  std::cout << "Present working directory: " << fs::current_path().string()
            << "\n";

  std::cout << "Removing " << dirname << " directory\n";
  if(!fs::remove(dirname, ec))                        // rmdir
    std::cout << "Error removing directory: " << ec.message() << "\n";
}

int main(int argc, char *argv[])
{
  fs::path directory = argc > 1 ? fs::path(argv[1]) : fs::current_path();
  std::cout << "Listing " << directory.string() << "\n\n";
  list_directory(directory);
  std::cout << "\nListing it again (no rewinddir: a new iterator)\n\n";
  list_directory(directory);
  std::cout << "\n";
  directory_function_test();
  return 0;
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //

Build and run

cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=17
cmake --build build-modern --target general_file_utils
./build-modern/general_file_utils
# or without CMake:
g++ -std=c++17 -pthread -o general_file_utils examples_modern/general/file_utils/modern.cpp

Output of the modern program

From run_modern_tests.sh on RHEL 10, GCC 14, C++20.

Listing build-modern/run

certs/
database_btree.log
database_btree_cached.log
database_btree_node.log
database_damaged.log
database_database.log
database_database_cache.log
database_defrag.log
database_fuzz_fuzz_db.log
database_fuzz_gen_seeds.log
modern_seeds/
database_infohog.log
database_journal.log
database_journal_salt.log
database_layout.log
database_pod.log
database_reclaim.log
database_replication_6x.log
database_simple.log
database_vector_index.log
general_binary_search_tree.log
general_config_class.log
general_device_cache.log
general_disk_file_class.log
general_doubly_linked_list.log
general_file_utils.log

Listing it again (no rewinddir: a new iterator)

certs/
database_btree.log
database_btree_cached.log
database_btree_node.log
database_damaged.log
database_database.log
database_database_cache.log
database_defrag.log
database_fuzz_fuzz_db.log
database_fuzz_gen_seeds.log
modern_seeds/
database_infohog.log
database_journal.log
database_journal_salt.log
database_layout.log
database_pod.log
database_reclaim.log
database_replication_6x.log
database_simple.log
database_vector_index.log
general_binary_search_tree.log
general_config_class.log
general_device_cache.log
general_disk_file_class.log
general_doubly_linked_list.log
general_file_utils.log

Testing general purpose directory functions
Creating "testdir" directory
The "testdir" directory was created
Present working directory: build-modern/run/testdir
Present working directory: build-modern/run
Removing "testdir" directory