BlackLib::BlackDirectory Class Reference

Interacts with end user, to use directory system of Linux based OS. More...

#include <BlackDirectory.h>

Public Types

enum  hiddenFormat {
  ExcludeHiddens = 0,
  IncludeHiddens = 1
}
 
enum  componentFormat {
  Directory = 1,
  File = 2,
  SymbolicLink = 4,
  All = Directory|File|SymbolicLink
}
 

Public Member Functions

 BlackDirectory ()
 Constructor of BlackDirectory class. More...
 
 BlackDirectory (std::string path)
 Constructor of BlackDirectory class. More...
 
virtual ~BlackDirectory ()
 Destructor of BlackDirectory class. More...
 
std::vector< std::string > getEntryList (BlackDirectory::hiddenFormat hf=BlackDirectory::ExcludeHiddens, BlackDirectory::componentFormat cf=BlackDirectory::All, bool sortAlphabetically=true)
 Exports the directories, files and sybollic links list in current directory. More...
 
unsigned int getEntryCount (BlackDirectory::hiddenFormat hf=BlackDirectory::ExcludeHiddens, BlackDirectory::componentFormat cf=BlackDirectory::All)
 Exports the directories, files and sybollic links count in current directory. More...
 
std::string getApplicationDirectoryPath ()
 Exports the application directory path. More...
 
std::string getApplicationDirectoryName ()
 Exports the application directory name. More...
 

Static Public Member Functions

static std::string getHomeDirectoryPath ()
 Exports "home" directory path. More...
 
static std::string getCurrentUserName ()
 Exports username. More...
 
static std::string getCurrentDirectoryPath ()
 Exports current directory path. More...
 
static std::string getCurrentDirectoryName ()
 Exports current directory name. More...
 
static long int getCurrentDirectorySize ()
 Exports current directory size in bytes. More...
 
static long int getDirectorySize (std::string directoryPath)
 Exports directory size in bytes. More...
 
static bool exists (std::string directoryPath)
 Checks directory existence. More...
 
static bool makeDirectory (std::string directoryPath)
 Creates new directory. More...
 
static bool removeDirectory (std::string directoryPath, bool rmRecursively=false)
 Removes directory. More...
 
static bool renameDirectory (std::string oldDirectoryPath, std::string newDirectoryPath)
 Renames directory. More...
 
static bool cd (std::string path)
 Changes working directory. More...
 
static bool cdUp ()
 Changes working directory to up directory. More...
 
static bool isRoot ()
 Checks working directory is root or not. More...
 
static std::vector< std::string > getEntryList (std::string directoryPath, BlackDirectory::hiddenFormat hf=BlackDirectory::ExcludeHiddens, BlackDirectory::componentFormat cf=BlackDirectory::All, bool sortAlphabetically=true)
 Exports the directories, files and sybollic links list in directory. More...
 
static unsigned int getEntryCount (std::string directoryPath, BlackDirectory::hiddenFormat hf=BlackDirectory::ExcludeHiddens, BlackDirectory::componentFormat cf=BlackDirectory::All)
 Exports the directories, files and sybollic links count in directory. More...
 

Private Attributes

std::string applicationDirectory
 is used to hold the application directory More...
 

Detailed Description

Interacts with end user, to use directory system of Linux based OS.

This class is end node to use directory system. End users interact with the directories from this class. It includes public functions for managing directories.

Example
EXAMPLE PROJECT FILE TREE:

   myDirectoryProject
   |-> src
       |-> BlackLib
           |-> BlackDirectory
               |-> BlackDirectory.cpp
               |-> BlackDirectory.h
           |-> BlackThread
               |-> BlackThread.cpp
               |-> BlackThread.h
           |-> BlackMutex
               |-> BlackMutex.cpp
               |-> BlackMutex.h
           |-> BlackTime
               |-> BlackTime.cpp
               |-> BlackTime.h
           |-> BlackADC
               |-> BlackADC.cpp
               |-> BlackADC.h
           |-> BlackGPIO
               |-> BlackGPIO.cpp
               |-> BlackGPIO.h
           |-> BlackI2C
               |-> BlackI2C.cpp
               |-> BlackI2C.h
           |-> BlackPWM
               |-> BlackPWM.cpp
               |-> BlackPWM.h
           |-> BlackSPI
               |-> BlackSPI.cpp
               |-> BlackSPI.h
           |-> BlackUART
               |-> BlackUART.cpp
               |-> BlackUART.h
           |-> BlackCore.cpp
           |-> BlackCore.h
           |-> BlackDef.h
           |-> BlackErr.h
           |-> BlackLib.h
       |-> myDirectoryProject.cpp


If BlackLib source files are located in your project like above example project file tree, you have to include BlackDirectory.h or another source files with adding this line to your project file (myDirectoryProject.cpp at the example):
#include "BlackLib/BlackDirectory/BlackDirectory.h"


If BlackLib source files are located at same level with your project file (myDirectoryProject.cpp at the example), you have to include BlackDirectory.h or another source files with adding this line to your project file:

// Filename: myDirectoryProject.cpp
// Author: Yiğit Yüce - ygtyce@gmail.com
#include <iostream>
#include "BlackLib/BlackDirectory/BlackDirectory.h"
int main()
{
BlackLib::BlackDirectory myDir("/home/root");
std::cout << myDir.getApplicationDirectoryPath();
return 0;
}


You can use "using namespace BlackLib" also. You can get rid of writing "BlackLib::", with using this method.
// Filename: myDirectoryProject.cpp
// Author: Yiğit Yüce - ygtyce@gmail.com
#include <iostream>
#include "BlackLib/BlackDirectory/BlackDirectory.h"
using namespace BlackLib;
int main()
{
BlackDirectory myDir("/home/root");
std::cout << myDir.getApplicationDirectoryPath();
return 0;
}

Member Enumeration Documentation

This enum is used to define directory components showing format.

Enumerator
Directory 

enumeration for showing directories at the output

File 

enumeration for showing files at the output

SymbolicLink 

enumeration for showing symbolic links at the output

All 

enumeration for showing all type components at the output

This enum is used to define hidden files showing format.

Enumerator
ExcludeHiddens 

enumeration for excluding hidden files from output

IncludeHiddens 

enumeration for including hidden files from output

Constructor & Destructor Documentation

BlackLib::BlackDirectory::BlackDirectory ( )

Constructor of BlackDirectory class.

This function initializes application path.

Example
std::cout << myDir.getApplicationDirectoryPath() << std::endl;
std::cout << myDirPtr->getApplicationDirectoryPath();
See also
getCurrentDirectoryPath()
BlackLib::BlackDirectory::BlackDirectory ( std::string  path)

Constructor of BlackDirectory class.

This function initializes application path and changes directory(cd).

Parameters
[in]pathdestination directory name
Example
BlackLib::BlackDirectory myDir("/home/root");
BlackLib::BlackDirectory *myDirPtr = new BlackLib::BlackDirectory("/home/root");
std::cout << myDir.getApplicationDirectoryPath() << std::endl;
std::cout << myDirPtr->getApplicationDirectoryPath();
See also
getCurrentDirectoryPath()
cd()
BlackLib::BlackDirectory::~BlackDirectory ( )
virtual

Destructor of BlackDirectory class.

Member Function Documentation

bool BlackLib::BlackDirectory::cd ( std::string  path)
static

Changes working directory.

Parameters
[in]pathdestination path
Returns
True if working directory is changed else false.
Example
BlackLib::BlackDirectory myDir("/sys/class/");
std::cout << "Changed?: " << std::boolalpha << myDir.cd("/tmp/dir1") << std::endl;
std::cout << "Current dir path: " << BlackLib::BlackDirectory::getCurrentDirectoryPath() << std::endl;
std::cout << "Changed?: " << std::boolalpha << BlackLib::BlackDirectory::cd("/tmp/dir2") << std::endl;
std::cout << "Current dir path: " << BlackLib::BlackDirectory::getCurrentDirectoryPath() << std::endl;
// Possible Output:
// Changed?: True
// Current dir path: /tmp/dir1
// Changed?: True
// Current dir path: /tmp/dir2
bool BlackLib::BlackDirectory::cdUp ( )
static

Changes working directory to up directory.

Returns
True if working directory is changed else false.
Example
BlackLib::BlackDirectory myDir("/sys/class/");
std::cout << "Changed?: " << std::boolalpha << myDir.cdUp() << std::endl;
std::cout << "Current dir path: " << BlackLib::BlackDirectory::getCurrentDirectoryPath() << std::endl;
std::cout << "Changed?: " << std::boolalpha << BlackLib::BlackDirectory::cdUp() << std::endl;
std::cout << "Current dir path: " << BlackLib::BlackDirectory::getCurrentDirectoryPath() << std::endl;
// Possible Output:
// Changed?: True
// Current dir path: /sys
// Changed?: True
// Current dir path: /
bool BlackLib::BlackDirectory::exists ( std::string  directoryPath)
static

Checks directory existence.

Parameters
[in]directoryPathdestination directory path
Returns
True if directory exists else false.
Example
BlackLib::BlackDirectory myDir("/sys/class/");
std::cout << "Exists?: " << std::boolalpha << myDir.exists("/home/root/Desktop") << std::endl;
std::cout << "Exists?: " << std::boolalpha << BlackLib::BlackDirectory::exists("/home/root/Desktop") << std::endl;
// Possible Output:
// Exists?: True
// Exists?: True
std::string BlackLib::BlackDirectory::getApplicationDirectoryName ( )

Exports the application directory name.

Returns
the application directory name.
Example
BlackLib::BlackDirectory myDir("/sys/");
std::cout << "App dir name: " << myDir.getApplicationDirectoryName() << std::endl;
// Possible Output:
// App dir name: project1
std::string BlackLib::BlackDirectory::getApplicationDirectoryPath ( )

Exports the application directory path.

Returns
the application directory path.
Example
BlackLib::BlackDirectory myDir("/sys/");
std::cout << "App dir path: " << myDir.getApplicationDirectoryPath() << std::endl;
// Possible Output:
// App dir path: /home/root/Desktop/project1
std::string BlackLib::BlackDirectory::getCurrentDirectoryName ( )
static

Exports current directory name.

Returns
Current directory name or empty string.
Example
std::cout << "Current dir name: " << BlackLib::BlackDirectory::getCurrentDirectoryName() << std::endl;
BlackLib::BlackDirectory myDir("/sys/class/");
std::cout << "Current dir name: " << myDir.getCurrentDirectoryName() << std::endl;
// Possible Output:
// Current dir name: project1
// Current dir name: class
std::string BlackLib::BlackDirectory::getCurrentDirectoryPath ( )
static

Exports current directory path.

Returns
Current directory path or BlackLib::CURRENT_DIRECTORY_NOT_FOUND string.
Example
std::cout << "Current dir path: " << BlackLib::BlackDirectory::getCurrentDirectoryPath() << std::endl;
BlackLib::BlackDirectory myDir("/sys/class/");
std::cout << "Current dir path: " << myDir.getCurrentDirectoryPath() << std::endl;
// Possible Output:
// Current dir path: /home/root/Desktop/project1
// Current dir path: /sys/class
long int BlackLib::BlackDirectory::getCurrentDirectorySize ( )
static

Exports current directory size in bytes.

Returns
Current directory size in bytes or 0.
Example
std::cout << "Current dir size: " << BlackLib::BlackDirectory::getCurrentDirectorySize() << std::endl;
BlackLib::BlackDirectory myDir("/sys/class/");
std::cout << "Current dir size: " << myDir.getCurrentDirectorySize() << std::endl;
// Possible Output:
// Current dir size: 41
// Current dir size: 1
std::string BlackLib::BlackDirectory::getCurrentUserName ( )
static

Exports username.

Returns
Current username which logged in to OS or empty string.
Example
std::cout << "My username: " << myDir.getCurrentUserName() << std::endl;
std::cout << "My username: " << BlackLib::BlackDirectory::getCurrentUserName() << std::endl;
// Possible Output:
// My username: root
// My username: root
long int BlackLib::BlackDirectory::getDirectorySize ( std::string  directoryPath)
static

Exports directory size in bytes.

Parameters
[in]directoryPathdestination directory path
Returns
Specified directory size in bytes or 0.
Example
BlackLib::BlackDirectory myDir("/sys/class/");
std::cout << "Dir size: " << myDir.getDirectorySize("/home/root/Desktop") << std::endl;
std::cout << "Dir size: " << BlackLib::BlackDirectory::getDirectorySize("/home/root/Desktop") << std::endl;
// Possible Output:
// Dir size: 1
// Dir size: 1
unsigned int BlackLib::BlackDirectory::getEntryCount ( std::string  directoryPath,
BlackDirectory::hiddenFormat  hf = BlackDirectory::ExcludeHiddens,
BlackDirectory::componentFormat  cf = BlackDirectory::All 
)
static

Exports the directories, files and sybollic links count in directory.

Users can hide or get hidden files with second paramter of function or select which components are added to count with third parameter of this function. The default values of this function are "exclude hidden files" and "show all files". It does not add to count "." and ".." never.

Parameters
[in]directoryPathdestination path
[in]hfhidden files shown format
[in]cfselection file formats to show
Returns
The count of directory's components.
Example
BlackLib::BlackDirectory myDir("/sys/class");
std::cout << "Current dir path: " << myDir.getCurrentDirectoryPath() << std::endl << std::endl;
std::cout << "The directory \"/sys\" contains " << myDir->getEntryCount("/sys") << " element(s)." << std::endl;
// Possible Output:
// Current dir path: /sys/class
//
// The directory "/sys" contains 10 element(s).
unsigned int BlackLib::BlackDirectory::getEntryCount ( BlackDirectory::hiddenFormat  hf = BlackDirectory::ExcludeHiddens,
BlackDirectory::componentFormat  cf = BlackDirectory::All 
)

Exports the directories, files and sybollic links count in current directory.

Users can hide or get hidden files with second paramter of function or select which components are added to count with third parameter of this function. The default values of this function are "exclude hidden files" and "show all files". It does not add to count "." and ".." never.

Parameters
[in]hfhidden files shown format
[in]cfselection file formats to show
Returns
The count of current directory's components.
Example
BlackLib::BlackDirectory myDir("/sys/");
std::cout << "Current dir path: " << myDir.getCurrentDirectoryPath() << std::endl << std::endl;
std::cout << "This directory contains " << myDir->getEntryCount() << " element(s)." << std::endl;
// Possible Output:
// Current dir path: /sys
//
// This directory "/sys" contains 10 element(s).
std::vector< std::string > BlackLib::BlackDirectory::getEntryList ( std::string  directoryPath,
BlackDirectory::hiddenFormat  hf = BlackDirectory::ExcludeHiddens,
BlackDirectory::componentFormat  cf = BlackDirectory::All,
bool  sortAlphabetically = true 
)
static

Exports the directories, files and sybollic links list in directory.

Users can hide or get hidden files with second paramter of function or select which components are shown in output with third parameter of this function. Also they can sort the output by alphabetically with forth parameter. The default values of this function are "exclude hidden files", "show all files" and "sort alphabetically". It does not show "." and ".." never.

Parameters
[in]directoryPathdestination path
[in]hfhidden files shown format
[in]cfselection file formats to show
[in]sortAlphabeticallysort alphabetically the output
Returns
The list of directory's components.
Example
BlackLib::BlackDirectory myDir("/sys/class");
std::vector<std::string> ls = myDir->getEntryList("/sys",
true);
std::cout << "Current dir path: " << myDir.getCurrentDirectoryPath() << std::endl << std::endl;
std::cout << "Output of \'ls -a /sys/\' command:" << std::endl;
for( unsigned int i = 0 ; i <ls.size() ; i++ )
{
std::cout << ls[i] << std::endl;
}
// Possible Output:
// Current dir path: /sys/class
//
// Output of 'ls -a /sys/' command:
// block
// bus
// class
// dev
// devices
// firmware
// fs
// kernel
// module
// power
std::vector< std::string > BlackLib::BlackDirectory::getEntryList ( BlackDirectory::hiddenFormat  hf = BlackDirectory::ExcludeHiddens,
BlackDirectory::componentFormat  cf = BlackDirectory::All,
bool  sortAlphabetically = true 
)

Exports the directories, files and sybollic links list in current directory.

Users can hide or get hidden files with second paramter of function or select which components are shown in output with third parameter of this function. Also they can sort the output by alphabetically with forth parameter. The default values of this function are "exclude hidden files", "show all files" and "sort alphabetically". It does not show "." and ".." never.

Parameters
[in]hfhidden files shown format
[in]cfselection file formats to show
[in]sortAlphabeticallysort alphabetically the output
Returns
The list of current directory's components.
Example
std::vector<std::string> ls = myDir->getEntryList(BlackLib::BlackDirectory::IncludeHiddens,
true);
std::cout << "Current dir path: " << myDir.getCurrentDirectoryPath() << std::endl << std::endl;
std::cout << "Output of \'ls -a\' command:" << std::endl;
for( unsigned int i = 0 ; i <ls.size() ; i++ )
{
std::cout << ls[i] << std::endl;
}
// Possible Output:
// Current dir path: /sys/
//
// Output of 'ls -a' command:
// block
// bus
// class
// dev
// devices
// firmware
// fs
// kernel
// module
// power
std::string BlackLib::BlackDirectory::getHomeDirectoryPath ( )
static

Exports "home" directory path.

Returns
User specific "home" directory path or empty string.
Example
std::cout << "My home dir: " << myDir.getHomeDirectoryPath() << std::endl;
std::cout << "My home dir: " << BlackLib::BlackDirectory::getHomeDirectoryPath() << std::endl;
// Possible Output:
// My home dir: /home/root
// My home dir: /home/root
bool BlackLib::BlackDirectory::isRoot ( )
static

Checks working directory is root or not.

Returns
True if working directory is root else false.
Example
BlackLib::BlackDirectory myDir("/sys/");
std::cout << "IsRoot?: " << std::boolalpha << myDir.isRoot() << std::endl;
myDir.cdUp();
std::cout << "IsRoot?: " << std::boolalpha << BlackLib::BlackDirectory::isRoot() << std::endl;
// Possible Output:
// IsRoot?: False
// IsRoot?: True
bool BlackLib::BlackDirectory::makeDirectory ( std::string  directoryPath)
static

Creates new directory.

Parameters
[in]directoryPathnew directory path or name
Returns
True if directory is created else false.
Example
BlackLib::BlackDirectory myDir("/sys/class/");
std::cout << "Created?: " << std::boolalpha << myDir.makeDirectory("/tmp/dir1") << std::endl;
std::cout << "Created?: " << std::boolalpha << BlackLib::BlackDirectory::makeDirectory("/tmp/dir2") << std::endl;
// Possible Output:
// Created?: True
// Created?: True
bool BlackLib::BlackDirectory::removeDirectory ( std::string  directoryPath,
bool  rmRecursively = false 
)
static

Removes directory.

If second parameter is set to false, this function tries to remove directory. If directory is empty, this operation will succes else fail. If second parameter is set to true, it tries to remove directory, files and subdirectories. If directory cannot be removed, it keeps going and attempts to delete as many files and sub-directories as possible.

Parameters
[in]directoryPathdestination directory path
[in]rmRecursivelyremove recursively
Returns
False if directory couldn't remove else true.
Example
BlackLib::BlackDirectory myDir("/sys/class/");
std::cout << "Removed?: " << std::boolalpha << myDir.removeDirectory("/tmp/dir1",true) << std::endl;
std::cout << "Removed?: " << std::boolalpha << BlackLib::BlackDirectory::removeDirectory("/tmp/dir2") << std::endl;
// Possible Output:
// Removed?: True
// Removed?: True
bool BlackLib::BlackDirectory::renameDirectory ( std::string  oldDirectoryPath,
std::string  newDirectoryPath 
)
static

Renames directory.

Parameters
[in]oldDirectoryPathold name of directory
[in]newDirectoryPathnew name of directory
Returns
True if directory name is changed else false.
Example
BlackLib::BlackDirectory myDir("/sys/class/");
std::cout << "Renamed?: " << std::boolalpha << myDir.renameDirectory("/tmp/dir1","/tmp/dir3") << std::endl;
std::cout << "Renamed?: " << std::boolalpha << BlackLib::BlackDirectory::renameDirectory("/tmp/dir2","/tmp/dir4") << std::endl;
// Possible Output:
// Renamed?: True
// Renamed?: True

Member Data Documentation

std::string BlackLib::BlackDirectory::applicationDirectory
private

is used to hold the application directory


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