{$I OVC.INC}

{$B-} {Complete Boolean Evaluation}
{$I+} {Input/Output-Checking}
{$P+} {Open Parameters}
{$T-} {Typed @ Operator}
{$W-} {Windows Stack Frame}
{$X+} {Extended Syntax}

{$IFNDEF Win32}
{$G+} {286 Instructions}
{$N+} {Numeric Coprocessor}
{$C MOVEABLE,DEMANDLOAD,DISCARDABLE}
{$ENDIF}

{*********************************************************}
{*                   OVCINTL.PAS 2.00                    *}
{*     Copyright (c) TurboPower Software Co 1995-96      *}
{*                 All rights reserved.                  *}
{*********************************************************}

unit OvcIntl;
  {-internation date/time support class}

interface

uses
  {$IFDEF Win32} Windows, {$ELSE} WinTypes, WinProcs, {$ENDIF}
  Classes, Forms, Messages, SysUtils,
  OvcConst, OvcData, OvcMisc, OvcStr, STDate;

type
  TCurrencySt = array[0..5] of AnsiChar;

  TIntlData = record
    {substitution strings for semi-literal mask characters}
    CurrencyLtStr : TCurrencySt; {corresponding string for 'c'}
    CurrencyRtStr : TCurrencySt; {corresponding string for 'C'}
    DecimalChar   : AnsiChar;    {character used for decimal point}
    CommaChar     : AnsiChar;    {character used for comma}
    {format specifiers for currency masks}
    CurrDigits    : Byte;        {number of dec places in currency}
    SlashChar     : AnsiChar;    {date seperator}
    {characters that represent boolean values}
    TrueChar      : AnsiChar;
    FalseChar     : AnsiChar;
    YesChar       : AnsiChar;
    NoChar        : AnsiChar;
  end;

type
  TOvcIntlSup = class(TObject)
  {.Z+}
  protected {private}
    FAutoUpdate      : Boolean;     {true to reset settings when win.ini changes}

    {substitution strings for semi-literal mask characters}
    FCurrencyLtStr   : TCurrencySt; {corresponding string for 'c'}
    FCurrencyRtStr   : TCurrencySt; {corresponding string for 'C'}
    FDecimalChar     : AnsiChar;    {character used for decimal point}

    {general international settings}
    FCommaChar       : AnsiChar;    {character used for comma}
    FCurrencyDigits  : Byte;        {number of dec places in currency}
    FListChar        : AnsiChar;    {list serarater}
    FSlashChar       : AnsiChar;    {character used to separate dates}

    {characters that represent boolean values}
    FTrueChar        : AnsiChar;
    FFalseChar       : AnsiChar;
    FYesChar         : AnsiChar;
    FNoChar          : AnsiChar;

    {event variables}
    FOnWinIniChange  : TNotifyEvent; {notify of win.ini changes}

    {internal working variables}
    intlHandle       : hWnd;  {our window handle}
    w1159            : array[0..5] of AnsiChar;
    w2359            : array[0..5] of AnsiChar;
    wColonChar       : AnsiChar;
    wCountry         : PAnsiChar;
    wCurrencyForm    : Byte;
    wldSub1          : array[0..5] of AnsiChar;
    wldSub2          : array[0..5] of AnsiChar;
    wldSub3          : array[0..5] of AnsiChar;
    wLongDate        : array[0..39] of AnsiChar;
    wNegCurrencyForm : Byte;
    wShortDate       : array[0..29] of AnsiChar;
    wTLZero          : Boolean;

    {property methods}
    function GetCountry : string;
      {-return the country setting}
    procedure SetAutoUpdate(Value : Boolean);
      {-set the AutoUpdate property}

    {internal methods}
    procedure isExtractFromPicture(Picture, S : PAnsiChar; Ch : AnsiChar;
              var I : Integer; Blank, Default : Integer);
    procedure isIntlWndProc(var Msg : TMessage);
    function isMaskCharCount(P : PAnsiChar; MC : AnsiChar) : Word;
    procedure isMergeIntoPicture(Picture : PAnsiChar; Ch : AnsiChar; I : Integer);
    procedure isMergePictureSt(Picture, P : PAnsiChar; MC : AnsiChar; SP : PAnsiChar);
    procedure isPackResult(Picture, S : PAnsiChar);
    procedure isSubstChar(Picture : PAnsiChar; OldCh, NewCh : AnsiChar);
    procedure isSubstCharSim(P : PAnsiChar; OC, NC : AnsiChar);
    function isTimeToTimeStringPrim(Dest, Picture : PAnsiChar; T : TStTime;
             Pack : Boolean; t1159, t2359 : PAnsiChar) : PAnsiChar;
  {.Z-}

  public
    constructor Create;
    destructor Destroy;
      override;

    function CurrentDateString(const Picture : string;
      Pack : Boolean) : string;
  {.Z+}
    function CurrentDatePChar(Dest : PAnsiChar; Picture : PAnsiChar;
      Pack : Boolean) : PAnsiChar;
      {-returns today's date as a string of the specified form}
  {.Z-}

    function CurrentTimeString(const Picture : string; Pack : Boolean) : string;
  {.Z+}
    function CurrentTimePChar(Dest : PAnsiChar; Picture : PAnsiChar; Pack : Boolean) : PAnsiChar;
      {-returns current time as a string of the specified form}
  {.Z-}

    function DateToDateString(const Picture : string; Julian : TStDate;
      Pack : Boolean) : string;
  {.Z+}
    function DateToDatePChar(Dest : PAnsiChar; Picture : PAnsiChar; Julian : TStDate;
      Pack : Boolean) : PAnsiChar;
  {.Z-}
      {-convert Julian to a string of the form indicated by Picture}

    function DateStringToDMY(const Picture, S : string; var Day, Month, Year : Integer;
      Epoch : Integer) : Boolean;
  {.Z+}
    function DatePCharToDMY(Picture, S : PAnsiChar; var Day, Month, Year : Integer;
      Epoch : Integer) : Boolean;
  {.Z-}
      {-extract day, month, and year from S, returning true if string is valid}

    function DateStringIsBlank(const Picture, S : string) : Boolean;
  {.Z+}
    function DatePCharIsBlank(Picture, S : PAnsiChar) : Boolean;
  {.Z-}
      {-return True if the month, day, and year in S are all blank}

    function DateStringToDate(const Picture, S : string; Epoch : Integer) : TStDate;
  {.Z+}
    function DatePCharToDate(Picture, S : PAnsiChar; Epoch : Integer) : TStDate;
  {.Z-}
      {-convert St, a string of the form indicated by Picture, to a julian date. Picture and St must be of equal lengths}

    function DayOfWeekToString(WeekDay : TDayType) : string;
  {.Z+}
    function DayOfWeekToPChar(Dest : PAnsiChar; WeekDay : TDayType) : PAnsiChar;
  {.Z-}
      {-return a string for the specified day of the week}

    function DMYtoDateString(const Picture : string;
      Day, Month, Year : Integer; Pack : Boolean; Epoch : Integer) : string;
  {.Z+}
    function DMYtoDatePChar(Dest : PAnsiChar; Picture : PAnsiChar;
      Day, Month, Year : Integer; Pack : Boolean; Epoch : Integer) : PAnsiChar;
  {.Z-}
      {-merge the month, day, and year into the picture}

    function InternationalCurrency(FormChar : AnsiChar; MaxDigits : Byte; Float,
                                   AddCommas, IsNumeric : Boolean) : string;
  {.Z+}
    function InternationalCurrencyPChar(Dest : PAnsiChar; FormChar : AnsiChar;
                                        MaxDigits : Byte; Float,
                                        AddCommas, IsNumeric : Boolean) : PAnsiChar;
  {.Z-}
      {-return a picture mask for a currency string, based on Windows' intl info}

    function InternationalDate(ForceCentury : Boolean) : string;
  {.Z+}
    function InternationalDatePChar(Dest : PAnsiChar; ForceCentury : Boolean) : PAnsiChar;
  {.Z-}
      {-return a picture mask for a short date string, based on Windows' international information}

    function InternationalLongDate(ShortNames : Boolean; ExcludeDOW : Boolean) : string;
  {.Z+}
    function InternationalLongDatePChar(Dest : PAnsiChar; ShortNames : Boolean; ExcludeDOW : Boolean) : PAnsiChar;
  {.Z-}
      {-return a picture mask for a date string, based on Windows' international information}

    function InternationalTime(ShowSeconds : Boolean) : string;
  {.Z+}
    function InternationalTimePChar(Dest : PAnsiChar; ShowSeconds : Boolean) : PAnsiChar;
  {.Z-}
      {-return a picture mask for a time string, based on Windows' international information}

    function MonthStringToMonth(const S : string; Width : Byte) : Byte;
  {.Z+}
    function MonthPCharToMonth(S : PAnsiChar; Width : Byte) : Byte;
  {.Z-}
      {-Convert the month name in S to a month (1..12)}

    function MonthToString(Month : Integer) : string;
  {.Z+}
    function MonthToPChar(Dest : PAnsiChar; Month : Integer) : PAnsiChar;
  {.Z-}
      {return month name as a string for Month}

    procedure ResetInternationalInfo;
      {-read string resources and update internal info to match Windows'}

    function TimeStringToHMS(const Picture, S : string; var Hour, Minute, Second : Integer) : Boolean;
  {.Z+}
    function TimePCharToHMS(Picture, S : PAnsiChar; var Hour, Minute, Second : Integer) : Boolean;
  {.Z-}
      {-extract Hours, Minutes, Seconds from St, returning true if string is valid}

    function TimeStringToTime(const Picture, S : string) : TStTime;
  {.Z+}
    function TimePCharToTime(Picture, S : PAnsiChar) : TStTime;
  {.Z-}
      {-convert S, a string of the form indicated by Picture, to a Time variable}

    function TimeToTimeString(const Picture : string; T : TStTime; Pack : Boolean) : string;
  {.Z+}
    function TimeToTimePChar(Dest : PAnsiChar; Picture : PAnsiChar; T : TStTime; Pack : Boolean) : PAnsiChar;
  {.Z-}
      {-convert T to a string of the form indicated by Picture}

    function TimeToAmPmString(const Picture : string; T : TStTime; Pack : Boolean) : string;
  {.Z+}
    function TimeToAmPmPChar(Dest : PAnsiChar; Picture : PAnsiChar; T : TStTime; Pack : Boolean) : PAnsiChar;
  {.Z-}
      {-convert T to a string of the form indicated by Picture. Times are always displayed in am/pm format.}

    property AutoUpdate : Boolean
      read FAutoUpdate
      write SetAutoUpdate;

    property CurrencyLtStr : TCurrencySt
      read FCurrencyLtStr
      write FCurrencyLtStr;

    property CurrencyRtStr : TCurrencySt
      read FCurrencyRtStr
      write FCurrencyRtStr;

    property DecimalChar : AnsiChar
      read FDecimalChar
      write FDecimalChar;

    property CommaChar : AnsiChar
      read FCommaChar
      write FCommaChar;

    property Country : string
      read GetCountry;

    property CurrencyDigits : Byte
      read FCurrencyDigits
      write FCurrencyDigits;

    property ListChar : AnsiChar
      read FListChar
      write FListChar;

    property SlashChar : AnsiChar
      read FSlashChar
      write FSlashChar;

    property TrueChar : AnsiChar
      read FTrueChar
      write FTrueChar;

    property FalseChar : AnsiChar
      read FFalseChar
      write FFalseChar;

    property YesChar : AnsiChar
      read FYesChar
      write FYesChar;

    property NoChar  : AnsiChar
      read FNoChar
      write FNoChar;

    property OnWinIniChange : TNotifyEvent
      read FOnWinIniChange
      write FOnWinIniChange;
  end;

const
  DefaultIntlData : TIntlData = (
    {substitution strings for semi-literal mask characters}
    CurrencyLtStr : '$'; {corresponding string for 'c'}
    CurrencyRtStr : '';  {corresponding string for 'C'}
    DecimalChar   : '.'; {character used for decimal point}
    CommaChar     : ','; {character used for comma}
    {format specifiers for currency masks}
    CurrDigits    : 2;   {number of dec places in currency}
    SlashChar     : '/'; {date seperator}
    {characters that represent boolean values}
    TrueChar      : 'T';
    FalseChar     : 'F';
    YesChar       : 'Y';
    NoChar        : 'N');

var
  {global default international support object}
  OvcIntlSup : TOvcIntlSup;



implementation
