home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / iffconverter / misc.c < prev    next >
C/C++ Source or Header  |  1997-01-07  |  4KB  |  207 lines

  1. /*
  2. **     $VER: Misc.c V0.01 (27-06-95)
  3. **
  4. **     Author:  Gerben Venekamp
  5. **     Updates: 27-06-95  Version 0.01     Intial module
  6. **
  7. **  Misc.c contains some miscellanious functions.
  8. **
  9. */
  10.  
  11.  
  12. #include <exec/memory.h>
  13. #include <libraries/gadtools.h>
  14. #include <utility/tagitem.h>
  15.  
  16. #include "IFFConverter.h"
  17.  
  18.  
  19. // Defining protos
  20. ULONG ConvertDecimal(STRPTR);
  21. void  MakeDecimal(LONG, char *, UWORD);
  22. void  UpdateDimensions(ULONG, ...);
  23. BOOL  StringCompare(STRPTR, STRPTR);
  24. UWORD StringLength(char *);
  25.  
  26. static UBYTE MakeUpper(UBYTE);
  27.  
  28.  
  29. /*
  30. **  MakeDecimal(NumberToConvert, ASCII_String, MaxChars)
  31. **
  32. **     Makes from an integer value the same ASCII number.
  33. **
  34. **  pre:  NumberToConvert -> Integer to convert.
  35. **        ASCII_String -> Where to store the ASCII representation.
  36. **        MaxChars -> Maximum number of characters to use.
  37. **  post: None.
  38. **
  39. */
  40. void MakeDecimal(LONG NumberToConvert, char *ASCII_String, UWORD MaxChars)
  41. {
  42.    int i;
  43.    UBYTE Digit;
  44.    
  45.    for(i=MaxChars-1; i>=0; i--)
  46.    {
  47.       Digit = '0' + (NumberToConvert % 10);
  48.       NumberToConvert /= 10;
  49.       ASCII_String[i] = (char) Digit;
  50.    }
  51.    
  52.    i = 1;
  53.    while(*ASCII_String == '0' && i<MaxChars)
  54.    {
  55.       *ASCII_String++ = ' ';
  56.       i++;
  57.    }
  58. }
  59.  
  60.  
  61. /*
  62. **  Result = ConvertDecimal( String )
  63. **
  64. **     Converter an ASCII string to a decimal interger.
  65. **
  66. **  pre:  String - Pointer to an \n terminated ASCII string.
  67. **  post: Result - Function returns a decimal number.
  68. **
  69. */
  70. ULONG ConvertDecimal(STRPTR String)
  71. {
  72.    ULONG Result = 0;
  73.  
  74.    while(*String != '\0')
  75.       Result = Result * 10 + (UBYTE)(*String++ - '0');
  76.    
  77.    return( Result );
  78. }
  79.  
  80.  
  81. /*
  82. **  Result = ConvertHex( String )
  83. **
  84. **     Converter an ASCII string to a hexadecimal interger.
  85. **
  86. **  pre:  String - Pointer to an \n terminated ASCII string.
  87. **  post: Result - Function returns a hexadecimal number.
  88. **
  89. */
  90. /*
  91. ULONG ConvertHex(STRPTR String)
  92. {
  93.    ULONG Result = 0;
  94.  
  95.    while(*String != '\0')
  96.    {
  97.       Hex = MakeUpper( (UBYTE)(*String++ - '0') );
  98.       if( Hex = (UBYTE)(*String++ - '0' < ('A' - '9') )
  99.          Hex -= ('A' - '9');
  100.       Result = Result * 16 + Hex;
  101.    }
  102.    return( Result );
  103. }
  104. */
  105.  
  106. /*
  107. **  UpdateDimensions()
  108. **
  109. **     Updates the picture and clip dimention gadgets.
  110. **
  111. **  pre:  None.
  112. **  post: None.
  113. **
  114. */
  115. void UpdateDimensions(ULONG Gadgets_To_Update, ...)
  116. {
  117.    STRPTR Gad_UpdateText = "1234567";
  118.    ULONG *Gad_To_Update = &Gadgets_To_Update;
  119.    
  120.    register ULONG Gad_enumID;
  121.    register ULONG Gad_Value;
  122.    
  123.    SetTextGadget[1] = (ULONG)Gad_UpdateText;
  124.  
  125.    while( (Gad_enumID = *Gad_To_Update) != GD_Sentinal )      
  126.    {
  127.       *Gad_To_Update++;
  128.       Gad_Value = *Gad_To_Update++;
  129.       
  130.       if(PanelGadgets[Gad_enumID].MyGadgetType == TEXT_KIND)
  131.       {
  132.          MakeDecimal(Gad_Value, (char *)SetTextGadget[1], 7);
  133.       
  134.          UpdateGadgets(Gad_enumID, &SetTextGadget,
  135.                        TAG_DONE);
  136.       }
  137.       else
  138.       {
  139.          SetIntegerGadget[1] = Gad_Value;
  140.          
  141.          UpdateGadgets(Gad_enumID, &SetIntegerGadget,
  142.                        TAG_DONE);
  143.       }
  144.    }
  145. }
  146.  
  147.  
  148. /*
  149. **  Length = StringLength( String )
  150. **
  151. **     Calculate the length of a given string. String should be \0 terminated.
  152. **
  153. **  pre:  String - Pointer to a \0 terminated string.
  154. **  post: Length - Length of \0 terminated string.
  155. **
  156. */
  157. UWORD StringLength(char *String)
  158. {
  159.    register UWORD i = 0;
  160.    
  161.    while( *String++ != 0 )
  162.       i++;
  163.       
  164.    return(i);
  165. }
  166.  
  167.  
  168. /*
  169. **  result = StringCompare( String1, String2 )
  170. **
  171. **     Compare two strings and return TRUE if they match.
  172. **     'StringCompare' is case insencitive.
  173. **
  174. **  pre:  String1, String2 - Pointer to a string to be compaired.
  175. **                           Strings should be \0 terminated.
  176. **  post: result - Function returns TRUE if strings are equal,
  177. **                 FALSE otherwise.
  178. **
  179. */
  180. BOOL StringCompare(STRPTR String1, STRPTR String2)
  181. {
  182.    while( (*String1 != 0) && (MakeUpper(*String1++) == MakeUpper(*String2++)) );
  183.  
  184.    if( *String2 == '\0' )
  185.       return(TRUE);
  186.    else
  187.       return(FALSE);
  188. }
  189.  
  190.  
  191. /*
  192. **  Upper = MakeUpper( Character )
  193. **
  194. **     Converters a character to upper case.
  195. **
  196. **  pre:  Character - character to be converter to upper case.
  197. **  post: Upper - Chatacter in upper case
  198. **
  199. */
  200. static UBYTE MakeUpper(UBYTE Character)
  201. {
  202.    if( Character < 'a' || Character > 'z' )
  203.       return(Character);
  204.    return( (UBYTE)(Character - (UBYTE)32) );
  205.  
  206. }
  207.