Retrieving path to current/own bundle - c++

While writing a cross-platform module, I needed a way to retrieve the path to the module directory (so I can access files in the modules folder, like configs and resources). My module, currently compiled as a bundle, is loaded by arbitrary processes. Getting the path of my module is done pretty easily in windows, like so:
GetModuleFileName(GetModuleHandle(0), buf, size); // copies the path of the current DLL into buf
I haven't been able to find a similar method on OSX. I tried _NSGetExecutablePath() but it only retrieves the path of the main program. Also tried the method here.
However, it also gets the path of the main executable. What is the common method for doing so on OSx?
Regards
edit:
grady player showed me the way :) I compiled the following code using NSBundle:
/*
this could be anything apparantly
*/
#interface dummyObject : NSObject
- (void) dummyMethod;
#end
#implementation dummyObject
- (void) dummyMethod {
}
#end
int GetBundlePath(char * buf, int bufSize)
{
NSString * path = [[NSBundle bundleForClass:[dummyObject class]]bundlePath];
const char * intString = [path UTF8String];
int length = strlen(intString);
int smallestLength = length > bufSize ? bufSize : length;
memcpy(buf, intString, smallestLength);
[path release];
return smallestLength;
}

[[NSBundle bundleForClass:[thisClass class]]bundlePath];
will work for dynamic library...
or if you are looking for your main
[[NSBundle mainBundle] bundlePath];
also bundleURL... see the NSBundle Documentation
then your resources should be relative to your main bundle.
you can use
[[[NSBundle mainBundle] bundlePath]stringByAppendingPathComponents:#[#"Contents",#"Resources"]];

Related

C++ Tesseract OCR: Getting ObjectCache(): WARNING! LEAK! object still has count 1

I installed libtesseract-dev (v4.1.1) on Ubuntu 20.04 & I am trying out a C++ code to OCR an image to searchable PDF.
My code is somewhat modified than the C++ API example code provided at official website:
/home/test/Desktop/Example2/testexample2.cpp:
#include <leptonica/allheaders.h>
#include <tesseract/baseapi.h>
#include <tesseract/renderer.h>
int main()
{
//const char* input_image = "/usr/src/tesseract-oc/testing/phototest.tif";
//const char* output_base = "my_first_tesseract_pdf";
//const char* datapath = "/Projects/OCR/tesseract/tessdata";
const char* input_image = "001.jpg";
const char* output_base = "001";
const char* datapath = ".";
int timeout_ms = 5000;
const char* retry_config = nullptr;
bool textonly = false;
int jpg_quality = 92;
tesseract::TessBaseAPI *api = new tesseract::TessBaseAPI();
if (api->Init(datapath, "eng")) {
fprintf(stderr, "Could not initialize tesseract.\n");
exit(1);
}
/*
tesseract::TessPDFRenderer *renderer = new tesseract::TessPDFRenderer(
output_base, api->GetDatapath(), textonly, jpg_quality);
*/
tesseract::TessPDFRenderer *renderer = new tesseract::TessPDFRenderer(
output_base, api->GetDatapath(), textonly);
bool succeed = api->ProcessPages(input_image, retry_config, timeout_ms, renderer);
if (!succeed) {
fprintf(stderr, "Error during processing.\n");
return EXIT_FAILURE;
}
api->End();
return EXIT_SUCCESS;
}
I also followed https://stackoverflow.com/a/59382664 as follows:
cd /home/test/Desktop/Example2
wget https://github.com/tesseract-ocr/tessdata/raw/master/eng.traineddata
wget https://github.com/tesseract-ocr/tesseract/blob/master/tessdata/pdf.ttf
export TESSDATA_PREFIX=$(pwd)
gedit config
(In the config file, entered the contents:
tessedit_create_pdf 1 Write .pdf output file
tessedit_create txt 1 Write .txt output file
)
g++ testexample2.cpp -o testexample2 -ltesseract
./testexample2
But on execution, it displays the errors as follows:
Warning: Invalid resolution 0 dpi. Using 70 instead.
Error during processing.
ObjectCache(0x7f1b096669c0)::~ObjectCache(): WARNING! LEAK! object 0x55af5c5241a0 still has count 1 (id /home/test/Desktop/Example2/eng.traineddatapunc-dawg)
ObjectCache(0x7f1b096669c0)::~ObjectCache(): WARNING! LEAK! object 0x55af5c506770 still has count 1 (id /home/test/Desktop/Example2/eng.traineddataword-dawg)
ObjectCache(0x7f1b096669c0)::~ObjectCache(): WARNING! LEAK! object 0x55af5c9a4a70 still has count 1 (id /home/test/Desktop/Example2/eng.traineddatanumber-dawg)
ObjectCache(0x7f1b096669c0)::~ObjectCache(): WARNING! LEAK! object 0x55af5c9a4980 still has count 1 (id /home/test/Desktop/Example2/eng.traineddatabigram-dawg)
ObjectCache(0x7f1b096669c0)::~ObjectCache(): WARNING! LEAK! object 0x55af5d7d5170 still has count 1 (id /home/test/Desktop/Example2/eng.traineddatafreq-dawg)
My directory structure is:
Example2
|------->001.jpg
|------->config
|------->eng.traineddata
|------->pdf.ttf
|------->testexample2
|------->testexample2.cpp
I have searched about this on multiple sources, but could not find any fix for this.
Further, I would like to know if there is someway I can build a binary using C++ compilation from this code + libtesseract such that my binary becomes a standalone portable binary, running which on other Ubuntu systems would not require reinstalling tesseract libraries & their dependencies
tesseract API examples is show case for using tesseract features without covering all specifics of programming language of your choice (c++ in your case).
Just looking at your code even without trying it: you dynamically allocates memory 2x but you did not deallocate them. Try to fix these issues.
You must free use dynamic memory for your class "api"
Use:
... you code...
if (renderer) delete renderer;
if (api) delete api;

How to give folder of images as input to Magick++ api?

I am need of passing a folder of images as input to Magick++ api. It can be done using mogrify in commandline as shown in post "ImageMagick script to resize folder of images". Reading a single image could be done through api call as
Image image(inputimage)
But how could we do the same for a folder of images? Can anyone help me with the respective api call?
That feature is not included in the Magick++ API. You will need to iterate the directory yourself and then use the Magick++ API to read and write the image. You can find an example on how to iterate through a folder in C/C++ in the following Stack Overflow post: How can I get the list of files in a directory using C or C++?.
I believe you would be responsible of reading the directory. The C library dirent.h is the first thing I think of, but I'm sure there's better C++/system/framework techniques.
#include <iostream>
#include <vector>
#include <dirent.h>
#include <Magick++.h>
int main(int argc, const char * argv[]) {
std::vector<Magick::Image> stack; // Hold images found
DIR * dir_handler = opendir("/tmp/images"); // Open dir
struct dirent * dir_entry;
if (dir_handler != NULL)
{
// Iterate over entries in directory
while ( (dir_entry = readdir(dir_handler)) != NULL ) {
// Only act on regular files
if (dir_entry->d_type == DT_REG) {
// Concatenate path (could be better)
std::string filename("/tmp/images/");
filename += dir_entry->d_name;
// Read image at path
stack.push_back(Magick::Image(filename));
}
}
closedir(dir_handler); // House keeping
} else {
// Handle DIR error
}
// Append all images into single montage
Magick::Image output;
Magick::appendImages(&output, stack.begin(), stack.end());
output.write("/tmp/all.png");
return 0;
}
There's also ExpandFilenames(int *,char ***) in the MagickCore library.
// Patterns to scan
int pattern_count = 1;
// First pattern
char pattern[PATH_MAX] = "/tmp/images/*.png";
// Allocate memory for list of patterns
char ** dir_pattern = (char **)MagickCore::AcquireMagickMemory(sizeof(char *));
// Assign first pattern
dir_pattern[0] = pattern;
// Expand patterns
Magick::MagickBooleanType ok;
ok = MagickCore::ExpandFilenames(&pattern_count, &dir_pattern);
if (ok == Magick::MagickTrue) {
std::vector<Magick::Image> stack;
// `pattern_count' now holds results count
for ( int i = 0; i < pattern_count; ++i) {
// `dir_pattern' has been re-allocated with found results
std::string filename(dir_pattern[i]);
stack.push_back(Magick::Image(filename));
}
Magick::Image output;
Magick::appendImages(&output, stack.begin(), stack.end());
output.write("/tmp/all.png");
} else {
// Error handle
}

C++ File Paths Issue

When I use this code to load an image for SDL to render:
SDL_Texture* testimg = IMG_LoadTexture(renderer, "testz.bmp");
(or)
SDL_Texture* testimg = IMG_LoadTexture(renderer, "/testz.bmp");
the image doesn't render at all. However... if I use this code:
SDL_Texture* testimg = IMG_LoadTexture(renderer, "../bin/testz.bmp");
SDL draws the image just fine. "bin" is the folder the .exe is in. So how do I fix this file paths issue?
edit: another possibility may be that visual studio, for some reason, is running the exe it put in the bin folder in another location which doesnt have the image...
Because the directory where your executable is installed is not the same thing as the current directory of the process that your operating system starts, when it runs this executable.
It's fairly obvious that the current directory of your started process is the sibling directory of its executable.
One thing that might work is to use argv[0] to make sure the correct base path is used. Assuming you're using standard main(), it would go something like this:
std::string base_path;
int main(int argc, char* argv[]) {
base_path = argv[0];
size_t last_slash = base_path.find_last_of('/');
if(last_slash == std::string::npos)
base_path = ".";
else base_path.erase(last_slash + 1);
// Anything else you need to do in `main()`
return 0;
}
And then your loading would look like this:
SDL_Texture* testimg = IMG_LoadTexture(renderer, base_path + "testz.bmp");
If it's in a different file than main(), you'll also need to redeclare base_path with extern:
extern std::string base_path;
And of course you need to #include <string> for this to work.
If all your loading is done in main(), you can make this a bit better by moving the declaration of base_path into main():
int main(int argc, char* argv[]) {
std::string base_path = argv[0];
// The rest is the same
}

Chrome extension has FireBreath plugin... Uncaught Error: Error calling method on NPObject

My deepest apologies. The file wasn't copied into the correct directory and thus could not be read. The extension works across all Windows platforms after all. This is a reminder to perform proper error handling at all times.
Straight question: Do I need to upload more than the FireBreath generated .dll file (needing proper .js, .html, and .json are obvious) for a FireBreath plugin within a Chrome extension?
Big picture:
FireBreath generates a .dll and I believe putting loading this .dll file into the Chrome extension folder I upload using chrome://extensions/ unpacked is sufficient. In other words, I think I do not need to upload additional C++ code. Calling plugin.openUserIdFromFile() generates my error.
Success: I loaded the desktop username from a file to a Chrome extension using a NPAPI FireBreath plugin. It works on the Windows desktop on which the plugin was developed.
Failure: Error calling method on NPObject. Error is received on all Windows environments: XP, 7, or 8 outside the development environment.
Known: A buddy ran http://www.dependencywalker.com/ software on it and found that IEShims.dll was a missing dependency in his environment, but I included it in the uploaded folder to no avail.
JavaScript Chrome extension makes call to FireBreath plugin dll:
$(document).ready(function () {
setInterval(getAllChromeTabs, 10000);
var plugin = document.getElementById("pluginId");
while (user.length < 1) {
user = plugin.openUserIdFromFile();
}
console.log(user);
});
C++ FireBreath plugin function called from Chrome Extension:
std::string LabStatsPluginAPI::openUserIdFromFile()
{
std::string aTempFileName = "aTempFileName";
DWORD nBufferLength = MAX_PATH;
LPTSTR lpBuffer = (new TCHAR[nBufferLength]);
DWORD tempPath = GetTempPath(nBufferLength, lpBuffer);
char* localTempPathArray = new char[nBufferLength];
for (int i = 0; i < nBufferLength; i++) {
localTempPathArray[i] = (char)lpBuffer[i];
}
std::string localTempPath(localTempPathArray);
localTempPath = localTempPath + aTempFileName;
std::ifstream streamFromFile;
std::ifstream::pos_type fileSize;
streamFromFile.open( localTempPath, std::ios::in|std::ios::binary|std::ios::ate );
char* userNameString;
int userNamesize = streamFromFile.tellg();
streamFromFile.seekg(0, std::ios::beg);
userNameString = new char[userNamesize];
streamFromFile.read(userNameString, userNamesize);
delete[] lpBuffer;
delete[] localTempPathArray;
std::string userNameSafeString(userNameString);
delete[] userNameString;
return userNameSafeString;
}
The following modification to the C++ (the if statement inserted) fixes the function.
char* userNameString;
int userNamesize = streamFromFile.tellg();
if (userNamesize < 1) {
return ""; // no file found
}
streamFromFile.seekg(0, std::ios::beg);
userNameString = new char[userNamesize];
streamFromFile.read(userNameString, userNamesize);

C++ Find execution path on Mac

Lets say my executable is located at /Users/test_user/app on Mac OSX and I am running it from /Users/test_user/Desktop/run_app:
Desktop run_app$ /Users/test_user/app/exec
Within my C++ code how can I find the path to the location of the executable (which in this case would be /users/test_user/app)? I need to reference some other files at this path within my code and do not want to put absolute paths within the code as some users might place the folder in a different location.
man 3 dyld says:
_NSGetExecutablePath() copies the path of the main executable into the
buffer buf. The bufsize parameter should initially be the size of the
buffer. This function returns 0 if the path was successfully copied.
It returns -1 if the buffer is not large enough, and * bufsize is set
to the size required. Note that _NSGetExecutablePath() will return "a
path" to the executable not a "real path" to the executable. That is,
the path may be a symbolic link and not the real file. With deep
directories the total bufsize needed could be more than MAXPATHLEN.
#include <mach-o/dyld.h>
#include <limits.h>
int main(int argc, char **argv)
{
char buf [PATH_MAX];
uint32_t bufsize = PATH_MAX;
if(!_NSGetExecutablePath(buf, &bufsize))
puts(buf);
return 0;
}
Provided I'm understanding you correctly, you should be able to use NSProcessInfo's -arguments method to get the executable path.
To mix in Objective-C code with C++ code, you can just change the filename extension of the source file in question from .cpp to .mm. Then add the Foundation.framework to the Link Binary With Library build phase of your Target.
[EDIT] updated to show the difference between argv[0] and [[[NSProcessInfo processInfo] arguments] objectAtIndex:0].
Then to use the code, you could do something like in the following code:
#include <iostream>
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
// print out raw args
NSMutableArray *arguments = [NSMutableArray array];
for (NSUInteger i = 0; i < argc; i++) {
NSString *argument = [NSString stringWithUTF8String:argv[i]];
if (argument) [arguments addObject:argument];
}
NSLog(#"arguments == %#", arguments);
const char *executablePath =
[[[[NSProcessInfo processInfo] arguments] objectAtIndex:0]
fileSystemRepresentation];
printf("executablePath == %s\n", executablePath);
const char *executableDir =
[[[[[NSProcessInfo processInfo] arguments] objectAtIndex:0]
stringByDeletingLastPathComponent] fileSystemRepresentation];
printf("executableDir == %s\n", executableDir);
[pool release];
return 0;
}
If I then cd into the parent directory of the executable, and then execute the executable using a relative path:
MacPro:~ mdouma46$ cd /Users/mdouma46/Library/Developer/Xcode/DerivedData/executablePath-ewememtbkdajumdlpnciyymduoah/Build/Products/Debug
MacPro:Debug mdouma46$ ./executablePath blah blah2
I get the following output:
2011-08-10 12:59:52.161 executablePath[43554:707] arguments == (
"./executablePath",
blah,
blah2
)
executablePath == /Users/mdouma46/Library/Developer/Xcode/DerivedData/executablePath-ewememtbkdajumdlpnciyymduoah/Build/Products/Debug/executablePath
executableDir == /Users/mdouma46/Library/Developer/Xcode/DerivedData/executablePath-ewememtbkdajumdlpnciyymduoah/Build/Products/Debug
So, while argv[0] may not necessarily be a full path, the result returned from [[[NSProcessInfo processInfo] arguments] objectAtIndex:0] will be.
So, there's [[[NSProcessInfo processInfo] arguments] objectAtIndex:0], or a slightly simpler approach is just to use NSBundle itself, even if it is a command-line tool (see What is the "main bundle" of a command-line foundation tool?):
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
const char *executablePath =
[[[NSBundle mainBundle] executablePath] fileSystemRepresentation];
[pool release];
Maybe you could use the which command. http://unixhelp.ed.ac.uk/CGI/man-cgi?which
If it's a "real" OS X application the proper way to do it is to create a bundle:
http://developer.apple.com/library/mac/#documentation/CoreFoundation/Reference/CFBundleRef/Reference/reference.html