home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / SourceCode / MiscKit1.2.6 / Source / MiscAppDefaults.m < prev    next >
Encoding:
Text File  |  1994-06-18  |  2.5 KB  |  81 lines

  1. //
  2. //    MiscAppDefaults.m -- easy access to preferences
  3. //        Written by Steve Hayman Copyright (c) 1994 by Steve Hayman.
  4. //                Version 1.0.  All rights reserved.
  5. //        This notice may not be removed from this source code.
  6. //
  7. //    This object is included in the MiscKit by permission from the author
  8. //    and its use is governed by the MiscKit license, found in the file
  9. //    "LICENSE.rtf" in the MiscKit distribution.  Please refer to that file
  10. //    for a list of all applicable permissions and restrictions.
  11. //    
  12.  
  13. #import <appkit/appkit.h>
  14. #import <misckit/MiscAppDefaults.h>
  15.  
  16. @implementation Application(MiscAppDefaults) 
  17.  
  18. // Three core methods first...
  19.  
  20. // Register a set of defaults under the application's name.
  21. - (int) registerDefaults:(const NXDefaultsVector) v
  22. {
  23.     return NXRegisterDefaults( [NXApp appName], v );
  24. }
  25.  
  26. // Return the requested default value as a string.
  27. - (const char *)defaultValue:(const char *)defName
  28. {
  29.     return NXGetDefaultValue( [NXApp appName], defName );
  30. }
  31.  
  32. // Set a default value.  This adds the value to the internal
  33. // defaults cache and also to the user's .NeXT/.NeXTdefaults database.
  34. // Returns what NXWriteDefault() returns (1 on success, 0 on failure)
  35. - (int) setDefault:(const char *)defName to:(const char *)defValue
  36. {
  37.     return NXWriteDefault( [NXApp appName], defName, defValue );
  38. }
  39.  
  40.  
  41. // -----------------------------------------------------------------
  42. // The rest of the methods are wrappers and format converters.
  43.  
  44. // Return a default value as an integer.
  45. // Note that if the default has never been set, or has been set to
  46. // something other than an integer, 0 will be returned.
  47. - (int) defaultIntValue:(const char *)defName
  48. {
  49.     const char *v = [self defaultValue:defName];
  50.     return( v ? atoi(v) : 0 );
  51. }
  52.  
  53. // Return a default value as a Boolean.
  54. // We return YES if the string value is "YES", NO if it's anything else
  55. // (including if it has never been set.)
  56. - (BOOL) defaultBoolValue:(const char *)defName
  57. {
  58.     const char *v = [self defaultValue:defName];
  59.     return ( v ? (strcmp( v, "YES") == 0) : NO ) ;
  60. }
  61.  
  62. // Set an integer default value.  Convert it to a string first.
  63. - (int)setIntDefault:(const char *)defName to:(int)defValue
  64. {
  65.     char intBuffer[80];
  66.     sprintf(intBuffer, "%i", defValue);
  67.     
  68.     return [self setDefault:defName to:intBuffer];
  69. }
  70.  
  71. // Set a boolean default.  We write the string YES if the boolean
  72. // is true, NO otherwise.
  73. - (int)setBoolDefault:(const char *)defName to:(BOOL)defValue
  74. {
  75.     return [self setDefault:defName to:(defValue ? "YES" : "NO")];
  76. }
  77.  
  78.  
  79.  
  80. @end
  81.