home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Pascal / Libraries / MCC Utils / MCConversions.p < prev    next >
Encoding:
Text File  |  1994-05-04  |  12.7 KB  |  383 lines  |  [TEXT/PJMM]

  1. {This document is formated in monaco 9 pt                                          }
  2. {                                                                                  }
  3. {LEGAL STUFF                                                                       }
  4. {                                                                                  }
  5. {Copyright © 1994 by University of Melbourne. All Rights Reserved. This work is    }
  6. {provided "as is" and without any express or implied warranties, including,        }
  7. {without limitation, the implied warranties of merchantability and fitness         }
  8. {for a particular purpose.                                                         }
  9. {                                                                                  }
  10. {University of Melbourne is not responsible for the consequences of the use of this}
  11. {work, regardless of the cause. You may use this work in a public domain,          }
  12. {freeware, or shareware product with no restrictions, as long as you include       }
  13. {the following notice in your product's about box or splash screen:                }
  14. {  "Portions Copyright © 1994 by University of Melbourne".                         }
  15. {If you use more than 50 lines of this work, please credit the author also:        }
  16. {  "Portions by Michael Cutter"                                                    }
  17. {Public domain is defined as something that you release to the public, without     }
  18. {copyright and without restrictions on use. Freeware is a copyrighted work,        }
  19. {for which you charge no money. Shareware is a copyrighted work for which you      }
  20. {charge a fee if the user decides to keep it. If you intend to use this work       }
  21. {in a commercial product, please contact us.                                       }
  22. {                                                                                  }
  23. {                                                                                  }
  24. {OTHER STUFF                                                                       }
  25. {                                                                                  }
  26. {AUTHOR:                                                                           }
  27. { Michael Trevor Cutter                                                            }
  28. {                                                                                  }
  29. {CONTACT:                                                                          }
  30. {  Internet:                                                                       }
  31. {    mtc@arbld.unimelb.edu.au (Preferred)                                          }
  32. {  Snail Mail:                                                                     }
  33. {    Dept of Architecture & Building                                               }
  34. {    University of Melbourne                                                       }
  35. {    Parkville VIC 3052                                                            }
  36. {    AUSTRALIA                                                                     }
  37. {                                                                                  }
  38. {PERSONAL STUFF                                                                    }
  39. {  I'd really appreciate it if you'd let me know what you're using my code         }
  40. {  in, (send me email or a postcard). Please report any bugs or errors to me.      }
  41. {                                                                                  }
  42. {MODULE DESCRIPTION                                                                }
  43. {This unit provides facilities for converting between different things. Such as    }
  44. {between a string and a boolean, or a number to a string. Some of these may appear }
  45. {a little redundant, such as MCNumToString, but I designed these because I'd much  }
  46. {rather just call:                                                                 }
  47. {   SetXString(MCNumToString(num));                                                }
  48. {than:                                                                             }
  49. { var                                                                              }
  50. {   str:string;                                                                    }
  51. {...                                                                               }
  52. {   NumToStr(num,str);                                                             }
  53. {   SetXString(str);                                                               }
  54. {Your taste may vary.                                                              }
  55.  
  56. unit MCConversions;
  57. {Contains the conversion functions I use most often}
  58.  
  59. interface
  60. {Convert a boolean to an integer, 1 to true, 0 for false}
  61.     function MCBoolToInt (x: boolean): integer;
  62.  
  63. {Convert a boolean to a string, 'true' or 'false'}
  64.     function MCBoolToStr (x: boolean): string;
  65.  
  66. {Convert a boolean to yes or no, true = 'Yes', false = 'No'}
  67.     function MCBoolToYesNo (x: boolean): string;
  68.  
  69. {Convert a boolean to on or off, true = 'On', false = 'Off'}
  70.     function MCBoolToOnOff (x: boolean): string;
  71.  
  72. {Convert a string to a boolean, 't[rue]' = true, 'f[alse]' = false}
  73.     function MCStrToBool (s: string): boolean;
  74.  
  75. {Convert an integer value to a string}
  76.     function MCNumToString (thenum: longint): string;
  77.  
  78. {Convert an integer to hexadecimal, digits describes ? I wrote that a long time }
  79. {ago, I'm sure it had a purpose. Obviously, you only get 8 chars out of a longint.}
  80.     function MCNumToHex (mynum: longint;
  81.                                     digits: integer): string;
  82.  
  83. {Convert a string to an integer value}
  84.     function MCStringToNum (thestr: string): longint;
  85.  
  86. {Convert a string to all upper case}
  87.     procedure MCStringToUpper (var str: string);
  88.  
  89. {Invert a boolean, i.e. true becomes false}
  90.     function MCBoolInvert (x: boolean): boolean;
  91.  
  92. {Convert a string representing a number in cents as dollars (including dollar sign)}
  93.     function MCStrCentsToDollars (cents: string): string;
  94.  
  95. {Convert a number in cents as dollars (including dollar sign)}
  96.     function MCIntCentsToDollars (cents: longint): string;
  97.  
  98. {Rough and ready conversion, just shifts non-ASCII into visible ASCII range}
  99.     function MCStrToASCII (str: string): string;
  100.  
  101. {Given a Macintosh filename, return a safe 8-char DOS file name, avoiding all weird chars etc}
  102. {This leaves you room to add a meaningful suffix such as .txt on the end}
  103.     function MCMacToDOSFileName (str: string): str32;
  104.  
  105. implementation
  106.  
  107.     function MCBoolToInt;
  108. {returns an integer boolean}
  109.     begin
  110.         case x of
  111.             true: 
  112.                 MCBoolToInt := 1;
  113.             otherwise
  114.                 MCBoolToInt := 0;
  115.         end;
  116.     end;
  117.  
  118.     function MCBoolToStr;
  119.     begin
  120.         case x of
  121.             true: 
  122.                 MCBoolToStr := 'true';
  123.             otherwise
  124.                 MCBoolToStr := 'false';
  125.         end;
  126.     end;
  127.  
  128.     function MCBoolToYesNo;
  129.     begin
  130.         case x of
  131.             true: 
  132.                 MCBoolToYesNo := 'yes';
  133.             otherwise
  134.                 MCBoolToYesNo := 'no';
  135.         end;
  136.     end;
  137.  
  138.     function MCBoolToOnOff;
  139.     begin
  140.         case x of
  141.             true: 
  142.                 MCBoolToOnOff := 'on';
  143.             otherwise
  144.                 MCBoolToOnOff := 'off';
  145.         end;
  146.     end;
  147.  
  148.     function MCStrToBool;
  149.     begin
  150.         MCStrToBool := false;
  151.         if length(s) > 0 then
  152.             if (s[1] = 't') or (s[1] = 'T') then
  153.                 MCStrToBool := true;
  154.     end;
  155.  
  156.     function MCNumToString;
  157.         var
  158.             tmpstr: str255;
  159.     begin
  160.         NumToString(thenum, tmpstr);
  161.         MCNumToString := tmpstr;
  162.     end;
  163.  
  164.     function MCStringToNum;
  165.         var
  166.             thenum: longint;
  167.     begin
  168.         StringToNum(thestr, thenum);
  169.         MCStringToNum := thenum;
  170.     end;
  171.  
  172. {takes a longint, and converts it to a string of up to 8 digits}
  173.     function MCNumToHex (mynum: longint;
  174.                                     digits: integer): string;
  175.         var
  176.             i, j: integer;
  177.             s: Str255;
  178.     begin
  179.         s := '';
  180.         if digits > 8 then
  181.             digits := 8;
  182.         for i := 1 to digits do
  183.             begin
  184.                 j := mynum mod 16; {get remainder}
  185.                 if j <= 9 then
  186.                     s := concat(MCNumToString(j), s)
  187.                 else
  188.                     s := concat(chr(j - 10 + ord('A')), s);
  189.                 mynum := mynum div 16;
  190.             end;
  191.     end;
  192.  
  193.  
  194. {-------------------------------------------------------------------}
  195.  
  196.     procedure MCStringToUpper;
  197.         var
  198.             i, len, ordi: integer;
  199.     begin
  200.         i := 0;
  201.         len := length(str);
  202.         while i <= len do
  203.             begin
  204.                 ordi := ord(str[i]);
  205.                 if (97 <= ordi) and (ordi <= 122) then
  206.                     str[i] := chr(ordi - 32);
  207.                 i := i + 1;
  208.             end;
  209.     end;
  210.  
  211.     function MCBoolInvert (x: boolean): boolean;
  212.     begin
  213.         case x of
  214.             false: 
  215.                 MCBoolInvert := true;
  216.             otherwise
  217.                 MCBoolInvert := false;
  218.         end;
  219.     end;
  220.  
  221.     function MCStrCentsToDollars;
  222.     begin
  223. {handle case of < $1.00}
  224.         if length(cents) = 1 then
  225.             cents := concat('$00', cents)
  226.         else if length(cents) = 2 then
  227.             cents := concat('$0', cents)
  228.         else
  229.             cents := concat('$', cents);
  230.         insert('.', cents, length(cents) - 1);    {insert the decimal point}
  231.         MCStrCentsToDollars := cents;
  232.     end;
  233.  
  234.     function MCIntCentsToDollars;
  235.         var
  236.             dolstr: str255;
  237.     begin
  238.         dolstr := MCNumToString(cents);
  239. {handle special case of < $1.00 e.g. 5, or 67}
  240.         if length(dolstr) = 1 then
  241.             dolstr := concat('$00', dolstr)
  242.         else if length(dolstr) = 2 then
  243.             dolstr := concat('$0', dolstr)
  244.         else
  245.             dolstr := concat('$', dolstr);
  246.         insert('.', dolstr, length(dolstr) - 1);    {insert the decimal point}
  247.  
  248.         MCIntCentsToDollars := dolstr;
  249.     end;
  250.  
  251.     function MCStrToASCII (str: string): string;
  252. {rough and ready method}
  253.         var
  254.             post, offset: longint;
  255.             texttosearch: StringHandle;
  256.             chartoblah: ptr;
  257.             chartoput: str255;
  258.             i: integer;
  259.     begin
  260.         texttosearch := NewString(str);
  261.         if texttosearch = nil then
  262.             begin
  263.                 MCStrToASCII := '';
  264.                 exit(MCStrToASCII);
  265.             end;
  266.         for i := 128 to 255 do
  267.             begin
  268.                 chartoblah := NewPtr(1);
  269.                 chartoblah^ := i;
  270.                 post := Munger(Handle(texttosearch), 1, chartoblah, 1, nil, 0);
  271.                 if post >= 0 then
  272.                     begin
  273.                         chartoput := chr(i - 96);
  274. {shifts all visible characters 'Ä' — 'ÿ' into the range ' ' — 'x'}
  275. {of course, this assumes that we are only dealing}
  276. {with the characters in the standard set}
  277.                         offset := 1; {don't change the length byte}
  278.                         while post >= 0 do
  279.                             begin
  280.                                 offset := post;
  281.                                 post := Munger(Handle(texttosearch), offset, chartoblah, 1, Pointer(ord4(@chartoput) + 1), 1);
  282.                             end;
  283.                     end;
  284.                 DisposePtr(chartoblah);
  285.             end;
  286.         MCStrToASCII := texttosearch^^;
  287.     end;
  288.  
  289.     function MCMacToDOSFileName (str: string): str32;
  290. {nice method of returning eight character dos name}
  291. {strips all funny chars, all brackets, /,\, & spaces, converts to lower case}
  292. {and returns an 8 character file name even if started with only 1 or 2}
  293.         var
  294.             post, offset: longint;
  295.             texttosearch: StringHandle;
  296.             chartoblah: ptr;
  297.             chartoput, tmpstr: str255;
  298.             i: integer;
  299.         function BadToGoodChar (inchar: integer): char;
  300. {optimised for file names, removes everything except '-', '_', and lowercase chars.}
  301.         begin
  302.             case inchar of
  303. {convert ascii symbols, e.g. brackets etc}
  304.                 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47: 
  305.                     BadToGoodChar := '_';{random :-)}
  306.                 58, 59, 60, 61, 62, 63, 64: 
  307.                     BadToGoodChar := '_'; {see above :-)}
  308. {convert the funny characters}
  309.                 128, 129, 174, 203, 204, 229, 231: 
  310.                     BadToGoodChar := 'a';
  311.                 130: 
  312.                     BadToGoodChar := 'c';
  313.                 131, 230, 232, 233: 
  314.                     BadToGoodChar := 'e';
  315.                 132: 
  316.                     BadToGoodChar := 'n';
  317.                 133, 175, 189, 205, 206, 238, 239, 241: 
  318.                     BadToGoodChar := 'o';
  319.                 134, 242, 243, 244: 
  320.                     BadToGoodChar := 'u';
  321.                 135, 136, 137, 138, 139, 140, 187, 190, 240, 171, 212, 213: 
  322.                     BadToGoodChar := 'a';
  323.                 141, 162, 169: 
  324.                     BadToGoodChar := 'c';
  325.                 142, 143, 144, 145: 
  326.                     BadToGoodChar := 'e';
  327.                 146, 147, 148, 149, 245: 
  328.                     BadToGoodChar := 'i';
  329.                 150: 
  330.                     BadToGoodChar := 'n';
  331.                 151, 152, 153, 154, 155, 161, 165, 188, 191, 207, 215, 219, 251: 
  332.                     BadToGoodChar := 'o';
  333.                 156, 157, 158, 159, 181: 
  334.                     BadToGoodChar := 'u';
  335.                 160, 170: 
  336.                     BadToGoodChar := 't';
  337.                 163: 
  338.                     BadToGoodChar := 'l';
  339.                 164, 225, 176, 228, 183, 186: 
  340.                     BadToGoodChar := 's';
  341.                 166, 184: 
  342.                     BadToGoodChar := 'p';
  343.                 167: 
  344.                     BadToGoodChar := 'b';
  345.                 168: 
  346.                     BadToGoodChar := 'r';
  347.                 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: 
  348.                     BadToGoodChar := '_';
  349.                 180, 217: 
  350.                     BadToGoodChar := 'y';
  351.                 182, 198: 
  352.                     BadToGoodChar := 'd';
  353.                 185: 
  354.                     BadToGoodChar := 'p';
  355.                 195, 249: 
  356.                     BadToGoodChar := 'v';
  357.                 196, 223: 
  358.                     BadToGoodChar := 'f';
  359.                 216: 
  360.                     BadToGoodChar := 'y';
  361.                 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 32, 202, 222, 255, 224, 226, 227, 250, 252, 254: 
  362.                     BadToGoodChar := '_';
  363.                 234, 235, 236, 237: 
  364.                     BadToGoodChar := 'l';
  365. {convert capitals}
  366.                 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: 
  367.                     BadToGoodChar := chr(inchar + 32);
  368.                 otherwise
  369.                     BadToGoodChar := chr(inchar);
  370.             end;
  371.         end;
  372.  
  373.     begin
  374.         for i := 1 to length(str) do
  375.             str[i] := BadToGoodChar(ord4(str[i]));
  376.         post := length(str);
  377.         if post < 8 then
  378.             for i := 1 to 8 - post do
  379.                 str := concat(str, '_');
  380.         tmpstr := copy(str, 1, 8);
  381.         MCMacToDOSFileName := tmpstr;
  382.     end;
  383. end.