Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So basically my dll file is reading a configuration.ini file and i'm attempting to write a function to allow me to place a bool & Integer/atoi on the same lines along with two different str char type/atoi on same lines.

for example

// virtual key value, virtual key value2
title here = vk_1, vk_2

// true/false, virtual Key value
title here = true, vk_9
title here = false, vk_p

example of Integer function I need help taking to values and reading from ini file on multiple lines

C++
int Integer(LPSTR Value) {
	if (!_stricmp("A", Value)) return 'A';
	if (!_stricmp(":", Value)) return VK_OEM_1;
	if (!_stricmp("PAUSE", Value)) return VK_PAUSE;
	if (!_stricmp("VK_PAUSE", Value)) return VK_PAUSE;

return atoi(Value);
}


What I have tried:

i have already wrote a function to set the int variable values allowing me to places as many int values on a single line as i want

example
C++
// int 1, int 2, int 3, int 4, int 5
title here = 30, 4, 6, 99, 1
// bool variable, int 1
title here = off, 40

void setIntValue(LPSTR Value, INT Variable[])
{
	stringstream ss(Value);
	string word;
	int i = 0;

	while (getline(ss, word, ',')) {

		transform(word.begin(), word.end(), word.begin(), ::tolower);

		if (!_stricmp("on", word.c_str()) || !_stricmp("true", word.c_str()))
			word = "1";

		if (!_stricmp("off", word.c_str()) || !_stricmp("false", word.c_str()))
			word = "0";

		Variable[i] = stoi(word);
		if (ss.peek() == ',')
			ss.ignore();

		i++;
	}
}
Posted
Updated 12-Sep-16 21:13pm
v2
Comments
Richard MacCutchan 13-Sep-16 3:41am    
That is a really bad design decision. DLL's should be self contained so they can operate in any user space. If any particular classes or functions need specific parameters to control their operations, then that data should be provided by the exe file that uses the features of the dll.

In my opinion, using INI files such a way is cumbersome. Probably you need a file format more suitable for holding hierarchical structured data. You might have a look at Serialization - Wikipedia, the free encyclopedia[^].
 
Share this answer
 
You must write a parser that splits each line into it's elements:
The title, the assignment character, and a comma separated list of values.

You can for example use strchr or string::find to locate the assignment character and get the title and the parameters.

Then extract multiple parameters using strtok or do it like above but using the comma now. Then remove leading and trailing (white) spaces from each substring and convert to lower.

Once you have a single parameter you should use lookup tables to determine the type and value. With mixed boolean and integers value I would check for boolean values first with a function returning three states (not a boolean, false, and true):

C++
int getBoolean(const char *s)
{
    if (0 == strcmp(s, "false") || 0 == strcmp(s, "no"))
        return 0;
    if (0 == strcmp(s, "true") || 0 == strcmp(s, "yes"))
        return 1;
    return -1;
}


If the above fails (returns -1), check for an integer:

C++
typedef struct {
    const char *name;
    int value;
} lookup_t;

int getInteger(const char *s)
{
    static const lookup_t lookup[] = {
        { "pause", VK_PAUSE },
        // ...
        { NULL, -1 }
    };

    int i = 0;
    while (lookup[i].name)
    {
        if (0 == strcmp(s, lookup[i].name))
            break;
        i++;
    }
    return lookup[i].value;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900