home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / turbo4 / mcutil.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1987-12-08  |  10.3 KB  |  371 lines

  1.  
  2. {           Copyright (c) 1985, 87 by Borland International, Inc.            }
  3.  
  4. unit MCUTIL;
  5.  
  6. interface
  7.  
  8. uses Crt, Dos, MCVars;
  9.  
  10. function Pad(S : String; Len : Word) : String;
  11. { Bringt einen String durch Anhängen von Leerzeichen auf die angegebene Länge }
  12. function Spaces(Num : Word) : String;
  13. { Liefert einen String mit der angegebenen Anzahl von Leerzeichen zurück }
  14. function UpperCase(S : String) : String;
  15. { Übersetzt in Großbuchstaben }
  16.  
  17. function WordToString(Num, Len : Word) : String;
  18. { Konvertiert einen Wert des Typs Word in einen String }
  19. function RealToString(Num : Real; Len, Places : Word) : String;
  20. { Konvertiert eine Realzahl in einen String }
  21.  
  22. function AllocText(Col, Row : Word; S : String) : Boolean;
  23. { Belegt Speicherplatz für eine Zelle mit Text }
  24. function AllocValue(Col, Row : Word; Amt : Real) : Boolean;
  25. { Belegt Speicherplatz für eine Zelle mit einem Wert }
  26. function AllocFormula(Col, Row : Word; S : String; Amt : Real) : Boolean;
  27. { Belegt Speicherplatz für eine Zelle mit einer Formel }
  28.  
  29. function RowWidth(Row : Word) : Word;
  30. { Liefert die Breite (Zeichenzahl) einer Reihe }
  31.  
  32. function FormulaStart(Input : String; Place : Word;
  33.                       var Col, Row, FormLen : Word) : Boolean;
  34. { Liefert TRUE, wenn der Beginn des Strings als Formel interpretiert
  35.   werden kann, ansonsten FALSE. Für Formeln werden zusätzlich Spalte,
  36.   Zeile und Länge des Textes zurückgeliefert }
  37.  
  38. function ColString(Col : Word) : String;
  39. { Konvertiert eine Spaltennummer in einen String }
  40. function CenterColString(Col : Word) : String;
  41. { Konvertiert eine Spaltennummer in einen mittenzentrierten String }
  42.  
  43. function TextString(InString : String; Col, FValue : Word;
  44.                     Formatting : Boolean) : String;
  45. { Bestimmt die Darstellung von Strings als Text }
  46. function ValueString(CPtr : CellPtr; Value : Real; Col, FValue : Word;
  47.                      var Color : Word; Formatting : Boolean) : String;
  48. { Bestimmt die Darstellung von Werten als Text }
  49. function CellString(Col, Row : Word; var Color : Word;
  50.                     Formatting : Boolean) : String;
  51. { Erzeugt den String zur Darstellung der Daten, die in der durch col und
  52.   row angegebenen Zelle gespeichert sind; liefert außerdem die Farbe
  53.   dieser Zelle zurück }
  54.  
  55. procedure Switch(var Val1, Val2 : Word);
  56. { Vvertauscht die beiden übergebenen Werte miteinander }
  57.  
  58. procedure InitVars;
  59. { Initialisiert diverse globale Variablen }
  60.  
  61. function Exists(FileName : String) : Boolean;
  62. { Liefert TRUE, wenn FileName existiert bzw. gefunden werden kann }
  63.  
  64. implementation
  65.  
  66. {$F+}
  67. function HeapFunc(Size : Word) : Word;  { Behandlung von Fehlern bei }
  68. begin                                   { der dynamischen Speicherverwaltung }
  69.   HeapFunc := 1;       { New und GetMem liefern im Fehlerfall nil zurück }
  70. end;
  71. {$F-}
  72.  
  73. function Pad;
  74. begin
  75.   if Length(S) < Len then
  76.     FillChar(S[Succ(Length(S))], Len - Length(S), ' ');
  77.   S[0] := Chr(Len);
  78.   Pad := S;
  79. end;
  80.  
  81. function Spaces;
  82. var  S: String;
  83. begin
  84.   S[0] := Chr(Num);
  85.   FillChar(S[1], Num, ' ');
  86.   Spaces := S;
  87. end;
  88.  
  89. function UpperCase;
  90. var Counter: Word;
  91. begin
  92.   for Counter := 1 to Length(S) do
  93.     S[Counter] := UpCase(S[Counter]);
  94.   UpperCase := S;
  95. end;
  96.  
  97. function WordToString;
  98. var S: String[5];
  99. begin
  100.   Str(Num:Len, S);
  101.   WordToString := S;
  102. end;
  103.  
  104. function RealToString;
  105. var S: String[80];
  106. begin
  107.   Str(Num:Len:Places, S);
  108.   RealToString := S;
  109. end;
  110.  
  111. function AllocText;
  112. var CPtr: CellPtr;
  113. begin
  114.   AllocText := False;  { Annahme: Belegung nicht möglich }
  115.   GetMem(CPtr, Length(S) + 3);  { Versuch der Belegung, liefert via HeapFunc }
  116.   if CPtr = nil then Exit;   { den Wert nil zurück -> Exit mit FALSE }
  117.   CPtr^.Attrib := TXT;
  118.   CPtr^.Error := False;
  119.   CPtr^.T := S;           { ansonsten Initialisierung }
  120.   Cell[Col, Row] := CPtr; { Eintrag ins Zell-Array }
  121.   AllocText := True;      { Platz für die Zelle ist belegt }
  122. end;
  123.  
  124. function AllocValue;
  125. var CPtr : CellPtr;
  126. begin
  127.   AllocValue := False;
  128.   GetMem(CPtr, SizeOf(Real) + 2);  { derselbe Mechanismus wie bei AllocText }
  129.   if CPtr = nil then Exit;
  130.   CPtr^.Attrib := VALUE; CPtr^.Error := False;
  131.   CPtr^.Value := Amt;  Cell[Col, Row] := CPtr;
  132.   AllocValue := True;
  133. end;
  134.  
  135. function AllocFormula;
  136. var CPtr : CellPtr;
  137. begin
  138.   AllocFormula := False;
  139.   GetMem(CPtr, Length(S) + SizeOf(Real) + 3);  { derselbe Mechanismus ... }
  140.   if CPtr = nil then Exit;
  141.   CPtr^.Attrib := FORMULA;  CPtr^.Error := False;
  142.   CPtr^.Formula := S;  CPtr^.FValue := Amt;
  143.   Cell[Col, Row] := CPtr;
  144.   AllocFormula := True;
  145. end;
  146.  
  147. function RowWidth;
  148. begin
  149.   RowWidth := Succ(Trunc(Ln(Row) / Ln(10)));
  150. end;
  151.  
  152. function FormulaStart;
  153. var
  154.   OldPlace, Len, MaxLen : Word;
  155.   Start : IString;
  156.   NumString : String[10];
  157. begin
  158.   FormulaStart := False;
  159.   OldPlace := Place;
  160.   MaxLen := RowWidth(MAXROWS);
  161.   if not (Input[Place] in LETTERS) then Exit;  { Keine Zelladresse -> Exit }
  162.   Col := Succ(Ord(Input[Place]) - Ord('A'));
  163.   Inc(Place);
  164.   if Input[Place] in LETTERS then
  165.   begin
  166.     Col := Col * 26;
  167.     Col := Succ(Col + Ord(Input[Place]) - Ord('A'));
  168.     Inc(Place);
  169.   end;
  170.   if Col > MAXCOLS then Exit;   { keine gültige Spalte }
  171.   Start := Copy(Input, Place, MaxLen);
  172.   Len := 0;
  173.   while (Place <= Length(Input)) and
  174.         (Input[Place] in ['0'..'9']) and (Len < MaxLen) do
  175.   begin
  176.     Inc(Len);
  177.     Inc(Place);
  178.   end;
  179.   if Len = 0 then Exit;
  180.   NumString := Copy(Start, 1, Len);
  181.   Val(NumString, Row, Len);
  182.   if Row > MAXROWS then Exit;   { keine gültige Zeile }
  183.   FormLen := Place - OldPlace;
  184.   FormulaStart := True;
  185. end;
  186.  
  187. function ColString;
  188. begin
  189.   if Col <= 26 then ColString := Chr(Pred(Col) + Ord('A'))
  190.   else ColString := Chr((Pred(Col) div 26) + Pred(Ord('A'))) +
  191.                Chr((Pred(Col) mod 26) + Ord('A'));
  192. end;
  193.  
  194. function CenterColString;
  195. var
  196.   S : String[2];
  197.   Spaces1, Spaces2 : Word;
  198. begin
  199.   S := ColString(Col);
  200.   Spaces1 := (ColWidth[Col] - Length(S)) shr 1;
  201.   Spaces2 := ColWidth[Col] - Length(S) - Spaces1;
  202.   CenterColString := Spaces(Spaces1) + S + Spaces(Spaces2);
  203. end;
  204.  
  205. function TextString;
  206. var
  207.   OutString : String[80];
  208. begin
  209.   if ((FValue and RJUSTIFY) <> 0) and Formatting then
  210.   begin
  211.     OutString := InString;
  212.     if Length(OutString) < ColWidth[Col] then
  213.     begin
  214.       while Length(OutString) < ColWidth[Col] do
  215.         OutString := ' ' + OutString;
  216.     end
  217.     else OutString[0] := Chr(ColWidth[Col]);
  218.   end
  219.   else begin
  220.     if Formatting then OutString := Pad(InString, ColWidth[Col])
  221.     else OutString := InString;
  222.   end;
  223.   TextString := OutString;
  224. end;
  225.  
  226. function ValueString;
  227. var
  228.   VString : String[MAXCOLWIDTH];
  229.   FString : String[3];
  230.   Width, P : Word;
  231. begin
  232.   if Formatting then
  233.   begin
  234.     Str(CPtr^.Value:1:(FValue and 15), VString);
  235.     if (FValue and COMMAS) <> 0 then
  236.     begin
  237.       P := Pos('.', VString);
  238.       if P = 0 then P := Succ(Length(VString));
  239.       while P > 4 do
  240.       begin
  241.         P := P - 3;
  242.         if VString[Pred(P)] <> '-' then Insert(',', VString, P);
  243.       end;
  244.     end;
  245.     if (FValue and DOLLAR) <> 0 then
  246.     begin
  247.       if VString[1] = '-' then
  248.       begin
  249.         FString := ' $';
  250.         Width := ColWidth[Col] - 2;
  251.       end
  252.       else begin
  253.         FString := ' $ ';
  254.         Width := ColWidth[Col] - 3;
  255.       end;
  256.     end
  257.     else begin
  258.       Width := ColWidth[Col];
  259.       FString := '';
  260.     end;
  261.     if (FValue and RJUSTIFY) <> 0 then
  262.     begin
  263.       if Length(VString) > Width then Delete(VString, Succ(Width),
  264.                                              Length(VString) - Width)
  265.       else while Length(VString) < Width do
  266.               VString := ' ' + VString;
  267.     end
  268.     else VString := Pad(VString, Width);
  269.     VString := FString + VString;
  270.   end
  271.   else Str(Value:1:MAXPLACES, VString);
  272.   Color := VALUECOLOR;
  273.   ValueString := VString;
  274. end;
  275.  
  276. function CellString;
  277. var
  278.   CPtr : CellPtr;
  279.   OldCol, P, NewCol, FormatValue : Word;
  280.   S : String[80];
  281.   V : Real;
  282. begin
  283.   CPtr := Cell[Col, Row];
  284.   if CPtr = nil then
  285.   begin
  286.     if (not Formatting) or (Format[Col, Row] < OVERWRITE) then
  287.     begin
  288.       S := Spaces(ColWidth[Col]);
  289.       Color := BLANKCOLOR;
  290.     end
  291.     else begin
  292.       NewCol := Col;
  293.       Dec(NewCol);
  294.       while Cell[NewCol, Row] = nil do
  295.         Dec(NewCol);
  296.       OldCol := NewCol;
  297.       P := 1;
  298.       while (NewCol < Col) do
  299.       begin
  300.         Inc(P, ColWidth[NewCol]);
  301.         Inc(NewCol);
  302.       end;
  303.       S := Copy(Cell[OldCol, Row]^.T, P, ColWidth[Col]);
  304.       S := S + Spaces(ColWidth[Col] - Length(S));
  305.       Color := TXTCOLOR;
  306.     end;
  307.   end
  308.   else begin
  309.     FormatValue := Format[Col, Row];
  310.     if CPtr^.Error and (Formatting or (CPtr^.Attrib = VALUE)) then
  311.     begin
  312.       S := Pad(MSGERRORTXT, ColWidth[Col]);
  313.       Color := ERRORCOLOR;
  314.     end
  315.     else case CPtr^.Attrib of
  316.           TXT : begin
  317.                    S := TextString(CPtr^.T, Col, FormatValue, Formatting);
  318.                    Color := TXTCOLOR;
  319.                  end;
  320.       FORMULA : begin
  321.                  if FormDisplay then
  322.                  begin
  323.                     S := TextString(CPtr^.Formula, Col, FormatValue,
  324.                                      Formatting);
  325.                     Color := FORMULACOLOR;
  326.                   end
  327.                 else begin
  328.                   V := CPtr^.FValue;
  329.                   S := ValueString(CPtr, V, Col, FormatValue,
  330.                                     Color, Formatting);
  331.                    end;
  332.                   end;
  333.       VALUE : begin
  334.                 V := CPtr^.Value;
  335.                 S := ValueString(CPtr, V, Col, FormatValue,
  336.                                  Color, Formatting);
  337.               end;
  338.       end; { case }
  339.   end;
  340.   CellString := S;
  341. end;
  342.  
  343. procedure Switch;
  344. var Temp : Word;
  345. begin
  346.   Temp := Val1; Val1 := Val2; Val2 := Temp;
  347. end;
  348.  
  349. procedure InitVars;
  350. begin
  351.   LeftCol := 1; TopRow := 1; CurCol := 1; Currow := 1;
  352.   LastCol := 1; LastRow := 1;
  353.   AutoCalc := True;
  354.   FormDisplay := False;
  355.   FillChar(ColWidth, SizeOf(ColWidth), DEFAULTWIDTH);
  356.   FillChar(Cell, SizeOf(Cell), 0);
  357.   FillChar(Format, SizeOf(Format), DEFAULTFORMAT);
  358. end;
  359.  
  360. function Exists;
  361. var SR: SearchRec;
  362. begin
  363.   FindFirst(FileName, ReadOnly + Hidden + SysFile, SR);
  364.   Exists := (DosError = 0) and (Pos('?', FileName) = 0) and
  365.             (Pos('*', FileName) = 0);
  366. end;
  367.  
  368. begin
  369.   HeapError := @HeapFunc;  { Installation der "Fehlerbehandlung" }
  370. end.
  371.