Hi all,

Sorry if this is trivial, but I'm sick of it :)
I'm using fpc to convert my (originally delphi) code to read and write
ini style files, however writing will not work as I want, the data in
the pointer keeps getting filled with junk.

I'm using getMem, and reAllocMem functions to create dynamic arrays of
strings. The writeINI function reads the entire file in, but doesn't
even do that correctly.

I have a test program that uses the same idea, and it works perfectly,
so I'm very bewildered :(

attached is the code if someone would like to have a look at it, please.

thank you
James
unit iniFiles;

interface

uses
	sysUtils, tokenizerClass;

type
	pString = ^String;

procedure writeINI(fileName: String; section: String; ident: String; value: String);
function readINI(fileName: String; section: String; ident: String; default: String): String;

implementation

procedure writeINI(fileName: String; section: String; ident: String; value: String);
var
	tmpStrings:		pString;
	inputFile:		Text;
	outputFile:		Text;
	I:					Integer;
	X:					Integer;
	C:					Integer;
	temp:				String;
	foundSection:	Boolean;
	foundIdent:		Boolean;
	tmpIdent:		String;
	inserted:		String;
	tokens:			PTokenizer;
begin
	foundSection := FALSE;
	foundIdent := FALSE;
	if NOT fileExists(fileName) then
		begin
			assign(outputFile, fileName);
			rewrite(outputFile);
			writeLn(outputFile, #91 + section + #93);
			writeLn(outputFile, ident + #61 + value);
			close(outputFile);
		end
	else
		begin
			I := 0;
			getMem(tmpStrings, 100);
			assign(inputFile, fileName);
			reset(inputFile);
			while NOT EOF(inputFile) do
				begin
					{reAllocMem(tmpStrings, (I + 1));}
					readLn(inputFile, tmpStrings[I]);
					inc(I);
				end;
			close(inputFile);
			C := (I - 1);

			for I := 0 to C do
				begin
					writeLn(intToStr(I) + ': ' + tmpStrings[I]);
				end;

			for I := 0 to C do
				begin
					if tmpStrings[I] = (#91 + section + #93) then
						begin
							foundSection := TRUE;
							break;
						end;
				end;

			if foundSection then
				begin
					writeLn('Found section at line ' + intToStr(I));

					for X := (I + 1) to C do
						begin
							if length(tmpStrings[X]) >= 1 then
								begin
									if (tmpStrings[X])[1] = #91 then
										begin
											break;
										end;
								end;
						end;

					writeLn('Section ranges from ' + intToStr(I) + ' to ' + intToStr(X));

					for I := I to X do
						begin
							tokens := new(PTokenizer, init(tmpStrings[I], '='));
							tmpIdent := tokens^.nextToken;
							dispose(tokens, deInit);

							if tmpIdent = ident then
								begin
									foundIdent := TRUE;
									break;
								end;
						end;

					if foundIdent then
						begin
							writeLn('Found ident at line ' + intToStr(I));

							tmpStrings[I] := ident + #61 + value;
							inserted := '';
							X := -1;
						end
					else
						begin
							inserted := ident + #61 + value;
						end;
				end
			else
				begin
					inc(C, 2);
					reAllocMem(tmpStrings, C);
					tmpStrings[I + 1] := (#91 + section + #93);
					tmpStrings[I + 2] := (ident + #61 + value);
				end;

			assign(outputFile, fileName);
			rewrite(outputFile);

			for I := 0 to C do
				begin
					if I = X then
						begin
							writeLn(outputFile, tmpStrings[I]);
							writeLn(outputFile, inserted);
						end
					else
						begin
							writeLn(outputFile, tmpStrings[I]);
						end;
				end;

			close(outputFile);

			freeMem(tmpStrings, 0);
		end;
end;

function readINI(fileName: String; section: String; ident: String; default: String): String;
var
	f:					Text;
	temp:				String;
	foundSection:	Boolean;
	tokens:			PTokenizer;
	tmpIdent:		String;
	tmpValue:		String;
begin
	readINI := default;
	foundSection := FALSE;
	if fileExists(fileName) then
		begin
			assign(f, fileName);
			reset(f);
			
			while NOT EOF(f) do
				begin
					readLn(f, temp);
					if NOT foundSection then
						begin
							if upperCase(temp) = upperCase((#91 + section + #93)) then
								begin
									foundSection := TRUE;
								end;
						end
					else
						begin
							if NOT (temp = '') then
								begin
									if temp[1] = #91 then
										begin
											break;
										end;

									tmpIdent := '';
									tmpValue := '';
									tokens := new(PTokenizer, init(temp, '='));
									tmpIdent := tokens^.nextToken;
									tmpValue := tokens^.nextToken;
									dispose(tokens, deInit);

									if upperCase(tmpIdent) = upperCase(ident) then
										begin
											if tmpValue = '' then
												begin
													break;
												end
											else
												begin
													readINI := tmpValue;
													break;
												end;
										end;
								end;
						end;
				end;
			close(f);
		end;
end;

end.
program test;

uses
	iniFiles;

var
	fileName:	String;
	section:		String;
	ident:		String;

begin
	writeLn('What filename? ');
	readLn(fileName);
	write('What section? ');
	readLn(section);
	write('What ident? ');
	readLn(ident);

	writeLn;
	writeLn(readINI(fileName, section, ident, ''));
end.
program test;

uses
	iniFiles;

var
	fileName:	String;
	section:		String;
	ident:		String;
	value:		String;

begin
	writeLn('What filename? ');
	readLn(fileName);
	write('What section? ');
	readLn(section);
	write('What ident? ');
	readLn(ident);
	write('What value? ');
	readLn(value);

	writeLn;
	writeINI(fileName, section, ident, value);
end.

Reply via email to