home *** CD-ROM | disk | FTP | other *** search
/ Bila Vrana / BILA_VRANA.iso / 005A / 4UTILS85.ZIP / STRINGDA.PAS < prev    next >
Pascal/Delphi Source File  |  1995-05-07  |  14KB  |  491 lines

  1. UNIT StringDateHandling;
  2. {$F+} (* I'am using procedural variables! *)
  3. (* ----------------------------------------------------------------------
  4.    Part of 4DESC - A Simple 4DOS File Description Editor
  5.        and 4FF   - 4DOS File Finder
  6.  
  7.        David Frey,         & Tom Bowden
  8.        Urdorferstrasse 30    1575 Canberra Drive
  9.        8952 Schlieren ZH     Stone Mountain, GA 30088-3629
  10.        Switzerland           USA
  11.  
  12.        Code created using Turbo Pascal 7.0, (c) Borland International 1992
  13.  
  14.    DISCLAIMER: This unit is freeware: you are allowed to use, copy
  15.                and change it free of charge, but you may not sell or hire
  16.                this part of 4DESC. The copyright remains in our hands.
  17.  
  18.                If you make any (considerable) changes to the source code,
  19.                please let us know. (send a copy or a listing).
  20.                We would like to see what you have done.
  21.  
  22.                We, David Frey and Tom Bowden, the authors, provide absolutely
  23.                no warranty of any kind. The user of this software takes the
  24.                entire risk of damages, failures, data losses or other
  25.                incidents.
  26.  
  27.  
  28.        Code created using Turbo Pascal 6.0 (c) Borland International 1990
  29.  
  30.    This unit provides the string handling and the date/time handling.
  31.  
  32.    ----------------------------------------------------------------------- *)
  33.  
  34. INTERFACE USES Dos;
  35.  
  36. TYPE  DateStr    = STRING[8];  (* 'mm-dd-yy','dd.mm.yy' or 'yy/mm/dd' *)
  37.       TimeStr    = STRING[6];  (* 'hh:mmp' or 'hh:mm'                 *)
  38.  
  39. VAR   DateFormat: DateStr; (* 'mm-dd-yy','dd.mm.yy','yy/mm/dd' or 'ddmmmyy' *)
  40.       TimeFormat: TimeStr; (* 'hh:mmp' or 'hh:mm'                           *)
  41.  
  42. (* String handling routines. The strings can be converted to lower/upper-
  43.    case. National characters will be converted.                           *)
  44.  
  45. FUNCTION  Chars(c: CHAR; Count: BYTE): STRING;
  46. FUNCTION  DownCase(C: CHAR): CHAR;
  47. FUNCTION  DownStr(s: STRING): STRING;
  48. PROCEDURE DownString(VAR s: STRING);
  49. FUNCTION  UpStr(s: STRING): STRING;
  50. PROCEDURE UpString(VAR s: STRING);
  51.  
  52. PROCEDURE StripLeadingSpaces(VAR s: STRING);
  53. PROCEDURE StripTrailingSpaces(VAR s: STRING);
  54.  
  55. (* Date/Time handling routines. Date/Time and Numbers will be formatted
  56.    in accordance with your COUNTRY=-settings in CONFIG.SYS.               *)
  57.  
  58. TYPE  FormDateFunc = FUNCTION (DateRec: DateTime) : DateStr;
  59.       FormTimeFunc = FUNCTION (DateRec: DateTime) : TimeStr;
  60.  
  61. VAR   FormDate : FormDateFunc;
  62.       FormTime : FormTimeFunc;
  63.  
  64.  
  65. FUNCTION FormattedIntStr(nr: WORD;minlength: BYTE): STRING;
  66. FUNCTION FormattedLongIntStr(nr: LONGINT;minlength: BYTE): STRING;
  67.  
  68. PROCEDURE EvaluateINIFileSettings;
  69.  
  70. IMPLEMENTATION USES HandleINIFile;
  71.  
  72. CONST MonthName: ARRAY[1..12] OF STRING[3] =
  73.                   ('Jan','Feb','Mar','Apr','May','Jun',
  74.                    'Jul','Aug','Sep','Oct','Nov','Dec');
  75.  
  76. CONST DateSep  : CHAR = '.';
  77.       TimeSep  : CHAR = ':';
  78.       MilleSep : CHAR = '''';
  79.  
  80. VAR   Buffer: ARRAY[0..15] OF CHAR;
  81.       (* Buffer for country code information.
  82.          This buffer may not be moved into GetCountryInfo,
  83.          since MS-DOS needs the address of this buffer!    *)
  84.  
  85. (*-------------------------------------------------------- String-Handling *)
  86. FUNCTION Chars(c: CHAR; Count: BYTE): STRING; ASSEMBLER;
  87. (* Concats Count times the character c *)
  88.  
  89. ASM
  90.  LES DI,@Result
  91.  MOV AL,&Count
  92.  CLD
  93.  STOSB
  94.  MOV CL,AL
  95.  XOR CH,CH
  96.  MOV AL,&c
  97.  REP STOSB
  98. END;
  99.  
  100. FUNCTION  DownCase(C: CHAR): CHAR; ASSEMBLER;
  101. (* Returns the character c in lower case, national characters will not
  102.    be handled correctly. [we will use this function to lowercase file
  103.    names and DOS doesn't like special characters in filenames anyway] *)
  104.  
  105. ASM
  106.   MOV AL,&c
  107.   CMP AL,'A'
  108.   JB  @@9                  (* No conversion below 'A'                     *)
  109.   CMP AL,'Z'
  110.   JA  @@9                  (* Conversion between 'A' and 'Z'              *)
  111.   ADD AL,$20
  112. @@9:
  113. END;                       (* finished. *)
  114.  
  115. FUNCTION  DownStr(s: STRING): STRING; ASSEMBLER;
  116. (* Returns the string s in lower case, national characters will not
  117.    be handled correctly. [we will use this function to lowercase file
  118.    names and DOS doesn't like special characters in filenames anyway] *)
  119.  
  120. ASM
  121.  PUSH DS
  122.  CLD
  123.  LDS SI,s
  124.  LES DI,@Result
  125.  LODSB
  126.  STOSB
  127.  XOR AH,AH
  128.  XCHG AX,CX
  129.  JCXZ @11
  130. @10:
  131.  LODSB
  132.  CMP AL,'A'
  133.  JB  @@9                  (* No conversion below 'A'                     *)
  134.  CMP AL,'Z'
  135.  JA  @@9                  (* Conversion between 'A' and 'Z'              *)
  136.  ADD AL,$20
  137. @@9:
  138.  STOSB
  139.  LOOP @10
  140. @11:
  141.  POP DS
  142. END;
  143.  
  144.  
  145. PROCEDURE DownString(VAR s: STRING);
  146. (* Returns the string s in lower case, national characters will not
  147.    be handled correctly. [we will use this function to lowercase file
  148.    names and DOS doesn't like special characters in filenames anyway] *)
  149.  
  150. VAR i : BYTE;
  151.  
  152. BEGIN
  153.  FOR i := 1 TO Length(s) DO s[i] := DownCase(s[i]);
  154. END;
  155.  
  156.  
  157. FUNCTION  UpStr(s: STRING): STRING; ASSEMBLER;
  158. (* Returns the string s in upper case, national characters will not
  159.    be handled correctly.                                              *)
  160.  
  161. ASM
  162.  PUSH DS
  163.  CLD
  164.  LDS SI,s
  165.  LES DI,@Result
  166.  LODSB
  167.  STOSB
  168.  XOR AH,AH
  169.  XCHG AX,CX
  170.  JCXZ @11
  171. @10:
  172.  LODSB
  173.  CMP AL,'a'
  174.  JB @@9
  175.  CMP AL,'z'
  176.  JA @@9
  177.  SUB AL,20H
  178. @@9:
  179.  STOSB
  180.  LOOP @10
  181. @11:
  182.  POP DS
  183. END;
  184.  
  185. PROCEDURE UpString(VAR s: STRING);
  186. (* Returns the string s in upper case, national characters will not
  187.    be handled correctly.                                              *)
  188.  
  189. VAR l : BYTE;
  190.  
  191. BEGIN
  192.  FOR l := 1 TO Length(s) DO s[l] := UpCase(s[l]);
  193. END;
  194.  
  195. PROCEDURE StripLeadingSpaces(VAR s: STRING);
  196.  
  197. BEGIN
  198.  WHILE (Length(s) > 0) AND (s[1] = ' ') DO System.Delete(s,1,1);
  199. END;
  200.  
  201. PROCEDURE StripTrailingSpaces(VAR s: STRING);
  202.  
  203. VAR l : BYTE;
  204.  
  205. BEGIN
  206.  l := Length(s);
  207.  WHILE (l>0) AND (s[l] = ' ') DO
  208.   BEGIN System.Delete(s,l,1); l := Length(s); END;
  209. END;
  210.  
  211. (*-------------------------------------------------------- Date-Handling *)
  212.  
  213. (* Various Date/Time format utilities to suit national date/time formats *)
  214.  
  215. FUNCTION FormDateEuropean(DateRec: DateTime): DateStr;
  216.  
  217. VAR MonStr, DayStr, YearStr : STRING[2];
  218.     res                     : DateStr;
  219.  
  220. BEGIN
  221.  Str(DateRec.Day:2, DayStr);
  222.  
  223.  Str(DateRec.Month:2, MonStr);
  224.  IF DateRec.Month < 10 THEN MonStr[1] := '0';
  225.  
  226.  DateRec.Year := DateRec.Year MOD 100;
  227.  Str(DateRec.Year:2, YearStr);
  228.  IF DateRec.Year < 10 THEN YearStr[1] := '0';
  229.  
  230.  FormDateEuropean := DayStr + DateSep + MonStr + DateSep + YearStr;
  231. END;
  232.  
  233. FUNCTION FormDateUS(DateRec: DateTime): DateStr;
  234.  
  235. VAR MonStr, DayStr, YearStr : STRING[2];
  236.     res                     : DateStr;
  237.  
  238. BEGIN
  239.  Str(DateRec.Day:2, DayStr);
  240.  IF DateRec.Day < 10 THEN DayStr[1] := '0';
  241.  
  242.  Str(DateRec.Month:2, MonStr);
  243.  
  244.  DateRec.Year := DateRec.Year MOD 100;
  245.  Str(DateRec.Year:2, YearStr);
  246.  IF DateRec.Year < 10 THEN YearStr[1] := '0';
  247.  
  248.  FormDateUS := MonStr + DateSep + DayStr + DateSep + YearStr;
  249. END;
  250.  
  251. FUNCTION FormDateJapanese(DateRec: DateTime): DateStr;
  252.  
  253. VAR MonStr, DayStr, YearStr : STRING[2];
  254.     res                     : DateStr;
  255.  
  256. BEGIN
  257.  Str(DateRec.Day:2, DayStr);
  258.  IF (DateRec.Day < 10) THEN DayStr[1] := '0';
  259.  
  260.  Str(DateRec.Month:2, MonStr);
  261.  IF (DateRec.Month < 10) THEN MonStr[1] := '0';
  262.  
  263.  DateRec.Year := DateRec.Year MOD 100;
  264.  Str(DateRec.Year:2, YearStr);
  265.  IF DateRec.Year < 10 THEN YearStr[1] := '0';
  266.  
  267.  FormDateJapanese := YearStr + DateSep + MonStr + DateSep + DayStr;
  268. END;
  269.  
  270. FUNCTION FormDateMyOwn(DateRec: DateTime): DateStr;
  271.  
  272. VAR DayStr, YearStr : STRING[2];
  273.     res             : DateStr;
  274.  
  275. BEGIN
  276.  Str(DateRec.Day:2, DayStr);
  277.  
  278.  DateRec.Year := DateRec.Year MOD 100;
  279.  Str(DateRec.Year:2, YearStr);
  280.  IF DateRec.Year < 10 THEN YearStr[1] := '0';
  281.  
  282.  FormDateMyOwn := DayStr + MonthName[DateRec.Month] + YearStr;
  283. END;
  284.  
  285. FUNCTION FormTime12(DateRec: DateTime): TimeStr;
  286.  
  287. VAR HourStr, MinStr, SecStr : STRING[2];
  288.     amflag                  : CHAR;
  289.     res                     : TimeStr;
  290.  
  291. BEGIN
  292.  IF DateRec.Hour < 12 THEN amflag := 'a'
  293.