home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-05-04 | 12.7 KB | 383 lines | [TEXT/PJMM] |
- {This document is formated in monaco 9 pt }
- { }
- {LEGAL STUFF }
- { }
- {Copyright © 1994 by University of Melbourne. All Rights Reserved. This work is }
- {provided "as is" and without any express or implied warranties, including, }
- {without limitation, the implied warranties of merchantability and fitness }
- {for a particular purpose. }
- { }
- {University of Melbourne is not responsible for the consequences of the use of this}
- {work, regardless of the cause. You may use this work in a public domain, }
- {freeware, or shareware product with no restrictions, as long as you include }
- {the following notice in your product's about box or splash screen: }
- { "Portions Copyright © 1994 by University of Melbourne". }
- {If you use more than 50 lines of this work, please credit the author also: }
- { "Portions by Michael Cutter" }
- {Public domain is defined as something that you release to the public, without }
- {copyright and without restrictions on use. Freeware is a copyrighted work, }
- {for which you charge no money. Shareware is a copyrighted work for which you }
- {charge a fee if the user decides to keep it. If you intend to use this work }
- {in a commercial product, please contact us. }
- { }
- { }
- {OTHER STUFF }
- { }
- {AUTHOR: }
- { Michael Trevor Cutter }
- { }
- {CONTACT: }
- { Internet: }
- { mtc@arbld.unimelb.edu.au (Preferred) }
- { Snail Mail: }
- { Dept of Architecture & Building }
- { University of Melbourne }
- { Parkville VIC 3052 }
- { AUSTRALIA }
- { }
- {PERSONAL STUFF }
- { I'd really appreciate it if you'd let me know what you're using my code }
- { in, (send me email or a postcard). Please report any bugs or errors to me. }
- { }
- {MODULE DESCRIPTION }
- {This unit provides facilities for converting between different things. Such as }
- {between a string and a boolean, or a number to a string. Some of these may appear }
- {a little redundant, such as MCNumToString, but I designed these because I'd much }
- {rather just call: }
- { SetXString(MCNumToString(num)); }
- {than: }
- { var }
- { str:string; }
- {... }
- { NumToStr(num,str); }
- { SetXString(str); }
- {Your taste may vary. }
-
- unit MCConversions;
- {Contains the conversion functions I use most often}
-
- interface
- {Convert a boolean to an integer, 1 to true, 0 for false}
- function MCBoolToInt (x: boolean): integer;
-
- {Convert a boolean to a string, 'true' or 'false'}
- function MCBoolToStr (x: boolean): string;
-
- {Convert a boolean to yes or no, true = 'Yes', false = 'No'}
- function MCBoolToYesNo (x: boolean): string;
-
- {Convert a boolean to on or off, true = 'On', false = 'Off'}
- function MCBoolToOnOff (x: boolean): string;
-
- {Convert a string to a boolean, 't[rue]' = true, 'f[alse]' = false}
- function MCStrToBool (s: string): boolean;
-
- {Convert an integer value to a string}
- function MCNumToString (thenum: longint): string;
-
- {Convert an integer to hexadecimal, digits describes ? I wrote that a long time }
- {ago, I'm sure it had a purpose. Obviously, you only get 8 chars out of a longint.}
- function MCNumToHex (mynum: longint;
- digits: integer): string;
-
- {Convert a string to an integer value}
- function MCStringToNum (thestr: string): longint;
-
- {Convert a string to all upper case}
- procedure MCStringToUpper (var str: string);
-
- {Invert a boolean, i.e. true becomes false}
- function MCBoolInvert (x: boolean): boolean;
-
- {Convert a string representing a number in cents as dollars (including dollar sign)}
- function MCStrCentsToDollars (cents: string): string;
-
- {Convert a number in cents as dollars (including dollar sign)}
- function MCIntCentsToDollars (cents: longint): string;
-
- {Rough and ready conversion, just shifts non-ASCII into visible ASCII range}
- function MCStrToASCII (str: string): string;
-
- {Given a Macintosh filename, return a safe 8-char DOS file name, avoiding all weird chars etc}
- {This leaves you room to add a meaningful suffix such as .txt on the end}
- function MCMacToDOSFileName (str: string): str32;
-
- implementation
-
- function MCBoolToInt;
- {returns an integer boolean}
- begin
- case x of
- true:
- MCBoolToInt := 1;
- otherwise
- MCBoolToInt := 0;
- end;
- end;
-
- function MCBoolToStr;
- begin
- case x of
- true:
- MCBoolToStr := 'true';
- otherwise
- MCBoolToStr := 'false';
- end;
- end;
-
- function MCBoolToYesNo;
- begin
- case x of
- true:
- MCBoolToYesNo := 'yes';
- otherwise
- MCBoolToYesNo := 'no';
- end;
- end;
-
- function MCBoolToOnOff;
- begin
- case x of
- true:
- MCBoolToOnOff := 'on';
- otherwise
- MCBoolToOnOff := 'off';
- end;
- end;
-
- function MCStrToBool;
- begin
- MCStrToBool := false;
- if length(s) > 0 then
- if (s[1] = 't') or (s[1] = 'T') then
- MCStrToBool := true;
- end;
-
- function MCNumToString;
- var
- tmpstr: str255;
- begin
- NumToString(thenum, tmpstr);
- MCNumToString := tmpstr;
- end;
-
- function MCStringToNum;
- var
- thenum: longint;
- begin
- StringToNum(thestr, thenum);
- MCStringToNum := thenum;
- end;
-
- {takes a longint, and converts it to a string of up to 8 digits}
- function MCNumToHex (mynum: longint;
- digits: integer): string;
- var
- i, j: integer;
- s: Str255;
- begin
- s := '';
- if digits > 8 then
- digits := 8;
- for i := 1 to digits do
- begin
- j := mynum mod 16; {get remainder}
- if j <= 9 then
- s := concat(MCNumToString(j), s)
- else
- s := concat(chr(j - 10 + ord('A')), s);
- mynum := mynum div 16;
- end;
- end;
-
-
- {-------------------------------------------------------------------}
-
- procedure MCStringToUpper;
- var
- i, len, ordi: integer;
- begin
- i := 0;
- len := length(str);
- while i <= len do
- begin
- ordi := ord(str[i]);
- if (97 <= ordi) and (ordi <= 122) then
- str[i] := chr(ordi - 32);
- i := i + 1;
- end;
- end;
-
- function MCBoolInvert (x: boolean): boolean;
- begin
- case x of
- false:
- MCBoolInvert := true;
- otherwise
- MCBoolInvert := false;
- end;
- end;
-
- function MCStrCentsToDollars;
- begin
- {handle case of < $1.00}
- if length(cents) = 1 then
- cents := concat('$00', cents)
- else if length(cents) = 2 then
- cents := concat('$0', cents)
- else
- cents := concat('$', cents);
- insert('.', cents, length(cents) - 1); {insert the decimal point}
- MCStrCentsToDollars := cents;
- end;
-
- function MCIntCentsToDollars;
- var
- dolstr: str255;
- begin
- dolstr := MCNumToString(cents);
- {handle special case of < $1.00 e.g. 5, or 67}
- if length(dolstr) = 1 then
- dolstr := concat('$00', dolstr)
- else if length(dolstr) = 2 then
- dolstr := concat('$0', dolstr)
- else
- dolstr := concat('$', dolstr);
- insert('.', dolstr, length(dolstr) - 1); {insert the decimal point}
-
- MCIntCentsToDollars := dolstr;
- end;
-
- function MCStrToASCII (str: string): string;
- {rough and ready method}
- var
- post, offset: longint;
- texttosearch: StringHandle;
- chartoblah: ptr;
- chartoput: str255;
- i: integer;
- begin
- texttosearch := NewString(str);
- if texttosearch = nil then
- begin
- MCStrToASCII := '';
- exit(MCStrToASCII);
- end;
- for i := 128 to 255 do
- begin
- chartoblah := NewPtr(1);
- chartoblah^ := i;
- post := Munger(Handle(texttosearch), 1, chartoblah, 1, nil, 0);
- if post >= 0 then
- begin
- chartoput := chr(i - 96);
- {shifts all visible characters 'Ä' — 'ÿ' into the range ' ' — 'x'}
- {of course, this assumes that we are only dealing}
- {with the characters in the standard set}
- offset := 1; {don't change the length byte}
- while post >= 0 do
- begin
- offset := post;
- post := Munger(Handle(texttosearch), offset, chartoblah, 1, Pointer(ord4(@chartoput) + 1), 1);
- end;
- end;
- DisposePtr(chartoblah);
- end;
- MCStrToASCII := texttosearch^^;
- end;
-
- function MCMacToDOSFileName (str: string): str32;
- {nice method of returning eight character dos name}
- {strips all funny chars, all brackets, /,\, & spaces, converts to lower case}
- {and returns an 8 character file name even if started with only 1 or 2}
- var
- post, offset: longint;
- texttosearch: StringHandle;
- chartoblah: ptr;
- chartoput, tmpstr: str255;
- i: integer;
- function BadToGoodChar (inchar: integer): char;
- {optimised for file names, removes everything except '-', '_', and lowercase chars.}
- begin
- case inchar of
- {convert ascii symbols, e.g. brackets etc}
- 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47:
- BadToGoodChar := '_';{random :-)}
- 58, 59, 60, 61, 62, 63, 64:
- BadToGoodChar := '_'; {see above :-)}
- {convert the funny characters}
- 128, 129, 174, 203, 204, 229, 231:
- BadToGoodChar := 'a';
- 130:
- BadToGoodChar := 'c';
- 131, 230, 232, 233:
- BadToGoodChar := 'e';
- 132:
- BadToGoodChar := 'n';
- 133, 175, 189, 205, 206, 238, 239, 241:
- BadToGoodChar := 'o';
- 134, 242, 243, 244:
- BadToGoodChar := 'u';
- 135, 136, 137, 138, 139, 140, 187, 190, 240, 171, 212, 213:
- BadToGoodChar := 'a';
- 141, 162, 169:
- BadToGoodChar := 'c';
- 142, 143, 144, 145:
- BadToGoodChar := 'e';
- 146, 147, 148, 149, 245:
- BadToGoodChar := 'i';
- 150:
- BadToGoodChar := 'n';
- 151, 152, 153, 154, 155, 161, 165, 188, 191, 207, 215, 219, 251:
- BadToGoodChar := 'o';
- 156, 157, 158, 159, 181:
- BadToGoodChar := 'u';
- 160, 170:
- BadToGoodChar := 't';
- 163:
- BadToGoodChar := 'l';
- 164, 225, 176, 228, 183, 186:
- BadToGoodChar := 's';
- 166, 184:
- BadToGoodChar := 'p';
- 167:
- BadToGoodChar := 'b';
- 168:
- BadToGoodChar := 'r';
- 30, 31, 33, 91, 92, 93, 94, 95, 96, 123, 124, 125, 126, 127, 172, 194, 201, 192, 193, 208, 209, 210, 211, 253, 246, 247, 214, 218, 248, 173, 197, 177, 178, 199, 220, 179, 200, 221:
- BadToGoodChar := '_';
- 180, 217:
- BadToGoodChar := 'y';
- 182, 198:
- BadToGoodChar := 'd';
- 185:
- BadToGoodChar := 'p';
- 195, 249:
- BadToGoodChar := 'v';
- 196, 223:
- BadToGoodChar := 'f';
- 216:
- BadToGoodChar := 'y';
- 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 32, 202, 222, 255, 224, 226, 227, 250, 252, 254:
- BadToGoodChar := '_';
- 234, 235, 236, 237:
- BadToGoodChar := 'l';
- {convert capitals}
- 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90:
- BadToGoodChar := chr(inchar + 32);
- otherwise
- BadToGoodChar := chr(inchar);
- end;
- end;
-
- begin
- for i := 1 to length(str) do
- str[i] := BadToGoodChar(ord4(str[i]));
- post := length(str);
- if post < 8 then
- for i := 1 to 8 - post do
- str := concat(str, '_');
- tmpstr := copy(str, 1, 8);
- MCMacToDOSFileName := tmpstr;
- end;
- end.