How to check if path leads to executable file? - c++

I try to create some kind of file browser. I want to know if file under path is executable in a cross-platform way.
How to do such thing with boost::filesystem?

Boost doesn't have stuff about permissions, because POSIX permissions are not "crossplatform".
Use the platform-specific APIs at your disposal as required. Sorry!

You can try QT. It is cross-platform. You do not have to care about the operating system differences while dealing with files. What you mean by "executable" is somehow unclear though. If you are talking about file permissions, OT can give this kind of information (Just look at QFile class documentation). If you want to learn whether you can actually run it or not, you have to have some kind of file extension convention. For example, .exe in Windows. I do not know, may be there is a way to look at the initial bits of the file and learn whether it is a binary or not, but I think you will not be able to find a library call for that. You have to implement some platform specific routines for this. If I am not mistaken, file browsers mostly look at the extension of the file to find out the type. For example, if you change the file extension of a pdf to exe than windows explorer sees this file as an executable. Clearly after the file type assumption, it can try to learn some other things about the file, such as icon of the executable. But initially it only looks at the extension. Otherwise, it would be very slow to browse directories containing large numbers of files.
I hope, I gave some relevant information here

Related

Access Path Of File In C++

I want to access the path of the executable file. I know in python, you can do the following:
import os
filePath, fileName = os.path.split(__file__)
The code above will get the path of the file, and the name of the file that the lines are in. Is this applicable in C++ (returning the name is more optional)? I do NOT want the source code, only the path of the executable.
PS: if you think this is very little detail, copy paste the code above, paste the following:
print(filePath)
and run the code in a python intepreter.
PPS: Tried installing POCO or qt, as one of the comments in the first question mentioned, and didn't find out how to install.
PPPS: For more details of why I want the path and what I'm doing it with: I need the path so I can load assets to a game. Sometimes, the program just won't load the image after putting in the name of the file, like other people in tutorials do (albeit the image file is in the same directory). And then I figured out that for python, the code above works, so I'm asking if this is applicable in C++.
In Python, you're getting the path to the file containing the source code that's executing.
In C++, the file containing the source code to what's executing may not exist, or (more often) may exist but be inaccessible (e.g., stored on a system to which you don't have access).
Instead, in C++ the source code is normally compiled to an executable file, and that's what runs. But that executable contains machine-level instructions rather than the C++ source code of the program as the programmer wrote it.
If you want access to the source code at run time, you're almost certainly going to have to do that on your own--make sure a copy of the source code is on the target computer in some location you can find.
The C++ standard library contains basic support for things like searching for a particular file in a file system tree, but it's going to be up to you to put those building blocks together into a system that gives you access to the source code at run-time. Chances are pretty good that in the process, you'll end up needing a few things that are available on most operating systems, but still require code specific to the operating system you're using (e.g., getting the path to the executable file so you can build some path relative to that where you store the source file).

How to specify a standard directory in a Qt project file

I have developed an application that I plan to deploy on Windows, Mac, and Linux. The program requires access to some files (scripts and the like) at run-time.
The installation process should install the files to a location that my application can later determine without user-intervention (or perhaps just a prompt allowing the user to change the location, if desired).
What is the best way to achieve this? I can't seem to find any way to:
1. Use a "standardized path" variable in the project file's INSTALLS statement. (e.g., my application could use QStandardPaths to initialize the location, but I can't figure out how to access this path from the INSTALLS statement)
2. Save the path to my project's QSettings (.plist, registry, whatever) for later retrieval
That leaves me with creating a custom project file and INSTALLS command for each environment, and then I still can't install to the user's directory because I don't know the user's name when I deploy the make command. It seems as if there must be a better way, but I can't seem to find any documentation for this. Am I just using the wrong keywords in my searches? Thanks in advance!
What standard directory? What type of getting that standard directory?
For instance, you can put such thing in your windows branch of .pro file:
win32 {
APPDATA_DIR = $$system(echo %APPDATA%) # should be %LOCALAPPDATA% as requested
message($$APPDATA_DIR)
}
Just unsure of what exact kind of standartized path you are talking about. QStandardPaths knows many. It makes sense to be more concrete to find the correspondence with concrete OS.
Also somewhat relative reply on mine, on how to check the correspondence with certain variable, etc: Qt .pro file - how to add conditioning on OSX version?
Maybe this class will help you
QStandardPaths documentation
But your problem is still little bit unclear for me.

Why does QFileInfo::isExecutable() return false for ".msi" files?

I'm currently developing an application using C++ and Qt and I need to know if an input file is executable.
I'm using QFileInfo::isExecutable() which behaves correctly for most files, except for those with the .msi extension, for which it returns false.
How can I figure out for sure if a file is executable or not? I need to do this in Qt for cross-compatibility reasons.
Well, an MSI file isn't actually executable. It's a document format used by the Windows Installer executable. So QFileInfo is correct.
What is the specific problem you need to solve? Why do you think knowing whether a file is executable will help you? What is the goal you have in mind? If you give us more information, we may be able to suggest a better approach.
You should know how QFileInfo::isExecutable() determines a file as executable:
In Windows Qt just checks if the extension of file is .exe, .com and .bat without checking its content.
In Unix-like systems, it checks the file's attribute and returns true for files which have execution permission.
So a .msi file isn't a executable file in view of Qt under Windows.

Is there a special place to store configurations in standard c++

Is there a standard place to store configurations like database setting in c++? Just use xml file?
Need windows solution, but it is better to be platform independent.
Check out Boost Program Options. Apart from being one of the best command-line option processors in any language, it also supports reading configuration data from files with a syntax like INI, and using environment variables. It's suitable for exactly what it says: program options. If you have a huge variety or a hierarchy of configurations, however, you might better check out Boost Property Tree, which read INI files but also XML or JSON, and is probably better suited if you have a really large configuration.
No standard that I know of, but you have several libraries for program configuration, for example libconfig. Also, the Windows API has some utilities to parse INI files for programs, for example see this link.
Standard C++ is a language only, it don't know anything other than the language itself.
What you're asking totally depends on the libraries or framework you'll decide to use to connect to databases. There is no standard library that have this purpose. So first choose the database, then the library to connect to it, then you'll get the configuration infos in the library documentation.
There's nothing in the standard, but Boost.Program_options is a good library for retrieving/storing configuration.
Obviously the configuration file must be stored in the correct location: if it's a per-user configuration file, on Windows it will be stored in the %APPDATA%1 directory (usually in a subdirectory named after your application), on Linux in a dot file under the home directory. For non-user specific configuration files, they may be stored in the "All Users" Application Data folder on Windows1, and under /etc on Linux2.
Naturally, you won't hardcode these paths, but you'll use SHGetFolderPath with the appropriate CSIDL values (or SHGetKnownFolderPath if you don't care about pre-Vista compatibility), like CSIDL_APPDATA for per-user settings, CSIDL_COMMON_APPDATA for settings common to all users.
Notice that /etc on Linux is writeable only by the superuser; I don't remember if the "all users" profile is writable for normal users under Windows.

Find out the mime type and associated applications with Qt

How can I find out a mime-type or content-type of a given file?
I cannot use the suffix because the file could be renamed.
Possible additions would be categorizing them as jpg, gif, png and so on are image files and can be opened by editing applications, which has been set in the OS.
Thank you in advance.
What platform? On *nix, you should refer to how the program file does it, which is based on a few heuristics including checks of the first few bytes of a file (many file formats start with a fixed header, including many image formats).
If you're on Windows, the *nix file command is probably still instructive, even if you can't reuse its code as directly. There may also be some better solution in the Windows APIs (I'm not a Windows programmer).
This could help, it is with C# but I think you can get the idea.
http://kseesharp.blogspot.com/2008/04/c-get-mimetype-from-file-name.html
You can use some sort of class for acccesing Windows Registry from qt or using the Windows API directly from qt.
I am not a qt programmer.
You can't, not from within Qt.
However, if all you want is to show a file with the correct application, you can use QDesktopServices::openUrl(), which wraps open (Windows/OSX) and xdg-open (Unix). The URL may also be a local file (in that case, use QUrl::fromLocalFile() to construct the URL).
For categorizing image files. For Qt and just only for image files.
QByteArray imageFormat = QImageReader::imageFormat(fileName); //Where fileName - path to your file
Further mapping imageFormat to mime-type is not complicable
It doesn't look like Qt offers that capability yet. However, you may want to look into the libmagic library which does what the file and other similar commands do. Assuming the library is maintained properly it will include new MIME types as time passes. From what I can tell it is part of the file tool suite. Under a Debian system do something like this:
sudo apt-get install libmagic-dev
to get the development library and use #include to make use of it.