Public feed #599168

Stuff

public text v1 · immutable
Raw Edit
·published 2007-07-02 01:28 UTC
rendered paste body
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

// ===== GLOBAL VARS ===== //

string inputcmds[500][2];
string configcmds[500][2];
short sh_gl_array1count = 0;
short sh_gl_array2count = 0;

string fixlist[500][2];

// ===== END GLOBAL VARS ===== //

// ===== FILE I/O ===== //

// Write newcfg.cfg
void writecfg(string sz_cfg)
{
     ofstream newcfg;
     
     newcfg.open ("config.cfg", ios::trunc);
     
     newcfg << sz_cfg;
            
     newcfg.close();
}

// Read input.txt for cfg rules
string readinput()
{
       string sz_input = "";
       ifstream st_inp;
       
       st_inp.open("input.txt");
       
       while (! st_inp.eof() )
       {
             sz_input.push_back(st_inp.get());
       }
       
       st_inp.close();

       return sz_input;
}

// Read config.cfg
string readcfg()
{
       string sz_config = "";
       ifstream st_cfg;
       
       st_cfg.open("config.cfg");
       
       while (! st_cfg.eof() )
       {
             sz_config.push_back(st_cfg.get());
       }
       
       st_cfg.close();

       return sz_config;
}

// Read README.txt
string readme()
{
       string sz_readme = "";
       ifstream st_readme;
       
       st_readme.open("README.txt");
       
       while (! st_readme.eof() )
       {
             sz_readme.push_back(st_readme.get());
       }
       
       st_readme.close();

       return sz_readme;
}

// ===== END FILE I/O ===== //

// ===== PARSING ===== //

short is_con_ch_num(char ch, bool b_func) // Detect if char is a number or convert char to number
{
     bool b_number = 0;
     short sh_number = 0;
     switch (ch)
     {
            case '1':
                 if (b_func == 0)
                 b_number = 1;
                 else
                 sh_number = 1;
                 break;
            case '2':
                 if (b_func == 0)
                 b_number = 1;
                 else
                 sh_number = 2;
                 break;
            case '3':
                 if (b_func == 0)
                 b_number = 1;
                 else
                 sh_number = 3;
                 break;
            case '4':
                 if (b_func == 0)
                 b_number = 1;
                 else
                 sh_number = 4;
                 break;
            case '5':
                 if (b_func == 0)
                 b_number = 1;
                 else
                 sh_number = 5;
                 break;
            case '6':
                 if (b_func == 0)
                 b_number = 1;
                 else
                 sh_number = 6;
                 break;
            case '7':
                 if (b_func == 0)
                 b_number = 1;
                 else
                 sh_number = 7;
                 break;
            case '8':
                 if (b_func == 0)
                 b_number = 1;
                 else
                 sh_number = 8;
                 break;
            case '9':
                 if (b_func == 0)
                 b_number = 1;
                 else
                 sh_number = 9;
                 break;
            case '0':
                 if (b_func == 0)
                 b_number = 1;
                 else
                 sh_number = 0;
                 break;
            default:
                    b_number = 0;
     }
     if (b_func == 0)
     return b_number;
     else
     return sh_number;
}

short con_sz_sh(string sz_number)
{
      short sh_number = 0;
      short sh_power = 1;
      for (short i = sz_number.size() - 1;
          i >= 0;
          i--)
          {
              sh_number += ( sh_power * is_con_ch_num(sz_number[i], 1) );
              sh_power *= 10;
          }
      return sh_number;
}

void parse_config(string sz_input)
{
     string sz_cmdbuff = "";
     string sz_valbuff = "";
     bool b_cmdfound = 0;
     bool b_valfound = 0;
     bool b_cmdspace = 0;
     bool b_valspace = 0;
     bool b_illegit = 0;
     bool b_donecmd = 0;
     bool b_doneval = 0;
     bool trace = 0;
     
     for (short i = 0; // Loop through content until end of sz_input.
         sz_input[i] != '\0';
         i++)
         {
             if (trace == 1) {cout << sz_input[i];}
             if (
                (sz_input[i] == '/' && sz_input[i+1] == '/')
                || (
                   sz_input[i] == 'e' 
                   && sz_input[i+1] == 'c' 
                   && sz_input[i+2] == 'h' 
                   && sz_input[i+3] == 'o'
                   )
                ) // If a comment/echo is found, disable cmd searching.
             {
             if (trace == 1) {cout << "<-ILL char";}
                b_illegit = 1;
             }
             if ( // If the content is not a tab/space/comment and not following a cmd on that line write to buffer
                sz_input[i] != '\t'
                && sz_input[i] != ' '
                && sz_input[i] != '\n' 
                && b_donecmd == 0
                && b_illegit == 0
                && b_cmdspace == 0
                && is_con_ch_num(sz_input[i], 0) == 0
                )
             {
                 sz_cmdbuff += sz_input[i];
                 b_cmdfound = 1;
             }
             if ( // If the content is not a cvalue on that line write to buffer 2
                sz_input[i] != '\t'
                && sz_input[i] != ' '
                && sz_input[i] != '\n' 
                && b_doneval == 0
                && b_illegit == 0
                && b_valspace == 0
                && is_con_ch_num(sz_input[i], 0) == 1
                )
             {
                 sz_valbuff += sz_input[i];
                 b_valfound = 1;
             }
             if (b_cmdfound == 1 && ( sz_input[i+1] == ' ' || sz_input[i+1] == '\t') ) // Indicate a command has been finished
             {
                if (trace == 1) {cout << "<-END CMD";}
                b_donecmd = 1;  
             }
             if (b_valfound == 1 && is_con_ch_num(sz_input[i+1], 0) == 0 ) // Indicate a value has been finished
             {
                b_doneval = 1;
                if (trace == 1) {cout << "<-END VAL";} 
             }
             if (b_donecmd == 1)
             {
                inputcmds[sh_gl_array1count][1] = sz_cmdbuff;
                sz_cmdbuff = "";
                b_donecmd = 0;
                b_cmdfound = 0;
                b_cmdspace = 1;
                sh_gl_array1count++;
             }
             if (b_doneval == 1)
             {
                inputcmds[sh_gl_array2count][2] = sz_valbuff;
                sz_valbuff = "";
                b_doneval = 0;
                b_valfound = 0;
                b_valspace = 1;
                sh_gl_array2count++;
             }
             if (sz_input[i] == '\n') // If a newline is found, reset searching parameters for next loop.
             {
                b_illegit = 0;
                b_cmdspace = 0;
                b_valspace = 0;
             }
         }
}
                
int main()
{
    char ch_readme;
    cout << "Welcome to the CFG Legalizer."
         << endl
         << "Have you read and followed the instructions \n"
         << "in the included README.txt? (y/n): ";
         
    cin.get(ch_readme);
    
    if (ch_readme == 'n' || ch_readme == 'N')
    {
       cout << endl
            << readme()
            << endl
            << endl;
    }

    parse_config(readinput());
    
    cout << con_sz_sh("1337") - 1337;
    
//for (int a=0;a<500 && inputcmds[a][1].size() > 0;a++){ cout << a << ": " << inputcmds[a][1] << " = " << inputcmds[a][2] << endl;} // display inputcmds array
//for (int a=0;a<500 && inputcmds[a][2].size() > 0;a++){ cout << a << ": " << inputcmds[a][2] << endl;} // display inputcmds array 2

system("pause");
system("cls");
    return 0;   
}