Example #1
0
void LoadIniFile(const char *iniFileName)
{
    FILE *infile = fopen(iniFileName, "rt");
    if (infile != NULL)
    {
        bool enableSerialPort = false;
        bool enableEcho = false;
        std::string portName = "COM1";
        int baud = 110;
        int bytesize = 8;
        int parity = 0;
        int stopbits = 2;
        int crDelayMillis = 0;      // delay in milliseconds after sending a carriage return

        const int max = 128;
        char line [max];
        while (fgets(line, max, infile))
        {
            line[max-1] = '\0';

            // Truncate each line at cr, lf, or pound sign.
            // Find position of equal sign, if present.
            int eqindex = -1;
            int firstNonSpaceIndex = -1;
            int lastNonSpaceIndex = -1;
            for (int i=0; line[i]; ++i)
            {
                if (line[i]=='\n' || line[i]=='\r' || line[i]=='#')
                {
                    break;
                }
                if (line[i] == '=')
                {
                    eqindex = i;
                }
                if (!isspace(line[i]))
                {
                    if (firstNonSpaceIndex < 0)
                    {
                        firstNonSpaceIndex = i;
                    }
                    lastNonSpaceIndex = i;
                }
            }
            line[1+lastNonSpaceIndex] = '\0';
            if (firstNonSpaceIndex >= 0)
            {
                // Not a blank line.  Is its contents valid?
                if (eqindex >= 0)
                {
                    line[eqindex] = '\0';
                    const char *key = line + firstNonSpaceIndex;
                    const char *value = line + eqindex + 1;
                    if (0 == stricmp(key, "serial"))
                    {
                        enableSerialPort = !!atoi(value);
                    }
                    else if (0 == stricmp(key, "port"))
                    {
                        portName = value;
                    }
                    else if (0 == stricmp(key, "baud"))
                    {
                        baud = atoi(value);
                    }
                    else if (0 == stricmp(key, "echo"))
                    {
                        enableEcho = !!atoi(value);
                    }
                    else if (0 == stricmp(key, "upcase"))
                    {
                        TheTerminal.setUpperCase(!!atoi(value));
                    }
                    else if (0 == stricmp(key, "bytesize"))
                    {
                        bytesize = atoi(value);
                    }
                    else if (0 == stricmp(key, "parity"))
                    {
                        parity = atoi(value);
                    }
                    else if (0 == stricmp(key, "stopbits"))
                    {
                        stopbits = atoi(value);
                    }
                    else if (0 == stricmp(key, "crdelay"))
                    {
                        crDelayMillis = atoi(value);
                    }
                    else
                    {
                        printf("ttychess.ini: Ignoring unknown key '%s'\n", key);
                    }
                }
                else
                {
                    printf("ttychess.ini: Ignoring line '%s'\n", line);
                }
            }
        }
        fclose(infile);
        infile = NULL;

        TheTerminal.setCarriageReturnDelayMillis(crDelayMillis);

        if (enableSerialPort)
        {
            printf("Opening serial port %s for %d baud ...\n", portName.c_str(), baud);
            TheTerminal.OpenComPort(portName.c_str(), baud, enableEcho, bytesize, parity, stopbits);
        }
    }
    else
    {
        printf("Note: could not open file '%s'\n", iniFileName);
    }
}