Accessing files in resources folder in mac osx app bundle - c++

I would like to access files which are inside Resources in app bundle. Unfortunately i cannot use QT resorces, as i'm using CascadeClassifier from opencv. My current paths are
const std::string FACE_CLASIFIER_PATH = "/Resources/haarcascade_frontalface_default.xml";
const std::string EYES_CLASIFIER_PATH = "/Resources/haarcascade_mcs_eyepair_big.xml";
I also tried
const std::string FACE_CLASIFIER_PATH = "../Resources/haarcascade_frontalface_default.xml";
const std::string EYES_CLASIFIER_PATH = "../Resources/haarcascade_mcs_eyepair_big.xml";
But nether of them work. As for config both files are present inside MyApp.app/Contents/Resources, i include them using qmake
mac {
APP_XML_FILES.files = ../haarcascade_frontalface_default.xml ../haarcascade_mcs_eyepair_big.xml
APP_XML_FILES.path = Contents/Resources
QMAKE_BUNDLE_DATA += APP_XML_FILES
}
I would appreciate any help with this issue

You don't say what you want to do with the files, though that could be due to my lack of knowledge of opencv. However you can use the Core Foundation classes to get paths to files in the resources folder: -
CFURLRef appUrlRef;
appUrlRef = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("somefile"), NULL, NULL);
// do something with the file
//...
// Ensure you release the reference
CFRelease(appUrlRef);
With a CFURLRef, you can use Apple's documentation to get what you need from it.
For example, if you want a file path: -
CFStringRef filePathRef = CFURLCopyPath(appUrlRef);
// Always release items retrieved with a function that has "create or "copy" in its name
CFRelease(filePathRef);
From the file path, we can get a char* to the path: -
const char* filePath = CFStringGetCStringPtr(filePathRef, kCFStringEncodingUTF8);
So, putting it all together, if you want to get a char* path to haarcascade_mcs_eyepair_big.xml: -
CFURLRef appUrlRef = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("haarcascade_mcs_eyepair_big.xml"), NULL, NULL);
CFStringRef filePathRef = CFURLCopyPath(appUrlRef);
const char* filePath = CFStringGetCStringPtr(filePathRef, kCFStringEncodingUTF8);
// Release references
CFRelease(filePathRef);
CFRelease(appUrlRef);

Related

Add files to the existing archive in XZip, C++, Windows

I am creating a windows service that periodically copies the contents of a folder to an archive. I've searched for archiving libraries and found suggestions to use XZip.
Now I can create a new archive using code like this:
HZIP arch = CreateZip((void*)Archive.c_str(), 0, ZIP_FILENAME);
if (arch == nullptr)
return addLogMessage("Unable to open archive.");
for (const auto& file : std::filesystem::directory_iterator(Directory)) {
const std::wstring filePath = file.path().wstring();
const std::wstring nameInArchive = file.path().filename().wstring();
if (ZipAdd(arch, (TCHAR*)nameInArchive.c_str(), (TCHAR*)filePath.c_str(), 0, ZIP_FILENAME) != ZR_OK) {
std::string what = "Error: Adding '" + std::string(filePath.begin(), filePath.end()) + "' to the archive.";
addLogMessage(what.c_str());
}
}
CloseZip(arch);
I also want to add files to an existing archive. When I run the code below, I get a ZR_ZMODE error from the ZipAdd function, which means tried to mix create / open zip archive:
if (std::filesystem::exists(Archive)) {
HZIP arch = OpenZip((void*)Archive.c_str(), 0, ZIP_FILENAME);
if (arch == nullptr)
return addLogMessage("Unable to re-open existing archive.");
for (const auto& file : std::filesystem::directory_iterator(Directory)) {
const std::wstring filePath = file.path().wstring();
const std::wstring nameInArchive = file.path().filename().wstring();
if (ZipAdd(arch, (TCHAR*)nameInArchive.c_str(), (TCHAR*)filePath.c_str(), 0, ZIP_FILENAME) != ZR_OK) {
std::string what = "Error: Adding '" + std::string(filePath.begin(), filePath.end()) + "' to the archive.";
addLogMessage(what.c_str());
}
}
CloseZip(arch);
}
I assume that in XZip library, I'm not able to add files to the existing archive. Is it true?
EDIT: I've solved the problem with different library, libzip. Now I can add new files to the old archive, and everything works fine. I've posted source code and compiling/linking instruction here.
I am still interested, if there is an option for adding files to the existing archive in XZip library, so I will not close the question for now.

How can i create a dir in Documents folder? [C++]

Im trying to create a directory , or a subdirectory in the Documents folder.
PWSTR ppszPath; // variable to receive the path memory block pointer.
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &ppszPath);
std::wstring myPath;
if (SUCCEEDED(hr)) {
myPath = ppszPath; // make a local copy of the path
}
const wchar_t* str = myPath.c_str();
_bstr_t b(str);
int status = _mkdir(b+"\\New");
As you can see , I'm trying to create a new folder named "New" in Documents Folder.
The path to the documents is correct but the dir is not created.
This is using _bstr_t to avoid using Unicode, the new path is converted to ANSI and will be invalid unless the original path was ANSI, (or ASCII to be guaranteed)
Just pad L"\\New" and wide string functions to solve the problem.
You also have to free ppszPath as stated in documentation
std::wstring myPath;
wchar_t *ppszPath;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_Documents, 0, NULL, &ppszPath);
if (SUCCEEDED(hr))
{
myPath = ppszPath;
CoTaskMemFree(ppszPath);
}//error checking?
myPath += L"\\New";
std::filesystem::create_directory(myPath)
//or _wmkdir(myPath.c_str());
The std::filesystem::path class understands Unicode just fine, so you don’t need to mess with any helpers there. Also, you need to check both function results to determine success or failure:
bool success = false;
PWSTR documents_path = nullptr;
if (SUCCEEDED( SHGetKnownFolderPath( FOLDERID_Documents, 0, NULL, &documents_path ) ))
{
using namespace std::filesystem;
success = create_directory( path{ documents_path } / "new_folder" );
CoTaskMemFree( documents_path );
documents_path = nullptr;
}
The result of the operation is indicated in the variable success.
I would personally separate the functions of obtaining the user’s Documents folder and the directory creation into two separate functions, but the above will do just fine.

How can I compress backup log files?

I'm using log4cplus in my project to do logging.
I created logger.conf and I will load it in the beginning of my application.
This is my logger.conf:
log4cplus.appender.Developer=log4cplus::RollingFileAppender
log4cplus.appender.Developer.DatePattern = ".yyyy-MM-dd"
log4cplus.appender.Developer.Schedule = HOURLY
log4cplus.appender.Developer.File=log/developer.log
log4cplus.appender.Developer.MaxFileSize=3MB
log4cplus.appender.Developer.MaxBackupIndex=10
log4cplus.appender.Developer.layout=log4cplus::PatternLayout
log4cplus.appender.Developer.layout.ContextPrinting=enabled
log4cplus.appender.Developer.layout.ConversionPattern=%D{%Y-%m-%d %H:%M:%S,%Q} [%t] %p - %m%n
log4cplus.appender.Developer.Threshold=TRACE
log4cplus.logger.DEVELOPER=TRACE, Developer
This is how I load my logger.conf:
QString log_path = qApp->applicationDirPath() + "/log";
QDir().mkpath(log_path);
PropertyConfigurator logger(L"configs/logger.conf", Logger::getDefaultHierarchy());
logger.configure();
And whenever I want to log, I use the following line:
Logger::getInstance(L"DEVELOPER").log(INFO_LOG_LEVEL, L"..............");
I'd like to know two things:
How can I tell Log4Cplus to compress the backup logs?
In some post I saw this reply:
I need to create my own appender, inheriting from RollingFileAppender and then add a compression steps.
If it's possible, can anyone tell me how to do it, please? I don't know how to implement this.
How can I add a pattern to the name of these backup logs?
At the moment, Log4Cplus makes my back up like this:
developer.log.1
developer.log.2
developer.log.3
...
I'd like to add date and time to it.
You will need to implement you own Appender like this:
class NewFileAppender : public ::log4cplus::RollingFileAppender
{
void
newFileAppender::rollover()
{
helpers::LogLog & loglog = getLogLog();
// Close the current file
out.close();
out.clear(); // reset flags since the C++ standard specified that all the
// flags should remain unchanged on a close
// If maxBackups <= 0, then there is no file renaming to be done.
if (maxBackupIndex > 0)
{
rolloverCompressedFiles(filename, maxBackupIndex);
// Rename fileName to fileName.DATE
tstring target = filename + DATE;
int ret;
ret = file_rename (filename, target);
//TODO: compress using zlib
}
}
}

C++ iterating through files and directories

I'm working on a C++ program that will automatically backup my work to my FTP server. So far I am able to upload a single file, by specifying a file name using this
CString strFilePath = szFile ;
int iPos = strFilePath.ReverseFind('\\');
CString strFileName = strFilePath.Right((strFilePath.GetLength()- iPos-1) );
CString strDirPath = m_szFolderDroppedIn ;
strDirPath = strDirPath.Mid(0,strDirPath.GetLength() - 1);
int iPost = strDirPath.ReverseFind('\\');
CString strDirName = strDirPath.Right((strDirPath.GetLength()- iPost -1) );
bool curdir = ftpclient.SetServerDirectory((char*)strDirName.GetBuffer(strDirName.GetLength()));
//Upload to Server
int uploadret = ftpclient.PutFile(szFile,(char*)strFileName.GetBuffer(strFileName.GetLength()),0,true,dwLastError);
m_lsDroppedFiles.RemoveAll();
break;
}
Now I want to be able to iterate through a directory (Which contains subdirectories) and recursively call. I'm having a problem getting a hold of the files in the directory.
Any help or code snippet...
Since you are using MFC, you can use the CFileFind class. Example code is given in MSDN. Alternatively, you can use boost.filesystem for the same.
#Swapnil: If you use boost::filesystem, there is a recursive_directory_iterator

Using taglib in a Qt application

I'd like to get the length of a media file in a qt application i'm building and so i decided to use taglib. This is the methos that is meant to read the length
void loadMetaData(QString file) {
QByteArray fileName = QFile::encodeName( file );
const char * encodedName = fileName.constData();
TagLib::FileRef fileref = TagLib::FileRef( encodedName );
if (fileref.isNull())
{
qDebug() << "Null";
}
else
{
qDebug() << "Not Null";
}
}
Problem is fileref is always null for some reason and i can't figure out why......
Use the getter audioProperties() on your FileRef object. The returned pointer contains the length of the file in seconds.
TagLib# is able to work with some Theora files. I used it in a project but found it wouldn't work with many Theora videos (I don't think any converted using libtheora 1.1 worked).
TagLib.File file = TagLib.File.Create(#"c:\video.ogv");
string height = file.Properties.VideoHeight;
This is for the .NET, not C++ though.