Saving, editing and loading information. The information that I want to load is something I will add myself. Each line of information will contain 4 pieces, (string, integer, string, integer). Via 4 seperate edit boxes and a button I will add this information to a 'database' (not sure if I need a database or if it can be done via something like a Tstringlist). Everytime the button is clicked it will added the content that is typed at that moment in the 'database'.
The only demand of the saved data is when I type the first string from the list it could place the rest of the information that belongs to it in a memobox or edit boxes as well. So I suppose I have to be able to search. Just want to keep it as simple as possible. There will only be about 10 to 15 lines of information. and if possible it would be good if I can load them again a later time.
Here's some very basic code that should get you on your way. There's no error checking, and you'll no doubt want to develop it and modify it further. The point is that there should be some ideas to help you write code that works for you.
Now that I have comma-separated the fields, but made no attempt to handle the appearance of commas in any of the values. If this is a problem then choose a different delimiter, or escape the commas. I had toyed with writing each field on its own line (effectively using a newline as the separator), but this makes the reading code more tricky to write.
Again, the main point is that this is not final production code, but is intended to give you a starting point.
function Split(const s: string; Separator: char): TStringDynArray;
var
i, ItemIndex: Integer;
len: Integer;
SeparatorCount: Integer;
Start: Integer;
begin
len := Length(s);
if len=0 then begin
Result := nil;
exit;
end;
SeparatorCount := 0;
for i := 1 to len do begin
if s[i]=Separator then begin
inc(SeparatorCount);
end;
end;
SetLength(Result, SeparatorCount+1);
ItemIndex := 0;
Start := 1;
for i := 1 to len do begin
if s[i]=Separator then begin
Result[ItemIndex] := Copy(s, Start, i-Start);
inc(ItemIndex);
Start := i+1;
end;
end;
Result[ItemIndex] := Copy(s, Start, len-Start+1);
end;
type
TValue = record
i1, i2: Integer;
s: string;
end;
TMyDict = class(TDictionary<string,TValue>)
public
procedure SaveToFile(const FileName: string);
procedure LoadFromFile(const FileName: string);
end;
{ TMyDict }
procedure TMyDict.SaveToFile(const FileName: string);
var
Strings: TStringList;
Item: TPair<string,TValue>;
begin
Strings := TStringList.Create;
Try
for Item in Self do begin
Strings.Add(Format(
'%s,%s,%d,%d',
[Item.Key, Item.Value.s, Item.Value.i1, Item.Value.i2]
));
end;
Strings.SaveToFile(FileName);
Finally
FreeAndNil(Strings);
End;
end;
procedure TMyDict.LoadFromFile(const FileName: string);
var
Strings: TStringList;
Item: TPair<string,TValue>;
Line: string;
Fields: TStringDynArray;
begin
Strings := TStringList.Create;
Try
Strings.LoadFromFile(FileName);
for Line in Strings do begin
Fields := Split(Line, ',');
Assert(Length(Fields)=4);
Item.Key := Fields[0];
Item.Value.s := Fields[1];
Item.Value.i1 := StrToInt(Fields[2]);
Item.Value.i2 := StrToInt(Fields[3]);
Add(Item.Key, Item.Value);
end;
Finally
FreeAndNil(Strings);
End;
end;
Note that you don't attempt to search the file on disk. You simply load it into memory, into the dictionary and look things up from there.
A dictionary is great when you always use the same key. If you have multiple keys then a dictionary is less convenient, but who cares about the performance impact if you've only got 15 records?!
Disclaimer: I've not run the code, I've not tested it, etc. etc.
Related
I am reading and writing data from a file using a filestream but am having a problem reading strings from my file.
In a test VCL form program I have written:
procedure tform1.ReadfromFile4;
var
fs: TFileStream;
arrayString: Array of String;
i, Len1 : Cardinal;
// s : string;
begin
fs := TFileStream.Create('C:\Users\Joe\Documents\Delphi\Streamtest.tst',
fmOpenRead or fmShareDenyWrite);
Memo1.lines.clear;
try
fs.ReadBuffer(Len1, SizeOf(Len1));
SetLength(arrayString, Len1);
FOR i := 0 to Len1-1 do begin
fs.ReadBuffer(Len1, SizeOf(Len1));
SetLength(arrayString[i], Len1);
Fs.ReadBuffer(arrayString[i], Len1);
memo1.lines.add (arrayString[i]);
end;
finally
fs.free;
end;
end;
procedure tform1.WriteToFile4;
var
fs: TFileStream;
arrayString: Array of String;
Len1, c, i: Cardinal;
begin
Memo1.lines.clear;
SetLength(arrayString, 4);
arrayString[0] := 'First string in this Array';
arrayString[1] := 'the Second Array string';
arrayString[2] := 'String number three of this Array';
arrayString[3] := 'this is the fourth String';
fs := TFileStream.Create('C:\Users\Joe\Documents\Delphi\Streamtest.tst',
fmCreate or fmOpenWrite or fmShareDenyWrite);
try
c := Length(arrayString);
Fs.WriteBuffer(c, SizeOf(c));
for i := 0 to c-1 do begin
Len1 := Length(arrayString[i]);
fs.WriteBuffer(Len1, SizeOf(Len1));
if Len1 > 0 then begin
fs.WriteBuffer(arrayString[i], Len1);
end;
end;
finally
fs.free;
end;
end;
The Save button action enters the four strings correctly, but the Load button (readFromFile4) fails to load the strings from the file. Using the Watch list, I find that the string lengths are set correctly for each string, but the data accessed is not the correct string values. I believe I am faithfully following the instructions on the website : http://www.angelfire.com/hi5/delphizeus/customfiles.html]1 in the section titled
Writing and Reading Dynamic Arrays of Non-Fixed Size Variables
Can anyone shed light on why this does not read the strings from the file correctly?
I am running this code:
function CmdLineParamExists(const Value: string): Boolean;
var
I: Integer;
begin
Result := False;
for I := 1 to ParamCount do
begin
if CompareText(Copy(ParamStr(I), 1, Length(Value)), Value) = 0 then
begin
Result := True;
Exit;
end;
end;
end;
function GetAppName(Value: string): string;
begin
if CmdLineParamExists('/COMPONENTS=prog2') then
begin
Result := 'Программа 2'; //<----This shows up as ????????? 2
end
else
begin
Result := '{#SetupSetting("AppName")}';
end;
end;
procedure CurPageChanged(CurPageID: Integer);
var
S: string;
Begin
if CurPageID = wpSelectDir then
begin
S := SetupMessage(msgSelectDirLabel3);
StringChange(S, '[name]', GetAppName(''));
WizardForm.SelectDirLabel.Caption := S;
end;
end;
Now, I'm not sure what I'm doing wrong here. Every other string shows up correctly, except when I use the result of GetAppName. Should I convert anything to AnsiString at some point?
I'm assuming that you are using Ansi version of Inno Setup.
In the Ansi version, the culprit is probably the StringChange as it does not play nicely with non-Ansi character sets. Try using StringChangeEx.
Though you should be using Unicode version of Inno Setup anyway.
Only the most recent version of Inno Setup, 5.6, does support Unicode string literals. So make sure you have the latest version.
If you are stuck with an older version:
Encode the string like
#$041F#$0440#$043E#$0433#$0440#$0430#$043C#$043C#$0430 + ' 2'
Or, actually the most correct way is to add a new custom message to the language files (like Russian.isl):
[CustomMessages]
Program2=Программа 2
And load it like:
CustomMessage('Program2')
procedure TForm1.bFAT1Click(sender: TObject);
var
FAT: Integer;
begin
for FAT := 0 to memo1.lines.Count - 1 do
begin
if AnsiContainsStr(memo1.lines[FAT], 'Olive Oil') then
begin
ShowMessage('Olive Oil exist!');
end;
end;
// But how to show message if integer is empty?
end;
I want to do something if no line contains 'Olive Oil'. How to do it?
What you need is an Exit statement to leave the procedure as soon as you found a matching element. That way, when you reach the end of the procedure, you know that you have not found a matching element:
for FAT := 0 to memo1.lines.Count - 1 do
begin
if AnsiContainsStr(memo1.lines[FAT], 'Olive Oil') then
begin
ShowMessage('Olive Oil exist!')
Exit; // we can stop here since we found it
end;
end;
// we only come here if no line contained 'Olive Oil' (because of the EXIT)
ShowMessage('Olive Oil does not exist!');
Edit: (inspired by #David) It is good practice to separate your logic from the UI / display (for example ShowMessage). To do that you can define a function like this:
function IndexOfLineContaining(const Text : String; Lines : TStrings) : Integer;
begin
for Result := 0 to Lines.Count - 1 do
if AnsiContainsStr(Lines[Result], Text) then
Exit;
Result := -1;
end;
On top of that you could easily define a boolean function:
function HasLineContaining(const Text : String; Lines : TStrings) : Boolean;
begin
Result := (IndexOfLineContaining(Text, Lines) > -1);
end;
and use that to do your message display:
if HasLineContaining('Olive Oil', Memo1.Lines) then
ShowMessage ('foo')
else
ShowMessage ('bar');
I suggest that you work a bit on your terminology to make your questions clearer. An integer cannot be empty and the sentence " do something if [FAT] do not found 'Olive Oil'." with FAT being an integer does not make any sense.
Something strange happens when I try to pass strings from the Lines of a TMemo control to an array of PChar. At the end of the routine, the last string in the array is duplicated. I was able to replicate this in this simple code:
procedure Test;
var
i: smallint;
arr: array of PAnsiChar;
strarr: array[0..1] of string;
begin
SetLength(arr, 2);
strarr[0] := 'abbb';
strarr[1] := 'baaa';
for i := 0 to Length(strarr) do
arr[i] := PAnsiChar(AnsiString(strarr[i]));
end;
If I run this procedure step by step, I can see arr[0] = 'abbb' however, at the end of the rutine, both values, arr[0] and arr[1] equal to baaa. I guess it has something to do with the typecast.
Can anyone see what is wrong ?
There are two problems with your code:
Your loop is exceeding the upper bound of the array. It needs to use for i := 0 to Length(strarr)-1 do or for i := 0 to High(strarr) do instead.
More importantly, when you type-cast an AnsiString to a PAnsiChar, it returns a pointer to the AnsiString's internal data if the AnsiString is not empty. You are type-casting a UnicodeString to an AnsiString and grabbing a pointer into it, so the compiler has to use a compiler-generated local variable for the AnsiString data. In other words, your code is effectively doing the same thing as the following:
procedure Test;
var
i: smallint;
arr: array of PAnsiChar;
strarr: array[0..1] of string;
compiler_temp: AnsiString;
begin
SetLength(arr, 2);
strarr[0] := 'abbb';
strarr[1] := 'baaa';
for i := 0 to Length(strarr) do
begin
compiler_temp := AnsiString(strarr[i]);
arr[i] := PAnsiChar(compiler_temp);
end;
end;
Depending on how the memory for compiler_temp gets managed by the RTL memory manager at run-time, it is certainly possible for arr[0] and arr[1] to end up pointing at the same physical memory block in this situation.
If you want an array of PAnsiChar values then you need to start with an array of Ansi data for them to point at:
procedure Test;
var
i: Integer;
arr: array of PAnsiChar;
strarr: array[0..1] of AnsiString;
begin
SetLength(arr, 2);
strarr[0] := 'abbb';
strarr[1] := 'baaa';
for i := 0 to Length(strarr)-1 do
arr[i] := PAnsiChar(strarr[i]);
end;
I have a string of delimited text ie:
Value1:Value2:Value3:Value4:Value5:Value6
How would I extract, for example, a specific value Ie:
Label.caption := GetValuefromDelimitedText(2); to get Value2
Thanks in advance
Paul
Something like that - if you like compact code (but not as performant as Davids):
function GetValueFromDelimitedText(const s: string; Separator: char; Index: Integer): string;
var sl : TStringList;
begin
Result := '';
sl := TStringList.Create;
try
sl.Delimiter := Separator;
sl.DelimitedText := s;
if sl.Count > index then
Result := sl[index];
finally
sl.Free;
end;
end;
Hope that helps
This should do it:
function GetValueFromDelimitedText(
const s: string;
const Separator: char;
const Index: Integer
): string;
var
i, ItemIndex, Start: Integer;
begin
ItemIndex := 1;
Start := 1;
for i := 1 to Length(s) do begin
if s[i]=Separator then begin
if ItemIndex=Index then begin
Result := Copy(s, Start, i-Start);
exit;
end;
inc(ItemIndex);
Start := i+1;
end;
end;
if ItemIndex=Index then begin
Result := Copy(s, Start, Length(s)-Start+1);
end else begin
Result := '';
end;
end;
This version allows you to specify the separator, you would obviously pass ':'. If you ask for an item beyond the end then the function will return the empty string. You could change that to an exception if you preferred. Finally, I have arranged that this uses 1-based indexing as per your example, but I personally would choose 0-based indexing.
If using Delphi XE or higher you can also use StrUtils.SplitString like this:
function GetValueFromDelimitedText (const Str: string; Separator: Char; Index: Integer) : string;
begin
Result := SplitString (Str, Separator) [Index];
end;
In production code, you should check that Index is indeed a valid index.
This method returns a TStringDynArray (a dynamic array of strings) so you can also use it like this (using enumerators):
for Str in SplitString (Str, Separator) do
Writeln (Str);
which can be very useful IMHO.