dotFusion.net

Source code numberings remover 0.2

Posted on: June 10th, 2010.
Category: C++

Didn’t write for a long time. Just a quick post to add the new version of my Source code numberings remover…

#ifndef __linux__
       #include <conio.h>
#else
       #include "getch.h"
#endif
 
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <cstring>
 
using namespace std;
 
    void RemStartingChars(string &input, bool spacesRemoved)
    {
         if (!spacesRemoved)
               while (input[0] == ' ')
                    input = input.substr( 1, strlen(input.c_str()) );
 
         char numbers[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
         int i = -1;
         bool hasMore = false, tmp = false;
 
         while (numbers[++i])
               while(tmp = (input[0] == numbers[i]))
               {
                     hasMore = hasMore || tmp;
                     input = input.substr( 1, strlen(input.c_str()) );
               }
 
         if (hasMore)
            RemStartingChars(input, true);
    }
 
    int main(int argc, char *argv[])
    {
		cout<<"Source code numberings remover 0.2"<<endl;
		cout<<"Written by Iskren Slavov"<<endl;
		cout<<"Copyright (C) 2009,2010"<<endl;
 
		cout<<"\nThis computer software must be ";
		cout<<"redistributed and edited under the terms of GNU GPLv2. ";
		cout<<"For more information about licensing read COPYING file.\n\n";
 
 
        if (argc > 1)
        {
                 ifstream inFile;
                 ofstream outFile;
 
                 inFile.open(argv[1]);
                 outFile.open(argv[2]);
 
                 if (!inFile)
                 {
                    cerr<<"Unable to open file: "<<argv[1]<<endl;
                    system("PAUSE");
                    exit(1);
                 }
 
                 string input;
                 cout<<"=========================\nConverting... ";
                 while (getline(inFile, input))
                 {
                       RemStartingChars(input, false);
                       outFile<<input<<endl;
                 }
                 cout<<"All done!\n========================="<<endl;
 
                 outFile.flush();
                 outFile.close();
                 inFile.close();
        }
		else
		{
			#ifdef __linux__
            			char * progname = argv[0];
			#else
						char * progname = strrchr(argv[0], '\\')+1;
			#endif
            cout<<"================================================================"<<endl;
            cout<<"Description: Removes leading numbers from source code."<<endl;
            cout<<"Usage: "<<progname<<" <input-file.txt> <output-file.txt>"<<endl;
            cout<<"================================================================"<<endl;
      		cout<<"Press any key to exit . . ."<<endl;
 
      		#ifdef __linux__
            	getchar();
            #else
            	getch();
            #endif
		}
        return 0;
    }

Source code numberings remover 0.2

Multi-platform .INI files in C#

Posted on: January 29th, 2009.
Category: C#

Here is a new way of reading and writing .ini files in C#.
 

It’s an alternative INI file parser written by me. It uses Regex and Collections. I’m only posting a part of the source code because it is too long. But if you scroll a little bit you can download the full source with a Demo project included.
 

Please redistribute and modify under the terms of GNU LGPL v3.0.

 
Source code:

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using System.Globalization;
 
namespace MPIniReadWrite
{
    class MultiplatformIni
    {
        private String iniFilePath;
        public String IniPath
        {
            get { return iniFilePath; }
        }
        private static Char decSep = NumberFormatInfo.CurrentInfo.NumberDecimalSeparator.ToCharArray()[0];
        private static Regex keyValRegex = new Regex(
          @"((\s)*(?<Key>([^\=^\n]+))[\s^\n]*\=(\s)*(?<Value>([^\n]+(\n){0,1})))",
            RegexOptions.IgnorePatternWhitespace | RegexOptions.Compiled | 
            RegexOptions.CultureInvariant
        );
        private static Regex secRegex = new Regex(
          @"(\[)*(?<Section>([^\]^\n]+))(\])" ,
          RegexOptions.Compiled | RegexOptions.CultureInvariant
        );
 
        private ArrayList newValues = new ArrayList();
        struct sectKeyVal
        {
            public String section, key, value;
            public sectKeyVal(String section, String key, String value)
            {
                this.section = section;
                this.key = key;
                this.value = value;
            }
        }
 
        public MultiplatformIni(String iniFilePath)
        {
            this.iniFilePath = iniFilePath;
        }
        ~MultiplatformIni()
        {
            Flush();
        }
// Source code truncated...

 
Download the full source code and an example (Visual C# 2005 Express Project)

I’ve compiled this using .NET framework 2.0 and Mono 2.2 with no problem. Have fun! :)