Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Double backslashes are turned into one #30

Open
guillaume86 opened this issue Aug 17, 2014 · 3 comments
Open

Double backslashes are turned into one #30

guillaume86 opened this issue Aug 17, 2014 · 3 comments

Comments

@guillaume86
Copy link

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 \

@corvinrok
Copy link

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
Copy link

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, '\\\\');

@m417z
Copy link

m417z commented Aug 17, 2022

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.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants