home *** CD-ROM | disk | FTP | other *** search
/ Clickx 22 / Clickx 22.iso / DATA / amc_install.exe / {app} / Scripts / StringUtils1.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  2005-05-14  |  4.1 KB  |  147 lines

  1. unit StringUtils1;
  2.  
  3. const StringUtils1_Version = 2;
  4.  
  5. {
  6.   PLEASE READ THIS BEFORE MODIFYING THIS FILE :
  7.   If you want to put some of your functions in an external
  8.   file because they are common to several of your scripts,
  9.   please create a new file your your functions
  10.   (e.g. StringUtils2 if they are also functions to work on
  11.   strings) rather than add them in this file.
  12.   So each "unit" (.pas files) belong to one script creator
  13.   and there is no risk that two people modify it at the
  14.   same time.
  15. }
  16.  
  17. {
  18.   This file was created by Antoine Potten, originally
  19.   for IMDB script.
  20.   Of course, you can use these functions in your scripts,
  21.   they're made for that. Simply add "StringUtils1" in the
  22.   uses clause of your script.
  23. }
  24.  
  25. {
  26.   History
  27.   -------
  28.   v.2:  Added a version number, few minor changes in the functions,
  29.         and the FindLine function used by lots of old scripts (they 
  30.         would all have to be modified to use this file).
  31. }
  32.  
  33. var
  34.   RemainingText: string;
  35.   {
  36.     when calling...     this variable contains...
  37.     ------------        ----------------------
  38.     TextBefore          the text after SearchText
  39.     TextBetween         the text after AfterText
  40.   }
  41.  
  42. // ***** Like the Pos function, but returns the last occurence instead of the first one *****
  43.  
  44. function LastPos(ASearch: string; AText: string): Integer;
  45. var
  46.   CurPos, PrevPos: Integer;
  47. begin
  48.   PrevPos := 0;
  49.   CurPos := Pos(ASearch, AText);
  50.   while CurPos > 0 do
  51.   begin
  52.     if PrevPos = 0 then
  53.       PrevPos := CurPos
  54.     else
  55.       PrevPos := PrevPos + CurPos + Length(ASearch) - 1;
  56.     Delete(AText, 1, CurPos + Length(ASearch) - 1);
  57.     CurPos := Pos(ASearch, AText);
  58.   end;
  59.   Result := PrevPos;
  60. end;
  61.  
  62. // *****
  63. {    Returns the text before SearchText, but not before BeginLimit (if it is not empty),
  64.     It takes the last occurence of BeginLimit found before the position of SearchText  }
  65.  
  66. function TextBefore(WholeText: string; SearchText: string; BeginLimit: string): string;
  67. var
  68.   FoundPos, PrevPos: Integer;
  69.   WorkText: string;
  70. begin
  71.   RemainingText := WholeText;
  72.   Result := '';
  73.   FoundPos := Pos(SearchText, WholeText);
  74.   if FoundPos = 0 then
  75.     Exit;
  76.   WorkText := Copy(WholeText, 1, FoundPos - 1);
  77.   RemainingText := Copy(WholeText, FoundPos + Length(SearchText), Length(WholeText));
  78.   if BeginLimit <> '' then
  79.   begin
  80.     FoundPos := LastPos(BeginLimit, WorkText);
  81.     if FoundPos = 0 then
  82.       Exit
  83.     else
  84.       FoundPos := FoundPos + Length(BeginLimit);
  85.   end
  86.   else
  87.     FoundPos := 1;
  88.   Result := Copy(WorkText, FoundPos, Length(WorkText));
  89. end;
  90.  
  91. // ***** Returns the text after SearchText *****
  92.  
  93. function TextAfter(WholeText: string; SearchText: string): string;
  94. var
  95.   FoundPos: Integer;
  96. begin
  97.   Result := '';
  98.   FoundPos := Pos(SearchText, WholeText);
  99.   if FoundPos = 0 then
  100.     Exit;
  101.   Result := Copy(WholeText, FoundPos + Length(SearchText), Length(WholeText));
  102. end;
  103.  
  104. // *****
  105. {    Returns the text between BeforeText and AfterText (without these two strings),
  106.      It takes the first AfterText occurence found after the position of BeforeText  }
  107.  
  108. function TextBetween(WholeText: string; BeforeText: string; AfterText: string): string;
  109. var
  110.   FoundPos: Integer;
  111.   WorkText: string;
  112. begin
  113.   RemainingText := WholeText;
  114.   Result := '';
  115.   FoundPos := Pos(BeforeText, WholeText);
  116.   if FoundPos = 0 then
  117.     Exit;
  118.   WorkText := Copy(WholeText, FoundPos + Length(BeforeText), Length(WholeText));
  119.   FoundPos := Pos(AfterText, WorkText);
  120.   if FoundPos = 0 then
  121.     Exit;
  122.   Result := Copy(WorkText, 1, FoundPos - 1);
  123.   RemainingText := Copy(WorkText, FoundPos + Length(AfterText), Length(WorkText));
  124. end;
  125.  
  126. // *****
  127. {    Searches for a partial text of one of the items of a TStringList
  128.      Returns -1 if not found   }
  129.  
  130. function FindLine(Pattern: string; List: TStringList; StartAt: Integer): Integer;
  131. var
  132.   i: Integer;
  133. begin
  134.   result := -1;
  135.   if StartAt < 0 then
  136.     StartAt := 0;
  137.   for i := StartAt to List.Count-1 do
  138.     if Pos(Pattern, List.GetString(i)) <> 0 then
  139.     begin
  140.       result := i;
  141.       Break;
  142.     end;
  143. end;
  144.  
  145. begin
  146. end.
  147.