Why is C++ on linux basically C? I've tried everything to avoid C on linux but it's simply near impossible. I've tried to stick directly to the standard library but I somehow find myself using dirent.h, types.h, etc..
Is there any other ways to do these other than using C or WinAPI? Or am I just going to have to live with it and merge both of them using #ifdef _WIN32? I'm using Ubuntu 12.04 with g++ and Codeblocks. There's barely any documentation of programming c++ on linux :S I've searched and every time I end up at stackoverflow or "Linux equivalent of..". For bitmaps, I had to create all the structures, types, etc.. There has to be an easier way than this?
Anyway is there any other way to do this with just c++?
#include <stdio.h>
#include <dirent.h>
int listdir(const char *path) {
struct dirent *entry;
DIR *dp;
dp = opendir(path);
if (dp == NULL) {
perror("opendir: Path does not exist or could not be read.");
return -1;
}
while ((entry = readdir(dp)))
Result.push_back(entry->d_name);
closedir(dp);
return 0;
}
Versus:
std::vector<std::string> SearchDirectory(std::string RootDirectory, std::string FileExtension, bool SearchSubdirectories, bool FullPath, bool IncludeFolders)
{
std::vector<std::string> FilesFound; // Result
std::string FilePath; // Filepath
std::string Pattern; // Pattern
std::string Extension; // Extension
HANDLE hFile; // Handle to file
WIN32_FIND_DATA FileInformation; // File information
Pattern = RootDirectory + "/*.*";
hFile = FindFirstFile(Pattern.c_str(), &FileInformation);
if(hFile != INVALID_HANDLE_VALUE)
{
Repeat
if(FileInformation.cFileName[0] != '.')
{
FilePath.erase();
FilePath = (FullPath ? RootDirectory + "/" + FileInformation.cFileName : FileInformation.cFileName);
if(FileInformation.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)
{
if (IncludeFolders)
FilesFound.push_back(FilePath);
if(SearchSubdirectories)
{
// Search subdirectory
std::vector<std::string> Temp = SearchDirectory(FilePath, FileExtension, FullPath, SearchSubdirectories);
for (size_t I = 0; I < Temp.size(); I++)
{
FilesFound.push_back(Temp[I]);
}
Temp.clear();
}
}
else
{
//Check extension
Extension = FileInformation.cFileName;
Extension = Extension.substr(Extension.rfind(".") + 1);
if (!FileExtension.empty() && (Extension == FileExtension))
FilesFound.push_back(FilePath); //Save filename
else if (FileExtension.empty())
FilesFound.push_back(FilePath);
}
}
Until(FindNextFile(hFile, &FileInformation) != TRUE);
FindClose(hFile); //CloseFile Handle
}
return FilesFound;
}