home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / tooltypes / tooltypes.mod < prev    next >
Text File  |  1996-07-15  |  12KB  |  408 lines

  1. (****************************************************************
  2. :Program.       ToolTypes.mod
  3. :Contents.      To get "ToolTypes" from the programm Icon
  4. :Contents.                              or CLI-Line
  5. :Contents.                              or ENV-Variable
  6. :Contents.                              or Config-File
  7. :Author.        Matthias Taube
  8. :Copyright.     PublicDomain
  9. :Language.      Oberon
  10. :Translator.    Oberon 3.00d
  11. :History.       1.5 [Taube] 10-Oct-1993 include Config-File support
  12. :History.       1.4 [Taube] 30-Sep-1993 include env-support
  13. :History.       1.3 [Taube] 29-Sep-1993 fixed Bug using empty ToolType
  14. :History.       1.2 [Taube] 16-May-1993 fixed Bug using Icon.GetToolType (pointer odd)
  15. :History.       1.1 [Taube] 02-May-1993 include Cli-Support
  16. :Address.       D-50858 Köln, Ricarda Huch Str. 8
  17. :Phone.         02234/49233
  18. :Remark.        ONLY TESTET ON A3000 - KS/WB 37.x und KS/WB 39.x
  19. *****************************************************************)
  20.  
  21. MODULE ToolTypes;
  22. IMPORT Arguments, Conversions, Dos, Exec, FileSystem, Icon, io,
  23.        OberonLib, Requests, Strings, SYSTEM, Workbench;
  24.  
  25. TYPE ConfigLinePtr = UNTRACED POINTER TO ConfigLine;
  26.          (* Konfig File wird im Ram gehalten, um es nur einmal einlesen zu
  27.             muessen *)
  28.      ConfigLine    = RECORD
  29.                        Next:ConfigLinePtr;
  30.                        Text:Exec.STRING;
  31.                      END;
  32.  
  33. VAR wbs-:           Workbench.WBStartupPtr;
  34.     MeinIcon-:      Workbench.DiskObjectPtr;
  35.     Config,ConfigW: ConfigLinePtr;
  36.  
  37. PROCEDURE CopyStr(Von:ARRAY OF CHAR;VAR Nach:ARRAY OF CHAR;AbPos:INTEGER);
  38. (* Kopiert einen String ab einer Position bis zum Ende in einen anderen String *)
  39.   VAR VonPos,NachPos:INTEGER;
  40.   BEGIN
  41.     VonPos:=AbPos;NachPos:=0;
  42.     WHILE (NachPos<LEN(Nach)) AND (Von[VonPos]#00X) DO
  43.         Nach[NachPos]:=Von[VonPos];
  44.         INC(NachPos);INC(VonPos);
  45.     END;
  46.     Nach[NachPos]:=00X;
  47.   END CopyStr;
  48.  
  49. (****i* StripQuote ********************************************************
  50. *
  51. *   NAME
  52. *      StripQuote - Strips of the Quotes of a String
  53. *
  54. *   SYNOPSIS
  55. *      StripQuote(VAR String:ARRAY OF CHAR);
  56. *
  57. *   FUNCTION
  58. *      Strips of the Quotes and leading or finishing spaces of an
  59. *      given String
  60. *
  61. *   INPUTS
  62. *      String: an ARRAY OF CHAR with the String
  63. *
  64. *   RESULT
  65. *      String: the String without Spaces
  66. *
  67. *****************************************************************************
  68. *
  69. *)
  70.  
  71. PROCEDURE StripQuote(VAR String:ARRAY OF CHAR);
  72. (* Entfernt die Anführungszeichen und die führenden und endenden Leerzeichen
  73.    aus einem String *)
  74.   VAR Pos:INTEGER;
  75.       Quote:CHAR;
  76.   BEGIN
  77.     Pos:=0;
  78.     (* führende Leerzeichen entfernen *)
  79.     WHILE (String[Pos]=" ") DO
  80.       INC(Pos);
  81.     END;
  82.     (* führendes Anführungszeichen entfernen *)
  83.     IF (String[Pos]="'") OR (String[Pos]='"') THEN
  84.        Quote:=String[Pos]; INC(Pos);
  85.     ELSE Quote:=" ";
  86.     END;
  87.     CopyStr(String,String,Pos);
  88.     Pos:=SHORT(Strings.Length(String));
  89.     WHILE ((String[Pos]=" ") OR (String[Pos]=00X)) AND (Pos>0) DO
  90.       DEC(Pos);
  91.     END;
  92.     IF String[Pos]=Quote THEN DEC(Pos); END;
  93.     String[Pos+1]:=00X;
  94.   END StripQuote;
  95.  
  96. (****** GetToolRawStr* ********************************************************
  97. *
  98. *   NAME
  99. *      GetToolRawStr - to get the String after a Congiguration Keyword without
  100. *                      any change
  101. *   SYNOPSIS
  102. *      GetToolRawStr* (Pattern:ARRAY OF CHAR;VAR in: Exec.STRING):BOOLEAN;
  103. *
  104. *   FUNCTION
  105. *      Searches for the ToolType in the Icon (or the commandline,
  106. *      if CLI-startet) and, if there not found, for an env: Variable
  107. *      with the name of the ToolType. If not there, it searches for
  108. *      the Keyword in the Configuration-file (named Progname.cfg or
  109. *      like the String in the "CONFIG" Keyword.
  110. *      The entry in the configfile must start with an "#" at the first
  111. *      column, like #TOOLTYPE=...
  112. *
  113. *   INPUTS
  114. *      Pattern: an ARRAY OF CHAR with the searched ToolType in it
  115. *
  116. *   RESULT
  117. *      in: an Exec.STRING with the raw String after the ToolType
  118. *      FuncResult: FALSE if tooltype was not found
  119. *
  120. *   EXAMPLE
  121. *
  122. *   NOTES
  123. *
  124. *   BUGS
  125. *
  126. *   SEE ALSO
  127. *
  128. *****************************************************************************
  129. *
  130. *)
  131.  
  132. PROCEDURE GetToolRawStr* (Pattern:ARRAY OF CHAR;VAR in: Exec.STRING):BOOLEAN;
  133. (* Liefert das Argument in der Variable in den String, so wie es in den ToolTypes
  134.    steht.
  135.    Beim Start vom Cli werden die Argumente nach dem String "Pattern=..."
  136.    durchsucht und danach String hinter dem Gleichheitszeichen ausgewertet.
  137.    Wenn nicht gefunden, wird nach einer env: Variable mit dem Namen gesucht. *)
  138.  
  139.  VAR  i:      INTEGER;
  140.       Ok:     BOOLEAN;
  141.       Temp:   Exec.STRPTR;
  142.       Config: ConfigLinePtr;
  143.  
  144.   PROCEDURE Vergleich(Pattern,String:ARRAY OF CHAR;VAR In:Exec.STRING):BOOLEAN;
  145.   VAR TempString:Exec.STRING;
  146.       Pos       :LONGINT;
  147.   BEGIN
  148.     In:="";
  149.     COPY(String,TempString);
  150.     Strings.Upper(TempString);
  151.     Pos:=Strings.Occurs(TempString,Pattern);
  152.     IF Pos = 0 THEN
  153.         Pos:=Strings.Length(Pattern);
  154.         WHILE (String[Pos]#"=") AND (String[Pos]#00X) DO
  155.            INC(Pos);
  156.         END;
  157.         IF String[Pos]="=" THEN
  158.            CopyStr(String,In,SHORT(Pos)+1);
  159.            RETURN TRUE;
  160.         ELSE
  161.            RETURN FALSE;
  162.         END;
  163.     END;
  164.     RETURN FALSE;
  165.   END Vergleich;
  166.  
  167.  BEGIN
  168.     Strings.Upper(Pattern);
  169.     IF MeinIcon#NIL THEN
  170. (* $OddChk- *)
  171.       Temp:=Icon.FindToolType(MeinIcon.toolTypes,Pattern);
  172.       IF Temp#NIL THEN
  173.         Exec.CopyMemAPTR(Temp,SYSTEM.ADR(in),SIZE(in));
  174.         RETURN(TRUE);
  175.       END;
  176. (* $OddChk= *)
  177.     ELSE (* Programm hat kein Icon oder wurde über den Cli gestartet *)
  178.       IF NOT OberonLib.wbStarted THEN
  179.          i:=Arguments.NumArgs();
  180.          REPEAT
  181.            Arguments.GetArg(i,in);
  182.            IF Vergleich(Pattern,in,in) THEN RETURN TRUE; END;
  183.            DEC(i);
  184.          UNTIL (i<1);
  185.        END;
  186.      END;
  187.      IF (Dos.GetVar(Pattern,in,LEN(in),LONGSET{Dos.globalOnly})#-1) THEN
  188.         RETURN TRUE;
  189.      END;
  190.      Config:=ConfigW;
  191.      WHILE Config#NIL DO
  192.         IF Vergleich(Pattern,Config^.Text,in) THEN RETURN TRUE; END;
  193.         Config:=Config^.Next;
  194.      END;
  195.      RETURN FALSE;
  196.   END GetToolRawStr;
  197.  
  198. (****** GetToolStr* ********************************************************
  199. *
  200. *   NAME
  201. *      GetToolStr - to get the String after the tooltype without the "" or ''
  202. *
  203. *   SYNOPSIS
  204. *      GetToolStr* (Pattern:ARRAY OF CHAR;VAR in: ARRAY OF CHAR):BOOLEAN;
  205. *
  206. *   FUNCTION
  207. *      Searches for the ToolType in the Icon (or the commandline,
  208. *      if CLI-startet) and, if there not found, for an env: Variable
  209. *      with the name of the ToolType.
  210. *      If not found, the Configfile (named ProgName.cfg) is used and
  211. *      scanned for an parameter like #TOOLTYPE = ...
  212. *    THIS IS NEEDET IN FIRST COLUMN--^
  213. *      The name of the configfile may be changed by using the CONFIG=
  214. *      ToolType
  215. *
  216. *   INPUTS
  217. *      Pattern: an ARRAY OF CHAR with the searched ToolType in it
  218. *
  219. *   RESULT
  220. *      in: an ARRAY OF CHAR with the String after the ToolType without "" or ''
  221. *      FuncResult: FALSE if tooltype was not found
  222. *
  223. *   EXAMPLE
  224. *
  225. *   NOTES
  226. *
  227. *   BUGS
  228. *
  229. *   SEE ALSO
  230. *
  231. *****************************************************************************
  232. *
  233. *)
  234.  
  235. PROCEDURE GetToolString*(Pattern:ARRAY OF CHAR;VAR in:ARRAY OF CHAR):BOOLEAN;
  236. (* von dem String hinter ToolType werden die Tüttelchen entfernt, er
  237.    wird in einen beliebigen String kopiert. *)
  238.  
  239.   VAR TMPStr:Exec.STRING;
  240.   BEGIN
  241.     IF GetToolRawStr(Pattern,TMPStr) THEN
  242.       StripQuote(TMPStr);
  243.       CopyStr(TMPStr,in,0);
  244.       RETURN(TRUE);
  245.     END;
  246.     in:="";
  247.     RETURN(FALSE);
  248.   END GetToolString;
  249.  
  250.  
  251. (****** GetToolInt* ********************************************************
  252. *
  253. *   NAME
  254. *      GetToolInt - to get integer ToolTypes
  255. *
  256. *   SYNOPSIS
  257. *      GetToolInt*(Pattern:ARRAY OF CHAR;Default:LONGINT):LONGINT
  258. *
  259. *   FUNCTION
  260. *      Searches for the ToolType in the Icon (or the commandline,
  261. *      if CLI-startet) and, if there not found, for an env: Variable
  262. *      with the name of the ToolType or an entry in the Configfile
  263. *      and converts the String to an INTEGER-value.
  264. *      If not found, uses the given default.
  265. *
  266. *   INPUTS
  267. *      Pattern: an ARRAY OF CHAR with the searched ToolType in it
  268. *      Default: the default-value if tooltype not found
  269. *
  270. *   RESULT
  271. *      The value after tooltype (or default, if not found or not convertable)
  272. *
  273. *   EXAMPLE
  274. *
  275. *   NOTES
  276. *
  277. *   BUGS
  278. *
  279. *   SEE ALSO
  280. *
  281. *****************************************************************************
  282. *
  283. *)
  284.  
  285. PROCEDURE GetToolInt*(Pattern:ARRAY OF CHAR;Default:LONGINT):LONGINT;
  286. (* Wertet Tools aus, die Integerargumente beinhalten *)
  287.  
  288.   VAR Temp:Exec.STRING;
  289.       Aus:LONGINT;
  290.   BEGIN
  291.     IF GetToolString(Pattern,Temp) THEN
  292.       IF Conversions.StringToInt(Temp,Aus) THEN RETURN(Aus); END;
  293.     END;
  294.     RETURN(Default);
  295.   END GetToolInt;
  296.  
  297. PROCEDURE OpenCfg():ConfigLinePtr;
  298. VAR
  299.   oldLock,dirLock: Dos.FileLockPtr;
  300.   file: FileSystem.File;
  301.   Pos:INTEGER;
  302.   open: BOOLEAN;
  303.   Config,ConfigNeu,ConfigWurzel:ConfigLinePtr;
  304.   BaseName,
  305.   NameExt,setting,Zeile: Exec.STRING;
  306. BEGIN
  307.   Arguments.GetArg(0,BaseName);
  308.   IF BaseName = "" THEN HALT(20); END;
  309.   Pos:=SHORT(Strings.Length(BaseName));
  310.   WHILE ((BaseName[Pos]#"/") AND (BaseName[Pos]#":")) AND (Pos>0) DO
  311.       DEC(Pos);
  312.   END;
  313.   IF Pos>0 THEN CopyStr(BaseName, BaseName,Pos+1); END;
  314.   NameExt:=BaseName;
  315.   Strings.Append(NameExt,".cfg");
  316. (* $IFNOT ClearVars *)
  317.   lock := NIL; oldLock := NIL; return := FALSE;Config:=NIL;ConfigWurzel:=NIL;
  318. (* $END *)
  319.  
  320.   IF GetToolString("CONFIG",setting) THEN         (* are any setting given? *)
  321.     open:=FileSystem.Open(file,setting,FALSE);
  322.   ELSE
  323.  
  324.     setting:="ENV:";
  325.     Strings.Append(setting,NameExt);
  326.     open:=FileSystem.Open(file,setting,FALSE);  (* ENV: *)
  327.  
  328.     IF NOT open THEN
  329.       setting:="ENV:";
  330.       Strings.Append(setting,BaseName);
  331.       Strings.AppendChar(setting,"/");
  332.       Strings.Append(setting,NameExt);
  333.       open:=FileSystem.Open(file,setting,FALSE);
  334.     END;
  335.  
  336.     IF NOT open THEN
  337.         dirLock := Dos.GetProgramDir();
  338.         IF dirLock # NIL THEN
  339.           oldLock := Dos.CurrentDir(dirLock);
  340.           open:=FileSystem.Open(file,NameExt,FALSE); (* programms home *)
  341.         END;
  342.     END;
  343.   
  344.     IF NOT open THEN
  345.         oldLock:=Dos.CurrentDir(Arguments.oldCurrentDir);
  346.         open:=FileSystem.Open(file,NameExt,FALSE);   (* current *)
  347.     END;
  348.   END;
  349.   IF open THEN
  350.     Pos:=0;
  351.     WHILE FileSystem.ReadString(file,Zeile) DO
  352.       IF Zeile[0]="#" THEN
  353.         REPEAT
  354.           NEW(ConfigNeu);
  355.           IF ConfigNeu=NIL THEN
  356.              IF ~ Requests.Request(BaseName,"Speichermangel",
  357.                           " Nochmal versuchen "," Abbruch ") THEN HALT(20)
  358.              END;
  359.           END;
  360.         UNTIL(ConfigNeu#NIL);
  361.         IF Config#NIL THEN              (* Neues Element in die Liste einhaengen *)
  362.             Config^.Next:=ConfigNeu;
  363.         ELSE
  364.             ConfigWurzel:=ConfigNeu;    (* war erste Zeile *)
  365.         END;
  366.         Config:=ConfigNeu;              (* und Liste weiterschalten *)
  367.         Config^.Next:=NIL;
  368.         Pos:=1;
  369.         WHILE(Zeile[Pos]#"=") AND (Zeile[Pos]#00X) DO
  370.             Zeile[Pos]:=Strings.CapIntl(Zeile[Pos]);
  371.             INC(Pos);
  372.         END;
  373.         CopyStr(Zeile,Config^.Text,1);
  374.       END;
  375.       Zeile:="";
  376.     END;
  377.     open:=NOT FileSystem.Close(file);
  378.   END;
  379.  
  380.   IF oldLock # NIL THEN
  381.     IF Dos.CurrentDir(oldLock) = NIL THEN END;
  382.   END;
  383.  
  384.   RETURN ConfigWurzel;
  385. END OpenCfg;
  386.  
  387. BEGIN
  388. (* $IFNOT ClearVars *)
  389.   Config:=NIL;
  390.   ConfigW:=NIL;
  391. (* $END *)
  392.   Requests.Assert(Dos.dos.lib.version>=36,"Programm benötigt dos.library V36 (OS 2.0)!");
  393.   IF OberonLib.wbStarted THEN
  394.     wbs := OberonLib.wbenchMsg; (* wird von Arguments nicht exportiert *)
  395.     MeinIcon := Icon.GetDiskObject(wbs.argList[0].name^);
  396.   ELSE MeinIcon:=NIL;wbs:=NIL;
  397.   END;
  398.   ConfigW:=OpenCfg();
  399. CLOSE
  400.   IF MeinIcon#NIL THEN Icon.FreeDiskObject(MeinIcon);END;
  401.   WHILE ConfigW#NIL DO
  402.     Config:=ConfigW^.Next;
  403.     DISPOSE(ConfigW);
  404.     ConfigW:=Config;
  405.   END;
  406. END ToolTypes.
  407.  
  408.