On Sun, Feb 09, 2003 at 08:30:57PM +1000, James Mills wrote:
> 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

Sorry, forgot to include the tokenizerclass.pas

cheers
James
unit tokenizerClass;

interface

type
	TTokenizer = object
		constructor init(tmpStr: String);
		constructor init(tmpStr: String; tmpDelim: Char);
		destructor deInit;
		private
			str:		String;
			token:	String;
			num:		Integer;

			delim:	Char;

			function countTokens: Integer;
		public
			function nextToken: String;
			function numTokens: Integer;
			function restOfTokens: String;
	end;
	PTokenizer = ^TTokenizer;

implementation

constructor TTokenizer.init(tmpStr: String);
begin
	delim := ' ';
	if tmpStr = '' then
		begin
			str := '';
			num := 0;
		end
	else
		begin
			str := tmpStr;
			num := countTokens;
		end;
end;

constructor TTokenizer.init(tmpStr: String; tmpDelim: Char);
begin
	delim := tmpDelim;
	if tmpStr = '' then
		begin
			str := '';
			num := 0;
		end
	else
		begin
			str := tmpStr;
			num := countTokens;
		end;
end;

destructor TTokenizer.deInit;
begin

end;

function TTokenizer.countTokens: Integer;
var
	i:	Integer;
	c:	Integer;
begin
	c := 0;
	for i := 1 to length(str) do
		begin
			if str[i] = delim then
				begin
					inc(c);
				end;
		end;
	countTokens := (c + 1);
end;

function TTokenizer.nextToken: String;
var
	i:	Integer;
begin
	i := pos(delim, str);
	if i > 0 then
		begin
			token := copy(str, 1, (i - 1));
			str := copy(str, (i + 1), length(str));
		end
	else
		begin
			token := str;
			str := '';
		end;
	dec(num);
	nextToken := token;
end;

function TTokenizer.numTokens: Integer;
begin
	numTokens := num;
end;

function TTokenizer.restOfTokens: String;
begin
	restOfTokens := str;
	str := '';
	num := 0;
end;

end.

Reply via email to