Ini File System!

This tutorial is a MUCH better alternative to the Inside3d .ini file tut.  This is much simpler, faster, and works better!

Lets get started!

Add in the blue code and take out any pink code.  Pay attention to any green text.

First create a new C file, M_CONFIG.C.

At the top instead of just #include "g_local.h" put:

#inlcude "g_local.h"
#include <windows.h>

This adds Windows files to your project.

Now go into the Linker options section of your dev program and check what libraries are being
linked into the DLL. Make sure it includes all of these:


kernel32.lib shell32.lib gdi32.lib user32.lib

Now you can begin adding functions to m_config.c.
The first is a function to grab the full path of your ini file.

Insert:


// the ini file char
char m_iniFile[MAX_PATH];

DWORD mSetupIniPath (void)
{
    cvar_t    *game;
    char file[MAX_PATH];
    char name[MAX_PATH];
    char dir[MAX_PATH];

    // grab the +set game parameter
    game = gi.cvar("game", "", 0);

    // now check on the availability and set the string
    if (*game->string)
        sprintf (file, "%s\\yourmod.ini", game->string); // **CHANGE THIS**
    else
        return (ERROR_PATH_NOT_FOUND);

    // get the current directory
    GetCurrentDirectory(MAX_PATH, dir);

    // form the path string
    sprintf(name, "%s\\%s", dir, file);

    // set up the string, and print it to the console
    strcpy (m_iniFile, name);
    gi.dprintf("Initialization file: %s\n", m_iniFile);

    // successful
    return (ERROR_SUCCESS);
}

Now you have the INI file in a global char. At the bottom of InitGame put:


mSetupIniPath();

and above InitGame put:



unsigned long mSetupIniPath (void);

From now on, whenever you need to load something from the INI file, first go to the top of that
C file and add:



#include <windows.h>

First of all, if you want to load a number from the ini file, use:



int number;
number = GetPrivateProfileInt("Section", "Key", 1, m_iniFile);

 

This will do the following:

1. Open the ini file in m_iniFile (set up earlier).
2. Go to the section [Section].
3. Load the key Key.
4. Replace it with the number 1 if anything messes up.

So that call would load the number from:

[Section]
Key=5

number would equal 5. If anything messed up, number would equal 1.

Definition:
GetPrivateProfileInt(char *section, char *key, int default, char *filename);

 

Instead, if you would like to load a string from the INI file, use this:



char something[256];
GetPrivateProfileString("Section", "Key", "Default", something,
sizeof(something), m_iniFile);

 

This will do the same as GetPrivateProfileInt except it'll load a string into char something, and
replace it with "Default" if something messes up.

It'll load from:

[Section]
Key=This is a string.

Would make something = "This is a string.".

Definition:
GetPrivateProfileString(char *section, char *key, char *default, char *destination,
int sizeofdest, char *inifile);


That's about it. Once you learn the system, it's much simpler than the old inside3d.com ini file
system.

Tutorial by Psykotik