ini icon indicating copy to clipboard operation
ini copied to clipboard

Double backslashes are turned into one

Open guillaume86 opened this issue 9 years ago • 3 comments

I'm on windows (don't think it is revelant) and I have a problem with values containing multiple backslashes like \\192.168.1.1, the parser turn the \\ into \

Is it expected behavior? I don't think it should be since a simple read then reencode change the file and lose information.

Exemple: first pass: \\\\ read as \\ and then encoded as \\ second pass: \\ read as \ and then encoded as \ third pass: \ read as \ and then encoded as \

guillaume86 avatar Aug 17 '14 16:08 guillaume86

This is a serious problem for ini.parse conversion that have windows share information in them. For example starting with this information in an ini file:

[SECTION]
pathkey=\\machinename\share\directory\myfile.txt

then execute this code against the above text:

let iniContents: any = ini.parse(fileText);

This is always interpreted and ruined (by the ini.parse) as:

"iniContents": {
	"SECTION": {
		"pathkey": "\machinename\share\directory\myfile.txt"
	},
}

which means that once I go to output the data back to an INI file, like this:

writeFile(iniPath, ini.stringify(iniContents))

The resulting INI file now contains this:

[SECTION]
pathkey=\machinename\share\directory\myfile.txt

Is this going to be looked at and put in the bug queue?

corvinrok avatar Aug 29 '18 16:08 corvinrok

AS a TEMPORARY work-around, you can do the following (this is typescript, but the same thing can be done in javascript, without the type definitions):

        const iniPath: string = this.getIniLocation;
        // read in the ini file
        readFile(iniPath, 'utf-8')
            .then((fileText) => {
                // the ini.parse method does not properly escape backslash characters in paths
                // so we must do this ourselves.
                let backslashEscape: string = fileText.replace(/\\/g, '\\\\');
                let iniContents: any = ini.parse(backslashEscape);
                iniContents.SECTION.MainFilename = mainFilePath;
                iniContents.SECTION.BackupFilename = backupFilePath;
                // write out to file
                return writeFile(iniPath, ini.stringify(iniContents));

The key change is the escape line of code that uses the replace to escape the backslashes in the raw text before being interpreted by the ini.parse method.

                let backslashEscape: string = fileText.replace(/\\/g, '\\\\');

corvinrok avatar Aug 29 '18 17:08 corvinrok

You can try my fork, ini-win, which aims to be more compatible with the way Windows handles ini files. Windows doesn't do any unescaping when reading the values, and so does my fork, so it fixes this issue.

m417z avatar Aug 17 '22 20:08 m417z