Is there a special place to store configurations in standard c++ - 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.

Related

Match the text (.txt) files to images (.jpg) files

I have two folders in my system, one is "Image folder (contains images)" and 2nd is "Text folder (contains text files)". In these folder, few images and text files have the same names like: abc.jpg and abc.txt.
Actually, I want to find out the text file from the 2nd folder using input image name (or I want to match the text files name with the image names).
Thereafter, I wants to copy the matched text file into the "Image folder".
I am working on WINDOWS operating system.
If your issue is to find two different file names (but with similar basenames), notice that:
directories and folders are unknown to the C++11 or C++14 standard. Future C++17 standard might provide a filesystem library (but you won't find a mature implementation easily today)
POSIX and Windows has directories (not folders). You could use (notably on Linux or MacOSX) POSIX functions like opendir(3), readdir(3), closedir(3) combined with stat(2) to explore them, or use some higher level library functions like nftw(3).
basename(3) could be useful, but you can use string functions once you know that / is used as a directory separator.
some framework libraries, notably Qt, POCO, Boost, .... provide useful functions on directories and may give a common abstraction of them usable on several operating systems. Actually I recommend using a framework library, because it is easier and more portable.
The notion of file, of file systems, and of directory is very operating system specific (and some academic OSes don't have them and provide a different notion of persistence). Read Operating Systems: Three Easy Pieces (freely downloadable) for an overview. On Linux and POSIX systems, a file is really some i-node, a directory is a kind of file having entries mapping names to i-nodes, and a file could have several names in various directories (e.g. using link(2)). The C++ standard knows about standard streams, e.g. thru its input/output library.
Copying a file generally means to copy its content (byte by byte) so is not an elementary operation. In practice better copy large blocks of at least 16 kilobytes. Some libraries provide functions to copy files.
On Windows (which I don't know) the notion of file and "folder" is different, and the directory separator is \. You need to dive into Microsoft documentation. Even Microsoft documentation speaks of directories. But using a framework library would be simpler (and more portable).
BTW, the terminology of folder is generally wrong. You see some folders (not all of them) on your GUI or desktop environment, but the OS (and your program) knows about directories and files.
Sometimes, using some higher-level abstraction than files is useful. For example the SQLite library provides you with some database abstraction, GDBM gives indexed files, and you might consider using some database system like PostGreSQL or MongoDB, etc etc.... YMMV.

Location of config files in project c++

I was wondering whether there is a convention for storing config files in c++. At the moment I am storing the config files in the same directory as the source code that uses them. When building I make sure via the CMakeLists to copy them to the correct location so I can just access them in a relative way (e.g. "config.cfg" iso "/foo/bar/config.cfg) for convenience.
The practice for config files is non portable and operating system dependent. You have also to ask yourself if your configuration is defined per installation/system or per user.
In general, it is a very bad idea to store config in the same directory as your execuable, at least once the developpement is finished. In general executables may be shared between several users and should therefore for security reasons be located in directories that are write-protected for everybody but the system administrator.
For unix/linux, you could for example consider:
/etc or a subfolder thereof, if your configuration is per installed system,
~/ if it's user defined configuration. The usual practice would be to start the filename with a dot. This article will tell you more.
For windows systems, you should consider:
the usual approach now, goes to the registry. Of course, this uses the windows api and is fully non portable.
a subfolder of C:\ProgramData or C:\Users\All users\AppData\Local if your configuration is per installed system,
a subfolder of C:\Users\%USERNAME%\AppData\Local for the users's own configuration.
This SO questions shows how to find the right folders.

How to check if path leads to executable file?

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

Program configuration data in Unix/Linux

What is recommended way to keep a user configuration data in Unix/Linux?
My programming language is C++. Configuration data will be kept in XML/text/binary format, I have no problem with handling such files. I want to know where can I keep them. For example, in the Windows OS configuration data may be kept in the Registry (old way) or in user application data directory. What about Linux?
I need read/write access to configuration files.
The concept of the registry is peculiar to Windows, and Microsoft once admitted to it being ill-conceived (see this, this, this, this (see #2), and this).
In Unix and Linux, configuration for system-wide programs is in /etc or maybe an application-specific subdirectory.
Per user configuration data are kept in the user's home directory in a hidden file—in text format—or an application-specific hidden directory in the user's home directory. The proper way to reference the home directory is through the environment variable HOME. Hidden files and directories are created by making . the first character of the name.
Examples for system-wide configuration is /etc/wgetrc and /etc/ssh/. Examples of per-user data are $HOME/.bashrc and $HOME/.mozilla/.
The XDG Base Directory Specification specifies where configuration and other files should be stored in Linux and other X-based operating systems:
http://freedesktop.org/wiki/Specifications/basedir-spec
This is the modern way, and may eventually reduce the dotfile mess in the typical user's home directory.
Dotfiles are the classic Unix solution. If you want to deal with reading/writing everything yourself, go for it.
However, most modern programs I use have used GConf for storing preferences. It makes a lot of things easier, both as a developer and as a user (and apparently as an administrator, but I have no experience there).
That depends a little on your flavor of Linux but as a general rule most programs have the system default configuration somewhere in /etc with .config files in your home directory that can override the defaults in the /etc dir.
Great point .config should be .[Name of config file]

Determining files in a directory

I come from a C# background and I am working on a C++ project. I need to open files in a directory, then process that data in the files. The problem is on my target environment (Greenhills Integrity), I cannot access a "directory". It seems C++ does not have a concept of a directory. Why not? This problem is simple in C#. I cannot link to any big library(BOOST or dirent) to get the files. I can open a file using fopen, but I won't always know the file names, so I have to "strcat" the directory to each filename in order to "fopen" the files.
I need a way to just get the file names in a directory without using an external API. Is that possible?
The major C++ APIs have directories. Start with readdir on POSIX or FindFirstFile() on Windows. Greenhills seems to support POSIX.
No, it's not possible. C++ has no "built-in" directory functionality - you need to use a library of some sort.
Check with your operating system. Directory handling is different for each. You will have to use the Windows 32 API if you want to list/query directories on Microsoft Windows, and the Linux API (e.g. opendir/stat) if you want to list/query directories on Linux.