Modifying a configuration file - c++

I want to modify value corresponding to a key in configuration file using cpp.
Eg:
key=value
has to be changed to
key=new_value
I works with 'sed' command, but is there some possible way to do it using cpp way.
Implementation fails when the string to be changed is less than the existing one. Is that not really possible using cpp?

You basically only have one option: Rewrite the configuration file, write out all configuration options from the start.
Trying to move around contents of a text file if one line changes length (bigger or smaller) is possible, but it's much more work than just rewriting it.
It's either that or the working solution using sed, which can be called from inside the program using the system function.

Related

When a file changes, I'd like to modify one or more different files

I've been scouring the web for hours looking for an approach to solving this problem, and I just can't find one. Hopefully someone can fast-track me. I'd like to cause the following behaviour:
When running ember s and a file of a certain extension is changed, I'd like to analyze the contents of that file and write to several other files in the same directory.
To give a specific example, let's assume I have a file called app/dashboard/dashboard.ember. dashboard.ember consists of 3 concatenated files: app/dashboard/controller.js, .../route.js, and .../template.hbs with a reasonable delimiter between the files. When dashboard.ember is saved, I'd like to call a function (inside an addon, I assume) that reads the file, splits it at the delimiter and writes the corresponding splitted files. ember-cli should then pick up the changed source (.js, .hbs, etc.) files that it knows how to handle, ignoring the .ember file.
I could write this as a standalone application, of course, but I feel like it should be integrated with the ember-cli build environment, but I can't figure out what concoction of hooks and tools I should use to achieve this.

Modifying a complex config file in C/C++

I need to be able to modify a complex config file from within my C/C++ program (whichever is going to be easier and more convenient to use in this case, probably C++). The idea is that the program computes the values that it needs to insert into the file, then writes them.
The config file itself looks like this:
^looots of stuff I won't be using^
option blah.blah.something "value"
option blah.blah.someotherthing "value"
Now, many of the options are logically connected, for instance:
option blah.blah.car.engine "value"
option blah.blah.car.color "value"
I don't really have an idea how to reach those lines that I'm interested in. Should I skip to a specific line and then search for a quotation mark and modify what's after it? That doesn't seems like a reliable and flexible solution, does it?

Edit an existing file in C++

I have preferences file for my application in Qt.
Whenever I launch the application it loads the file & depending the contents creates the widgets accordingly. I have given some customisation option for the user. Now when my application is closed I am checking whether the preferences are changed. If yes then I am opening the preferences file again & then just adding everything again in it. I am not creating a temporary file, I am directly editing the file. Is this method an efficient one? or am I doing it wrong? because I found these threads:
1. What is the best way to edit the middle of an existing flat file?
http://qt-project.org/forums/viewthread/25924/
Should use I binary file? Currently I am using ASCII file.
Thanks.
UPDATE: In the stackoverflow link above the correct answer has
If you're stuck using flat, then you're stuck using the old fashioned
way of updating them
I meant to ask whether the method I am using is old fashioned & is there a better method used nowadays? & what does flat file mean?
Don't try to edit the file. Rewrite the whole thing
Rewrite by using a temporary file. i.e. write the current state of preferences into temp file. And then move/rename the file to your actual preference file after making sure the temp file has been written successfully. This helps in case there is a crash when you rewriting the file. If you rewrite it directly, you will be left with neither the old one nor a good new one. In case you do it through a temp file, then do it this way ensures that in case of a crash, you atleast have a good preferences file(though it contains the old preferences).
Text/Binary doesn't make much difference unless it's a really huge file. Text will help to hand edit it if required.
That's a perfectly fine way of doing it. Since you already seem to have all of the preferences loaded into memory and you have edited them there, you don't need to bother with a temporary file (unless you want to ensure safety if your program fails during writing). Just write all of the preferences back into the original file.
Whether you choose a binary file or text file is up to you. Preferences tend to be text files because they have the added benefit of being human readable with just a text editor, rather than some specialised software for viewing them. A binary file may, however, have slightly better performance due to simpler decoding.

Opening a File with different text editors

Apparently this supposed to be possible. For example opening and operating on a file with NOTEPAD, or HxD. But aren't they all text files...how would one specify which text editor to open the file and operate on the file with using the WINDOWS API. It is certainly not in "CreateFile".
Hopefully I'm understanding your question... The easiest way to do this is to launch the desired editor and pass the filename as an argument, rather than "invoking" the file (which will launch the default program associated with the file type).
For example, notepad.exe mytextfile.txt or gvim.exe mytextfile.txt.
If the editor is not on your %PATH%, you'll need to use a full path file name.
What are you trying to do, exactly? You could:
Maintain a list of editors that you expect to be installed and have entries for in the system's PATH (bad idea)
Have an editor/editors that you want to use, query the Windows registry to find the installation path of the editors (using RegGetValue), and launch the editor with CreateProcess) (a little better idea)
Query the registry to get the default editor for a given file type and then launch that editor using CreateProcess. (best idea)
But it all depends on what your goal is really.
Edit based on requirements
So, just so we're on the same page, from C++, you want to:
Take a command line parameter to your C++ application (filename)
Open that file in an arbitrary editor
Detect when the user has made changes to that file
Operate on the file contents
Is that correct?
If so, you could:
Use Boost libs to compute a CRC for the current data in the file
Launch an editor using one of the methods I initially described
Stick in a tight loop and sleep so you don't chew up resources while the initially computed CRC matches one calculated every iteration of the loop
Of course, there are all kinds of issues that you'd have to deal with (that's just a super simple way of describing the algorithm I might use), such as:
What happens if the user doesn't change the file?
What happens if the file isn't found?
I'm sure that there are a number of different methods of doing this, but this is the easiest method that I can think of at the moment (while still being able to be fairly certain of the changes).
Disclaimer: I haven't implemented something like this, so I might be completely off base ;)
Are you looking for the ShellExecute() or ShellExecuteEx() APIs on Windows? They'll launch whatever program is registered for a file (generally based on the filename extention).

Using windows CopyFile function to copy all files with certain name format

Hello! I am updating some C code that copys files with a certain name. basically, I have a directory with a bunch of files named like so:
AAAAA.1.XYZ
AAAAA.2.ZYX
AAAAA.3.YZX
BBBBB.1.XYZ
BBBBB.2.ZYX
Now, In the old code, they just used a call to ShellExecute and used xcopy.exe. to get all the files starting with AAAAA, they just gave xcopy the name of the file as AAAAA.* and it knew to copy all of the files starting with AAAAA. now, im trying to get it to copy with out having to use the command line, and I am running into trouble. I was hoping CopyFile would be smart enough to handle AAAAA.* as the file to be copied, but it doesnt at all do what xcopy did. So, any Ideas on how to do this without the external call to xcopy.exe?
Check this out as a starting point
http://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx
or even better this full example
http://msdn.microsoft.com/en-us/library/aa365200(v=VS.85).aspx
You could also use SHFileOperation or IFileOperation (the latter being only available from Vista upwards but is now the recommended way according to MSDN). SHFileOperation supports wildcards and displays a progress by default, but there's also a flag for silent operation.
Check out the following MSDN links for more info:
http://msdn.microsoft.com/en-us/library/bb762164(v=VS.85).aspx
http://msdn.microsoft.com/en-us/library/bb775771(v=VS.85).aspx
You would basically have to write code to reproduce the functionality in xcopy. To do so, you must build a list of files by accessing the path and recursing through it. Test each found entry with your pattern and keep only those that match. Then iterate over that list with CopyFile.
See the following set of functions that can help you build the file list:
http://en.wikipedia.org/wiki/Dirent.h
It might just be easier to keep using xcopy unless you have a specific reason not to.
There are lots of ways to do it. I'd probably use a loop of FindFirstFile() / FindNextFile().
However, is there any reason you can't still use xcopy? You can launch it with CreateProcess(). It isn't pretty, but it works.