1

I need your help. I have two .ini-files with similar keys and values. I want them to be synchronized but more like merged.

There will be change on both sides of those .ini-files so we can't create a synchronisation cause we won't overwrite the other side. Everything should be merged/synchronized automatically, while we change content on both ini-files!

The goal is always to change a ini-file in our headquarter and merge/sync it with a file on our cash desk.

Example which cannot work:

ini-file1.ini, is on a cash desk and gets changed several times a day by the cash desk!

ini-file2.ini, is a dublicate of ini-file1.ini in our headquarter. We wanna be able to chnage ini-file2.ini during a change on the cash desk.

The cash desk changed the receiptnumber in ini-file1.ini and we (headquarter) changed key2 in ini-file2.ini:

ini-file1.ini:

key1 = 1;
key2 = 2;
receiptnumber: 122

ini-file2.ini:

key1 = 1;
key2 = 4;
receiptnumber: 118

And now we establish a synchronisation/merge between ini-file1.ini and ini-file2.ini.

A merge-application cannot know which of those keys is the proper key and which one to discard and overwrite.

Actually we thought we could investigate in a "insert/include" in one of those ini-files and outsource everything that comes from our second ini-file. So we got a object orieted ini-file. (My "ini-file oop concept")

Should mean:

ini-file1.ini, is on a cash desk and gets changed several times a day by the cash desk!

ini-file-outsource.ini, is just a part of the cash desk config file. We want it like attached to ini-file1.ini through a line like insert "C:\ini-file-outsource.ini"; which doesn't exist, but we need something similar. It is everything which handles settings in the cashdesk, so this file is not changed by the cash desk. It's on the cash desk.

ini-file2.ini, is a dublicate of ini-file-outsource.ini. It's in our headquarter and used to change settings for the cash desk.

ini-file1.ini:

key1 = 1;
key2 = 2;
receiptnumber: 122
insert "C:\ini-file-outsource.ini";

ini-file-outsource.ini:

key3 = 3;
key4 = 4;
key5 = 8;

ini-file2.ini:

key3 = 3;
key4 = 4;
key5 = 8;

This time we establish a synchronisation between ini-file2.ini and ini-file-outsource.ini.

This should work if it's possible to achieve cause we change only different content on every side. Every key will be unique.

If you have any better approach I'm open for everything.

Thanks for your help

  • Are you looking to actually alter the file itself? What should `ini-file1.ini` look like after this merger? How does `ini-file1.ini` get used? – MonkeyZeus Aug 09 '18 at 13:20
  • I can definitely change the file itself, I'm just forced to keep the name and somehow its content. It's a config file and used by a cash register program. Best solution is to keep in its current state without any include like we have the include in another file. But we won't a huge application which opens the ini-file, change the keys, save and close. It's what we do currently. Or we use a include, if it's somehow possible...? – John Vision Aug 09 '18 at 13:33
  • I do not understand. The solution you need heavily depends on the capabilities of the cash register program's handling of ini files. If it supports includes then use includes. If it does not then PHP could be used to merge multiple files into one file. Which solution are you targetting? – MonkeyZeus Aug 09 '18 at 14:03
  • Ok I see. I cannot know wheather the cash register supports includes or not. Its configuration guide is way not deep enough. Could you give me some examples how to use an "include/insert" for a .ini-file? Or do you think it's really dependent from the cash desk software? Because if so I'm probably targetting your solution two with PHP. Because I really don't have any kind of instructions regarding include commands from the cash desk software. – John Vision Aug 09 '18 at 14:27
  • It's 100% dependent on the cash register software. If the cash register software does not support include/insert then there is no valid example to use "include/insert" for your ini files. The configuration guide would hold that information. If that information is not in the guide then assume it's not possible. – MonkeyZeus Aug 09 '18 at 14:31
  • Why do you assume `insert "C:\ini-file-outsource.ini";` is valid? Who or what told you to do that? – MonkeyZeus Aug 09 '18 at 14:34
  • It's not valid. It's just an example to visualize what I'm aiming for.. – John Vision Aug 09 '18 at 14:37
  • Thanks for your help anyway. I thought there's a valid syntax for some .ini-files or something similar. You helped me a lot. Do you have any other suggestions for me I could attempt? – John Vision Aug 09 '18 at 14:41
  • Yes, PHP has a function called [`parse_ini_file()`](http://php.net/manual/en/function.parse-ini-file.php) which you can use to read two separate `ini` files into arrays and then use [`array_merge_recursive()`](http://php.net/manual/en/function.array-merge-recursive.php) to merge the arrays of data and then check out the answers at https://stackoverflow.com/q/17316873/2191572 for ideas of how to transform the merged array back into an ini file for your cash register program to use. – MonkeyZeus Aug 09 '18 at 14:46
  • Either that or you can just concatenate ini files on to one another but you can expect unexpected behavior when file1 has `key3 = 3` and file2 has `key3 = 4`. It will be up to the cash register software to determine if the latter (4) value wins or if the first encountered value (3) wins. With the `array_merge_recursive()` route you are guaranteed that the latter (4) value will win. – MonkeyZeus Aug 09 '18 at 14:53
  • With either solution, you will need to have a minimum of 3 ini files: the first one is the absolute original file, the second is the data which needs to be appended to the original, and the third file is the merger of the other two which the cash register software picks up and uses. – MonkeyZeus Aug 09 '18 at 14:56
  • Anyways, go ahead and try something. If you get stuck then update your question with your attempt and feel free to [ping me](https://meta.stackexchange.com/q/43019/235923). – MonkeyZeus Aug 09 '18 at 15:11
  • Could you explain me what do you mean by `concatenate ini files on to one another`? And how you would do it? I should have no problems with similar keys and different values because every key is unique. I think I can't go that way with 3 files. The third file which is used by the cash desk as you mentioned must be able to write data in the ini-file. If I merge my absolute original file with my attached file I always overwrite new data in my third ini-file. So there's always something like: `But we won't a huge application which opens the ini-file, change the keys, save and close.` – John Vision Aug 10 '18 at 08:35
  • I was just hoping I skipped a point or there's another possibility or a way I missed which didn't come into my mind... unfortunately the ini-file oop concept doesn't work, that should have perform perfectly. Nevertheless thanks for your help and your effort! – John Vision Aug 10 '18 at 08:40
  • I honestly do not understand what you are trying to achieve. What is writing to which ini file and which ini file are you looking to merge with what and which data is supposed to be preserved or overwritten? What is an ini-file oop concept? I did not provide any oop examples. – MonkeyZeus Aug 10 '18 at 12:29
  • @MonkeyZeus I revised my whole task to clarify your questions. I hope you get a better definition of my issue now. I'm glad to have someone who has a look on my problem. – John Vision Aug 10 '18 at 14:03
  • So you should overwrite `ini-file-outsource.ini` with `ini-file2.ini` and then use the `array_merge_recursive()` to merge `ini-file-outsource.ini`'s contents into `ini-file1.ini` – MonkeyZeus Aug 10 '18 at 14:18
  • You keep using `insert "C:\ini-file-outsource.ini";` in your examples but it's invalid, right? If it's not invalid then say so. – MonkeyZeus Aug 10 '18 at 14:19
  • this is honestly aggravating and a waste of my time, good luck. – MonkeyZeus Aug 10 '18 at 14:19
  • I will try this: So you should overwrite ini-file-outsource.ini with ini-file2.ini and then use the array_merge_recursive() to merge ini-file-outsource.ini's contents into ini-file1.ini – MonkeyZeus 26 mins ago. Thanks for your time @MonkeyZeus – John Vision Aug 10 '18 at 14:44

1 Answers1

0

You can use something like:

ini=%d/../anotherFile.ini:section

Where %d is current file directory and :section refers to specific section to include

Alexandru R
  • 8,560
  • 16
  • 64
  • 98