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
I’ve been trying to get Vista Compatible Open and Save Dialogs in Lazarus (FreePascal) recently. After researching I found some example code around the net which didn’t quite work in FreePascal. I modified and extended the code and the result is here:

(Default Lazarus Open Dialog)

(Vista Compatible Open Dialog)

(Default Lazarus Save Dialog)

(Vista Compatible Save Dialog)
And now the source code…
{
VistaOpenSaveDialog:
A new Open/Save dialog that uses Windows API to display Vista
compatiable Open or Save Dialog.
Tested in Windows 2000/XP/Vista/Vista x64.
Compiled with Lazarus 0.9.29 beta (21448) / FreePascal 2.2.4
It should compile successfully with all newer and maybe some the versions.
Created by Iskren Slavov (http://www.dotfusion.net/).
No right reserved. Feel free to use for any of your applications
regardless of the license.
##########################################
Example usage:
VistaOpenSaveDialg(Handle, '', '', '', 'Open file...', fileName,
OFN_FILEMUSTEXIST, VDT_OPENDIALOG);
Some usable flags under Windows Vista:
OFN_READONLY, OFN_HIDEREADONLY,
OFN_OVERWRITEPROMPT, OFN_FILEMUSTEXIST,
OFN_PATHMUSTEXIST, OFN_FORCESHOWHIDDEN,
OFN_DONTADDTORECENT
}
unit VistaOpenSaveDlg;
{$mode objfpc}{$H+}
interface
uses
Windows, Messages, CommDlg, SysUtils;
type
TVistaDlgType = (VDT_OPENDIALOG, VDT_SAVEDIALOG);
function ReplaceStr(const S, Srch, Replace: string): string;
function VistaOpenSaveDialog(Parent: HWND; const Filters,
DefaultExtension, InitialDir, Title: string; var FileName: string;
DlgFlags: DWORD; DlgType: TVistaDlgType): Boolean;
implementation
function ReplaceStr(const S, Srch, Replace: string): string;
var
i: Integer;
Source: string;
begin
Source := S;
Result := '';
repeat
i := Pos(UpperCase(Srch), UpperCase(Source));
if i > 0 then
begin
Result := Result + Copy(Source, 1, i - 1) + Replace;
Source := Copy(Source, i + Length(Srch), MaxInt);
end
else
Result := Result + Source;
until i <= 0;
end;
function VistaOpenSaveDialog(Parent: HWND; const Filters, DefaultExtension, InitialDir, Title: String;
var FileName: String; DlgFlags: DWORD; DlgType: TVistaDlgType): Boolean;
var
fnStruct: TOpenFileName;
szFile: array[0..MAX_PATH] of Char;
begin
Result := False;
FillChar(fnStruct, SizeOf(TOpenFileName), 0);
with fnStruct do
begin
hwndOwner := Parent;
lStructSize := SizeOf(TOpenFileName);
lpstrFile := szFile;
StrPCopy(lpstrFile, FileName);
nMaxFile := SizeOf(szFile);
lpstrFilter := PChar(ReplaceStr(Filters, '|', #0) + #0#0);
if (Title <> '') then
lpstrTitle := PChar(Title);
if (InitialDir <> '') then
lpstrInitialDir := PChar(InitialDir);
if DefaultExtension <> '' then
lpstrDefExt := PChar(DefaultExtension);
Flags := Flags or DlgFlags;
end;
case DlgType of
VDT_OPENDIALOG:
if GetOpenFileName(@fnStruct) then
begin
Result := True;
FileName := StrPas(szFile);
end;
VDT_SAVEDIALOG:
if GetSaveFileName(@fnStruct) then
begin
Result := True;
FileName := StrPas(szFile);
end;
end;
end;
end.
Download Lazarus Compiled Demo Project with Source Code.
Posted on: January 26th, 2009.
Category:
C#
This time I’m going to present you the Windows way of Reading and Writing .INI files using C#.
This is far from the best way of doing this, because it works only when your executable is being run on Windows (this code uses kernel32.dll functions). Here is the base INI Read/Write class code:
Here’s the nuberings remover source:
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace IniReadWrite
{
class IniFile
{
[DllImport("kernel32")] private static extern long WritePrivateProfileString(string section,
string key, string value, string iniFilePath);
[DllImport("kernel32")] private static extern int GetPrivateProfileString(string section,
string key, string defValue, StringBuilder retValue, int size, string iniFilePath);
private const int valueLength = 255; // Maximum length of value
private String iniPath; // The path of our .ini file
public IniFile(String iniFilePath)
{
iniPath = iniFilePath;
}
public void WriteString(String section, String key, String value)
{
WritePrivateProfileString(section, key, value, iniPath);
}
public void WriteInt(String section, String key, int value)
{
WriteString(section, key, value.ToString());
}
public void WriteDouble(String section, String key, Double value)
{
WriteString(section, key, value.ToString());
}
public String ReadString(String section, String key, String defValue)
{
StringBuilder tmp = new StringBuilder(valueLength);
int i = GetPrivateProfileString(section, key, defValue, tmp, valueLength, iniPath);
return tmp.ToString();
}
public int ReadInt(String section, String key, int defValue)
{
return Int32.Parse(ReadString(section, key, defValue.ToString()));
}
public Double ReadDouble(String section, String key, Double defValue)
{
return Double.Parse(ReadString(section, key, defValue.ToString()));
}
}
}
Download this code and an example (Visual C# 2005 Express Edition Project)
I will soon be posting a version of this code that doesn’t need kernel32.dll and should work cross-platform even when compiled for Linux (using Mono).
Posted on: January 23rd, 2009.
Category:
C++
This is the C++ rewrite of the Numberings remover tool. It compiles with MingW/GCC and any other major C++ compiler under Windows. Under Linux it is tested with GCC only. I think it should work fine on some other Unix-like operating systems but it is not tested. If you test it on some OS other than Linux or Windows you can send me your fixes/suggestions and/or the results of the test.
Here is the source code:
#ifndef __linux__
#include <conio.h>
#else
#include "getch.h"
#endif
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
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 (C++ rewrite) 0.1"<<endl;
cout<<"Written by Iskren Slavov"<<endl;
cout<<"Copyright (C) 2009"<<endl;
cout<<"\nThis computer software must be ";
cout<<"redistributed and edited under the terms of GNU GPLv2. ";
cout<<"For more information about licensing read LICENSE.TXT\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;
getch();
}
return 0;
}
Download the full Source Code (C++)
Posted on: January 20th, 2009.
Category:
C#
I’ve been copying a lot of source code from different sites recently. But when I copy the source code it is being pasted with numberings in front of every line.
So I wrote this program to remove all the numbers before each line of source code.
Here’s the nuberings remover source:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace RemFirstNums
{
class Program
{
static void RemStartSymbols(ref String input, bool firstSpacesRemoved)
{
String numbers = "0123456789";
CharEnumerator charEnum = numbers.GetEnumerator();
bool foundMore = false, tmp = false;
if (!firstSpacesRemoved)
while (input.StartsWith(" ") || input.StartsWith("\\s+"))
input = input.Substring(1, input.Length - 1);
while (charEnum.MoveNext())
while (tmp = input.StartsWith(charEnum.Current.ToString()))
{
foundMore = foundMore || tmp;
input = input.Substring(1, input.Length - 1);
}
if (foundMore)
RemStartSymbols(ref input, true);
}
static void Main(String[] args)
{
TextReader TextRead = null;
TextWriter TextWrite = null;
Console.WriteLine("NumberingRemover for C# 0.1");
Console.WriteLine("Written by Iskren Slavov");
Console.WriteLine("Copyright (C) 2009");
Console.Write("\nThis computer software must be ");
Console.Write("redistributed and edited under the terms of GNU GPL v2. ");
Console.Write("For more information about licensing read LICENSE.TXT\n\n");
try
{
if ((args[0].Trim() != null) && (args[1].Trim() != null))
{
String workStr = "";
TextRead = new StreamReader(args[0]);
TextWrite = new StreamWriter(args[1]);
Console.Write("=========================\nConverting... ");
while ((workStr = TextRead.ReadLine()) != null)
{
RemStartSymbols(ref workStr, false);
TextWrite.WriteLine(workStr);
}
Console.WriteLine("All done!\n=========================");
}
TextRead.Close();
TextWrite.Close();
}
catch (IOException ex)
{
Console.Error.WriteLine("{0}", ex.Message);
Console.WriteLine("\nPress any key to exit . . .");
Console.ReadKey();
}
catch (IndexOutOfRangeException)
{
Console.WriteLine("==================");
Console.WriteLine("Description: Removes leading numbers from source code.");
Console.WriteLine("Usage: NumberingRemover.exe <input-file.txt> <output-file.txt>");
Console.WriteLine("==================");
Console.WriteLine("\nPress any key to exit . . .");
Console.ReadKey();
}
}
}
}
Download the source code and binaries (MS Visual C# Express edition project)
I hope this little program will be helpful for you, developers. I’m thinking of a rewrite in C++, so stay tuned…