home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1995 August / NEBULA.mdf / Apps / DevTools / Compiler2.0 / Source / Defaults.m < prev    next >
Encoding:
Text File  |  1993-01-25  |  4.5 KB  |  177 lines

  1. //
  2. // This is the Compiler's Companion a program to integrate make's and Edit
  3. // more cleanly. Version 2.0
  4. //
  5. // Copyright(C) 1992 Daryll Strauss
  6. //
  7. //    This program is shareware; you can redistribute it and/or modify
  8. //    it under the terms of the GNU General Public License as published by
  9. //    the Free Software Foundation; either version 1, or (at your option)
  10. //    any later version as long as you leave all the references to the fact
  11. //    that it is shareware and covered by the GNU General Public License in
  12. //    tact.
  13. //
  14. //    This program is distributed in the hope that it will be useful,
  15. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. //    GNU General Public License for more details.
  18. //
  19. //    You should have received a copy of the GNU General Public License
  20. //    along with this program; if not, write to the Free Software
  21. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. //
  23. //    Please see the README.rtf file for more details on the shareware
  24. //    distribution and the GNU General Public License.
  25. //
  26.  
  27. #import "Defaults.h"
  28. #import <objc/objc.h>
  29. #import <appkit/nextstd.h>
  30. #import <defaults/defaults.h>
  31.  
  32. //
  33. // Basic support functions
  34. //
  35.  
  36. static BOOL parse_bool(char *bool_str)
  37. {
  38.     if (!strcasecmp(bool_str, "yes")) return TRUE;
  39.     if (!strcasecmp(bool_str, "no")) return FALSE;
  40.     if (!strcasecmp(bool_str, "true")) return TRUE;
  41.     if (!strcasecmp(bool_str, "false")) return FALSE;
  42.     return(FALSE);
  43. }
  44.  
  45. static void Read_Default_Value(Full_Defaults_Vector def, char *value)
  46. {
  47.     switch (def.type) {
  48.     case default_string:
  49.         if (*(char**)def.current_value) free(*(char**)def.current_value);
  50.         *(char**)def.current_value=malloc(strlen(value)+1);
  51.         strcpy(*(char**)def.current_value, value);
  52.         break;
  53.     case default_const_string:
  54.         *(char**)def.current_value=value;
  55.         break;
  56.     case default_int:
  57.         *(int*)def.current_value=atoi(value);
  58.         break;
  59.     case default_float:
  60.         *(float*)def.current_value=atof(value);
  61.         break;
  62.     case default_bool:
  63.         *(BOOL*)def.current_value=parse_bool(value);
  64.         break;
  65.     default:
  66.         fprintf(stderr, "Unknown type of default\n");
  67.     }
  68. }
  69.  
  70. static void Write_Default_Value(char *app_name, Full_Defaults_Vector def)
  71. {
  72.     char value[64];
  73.     
  74.     switch (def.type) {
  75.     case default_string:
  76.     case default_const_string:
  77.         NXWriteDefault(app_name, def.name, *(char**)def.current_value);
  78.         break;
  79.     case default_int:
  80.         sprintf(value, "%d", *(int*)def.current_value);
  81.         NXWriteDefault(app_name, def.name, value);
  82.         break;
  83.     case default_float:
  84.         sprintf(value, "%f", *(int*)def.current_value);
  85.         NXWriteDefault(app_name, def.name, value);
  86.         break;
  87.     case default_bool:
  88.         if (*(BOOL*)def.current_value)
  89.             NXWriteDefault(app_name, def.name, "TRUE");
  90.         else
  91.             NXWriteDefault(app_name, def.name, "FALSE");
  92.         break;
  93.     default:
  94.         fprintf(stderr, "Unknown type of default\n");
  95.     }
  96. }
  97.  
  98. void Register_Full_Defaults(char *app_name, Full_Defaults_Vector *defaults)
  99. {
  100.     struct _NXDefault *v;
  101.     int i, cnt=0;
  102.  
  103.     while (defaults[cnt].type!=default_end) cnt++;
  104.     cnt++;
  105.     v = malloc(cnt*sizeof(struct _NXDefault));
  106.     for (i=0; i<cnt; i++) {
  107.         v[i].name=defaults[i].name;
  108.         v[i].value=defaults[i].default_value;
  109.     }
  110.     NXRegisterDefaults(app_name, v);
  111.     free(v);
  112. }
  113.  
  114. void Get_Full_Defaults(char *app_name, Full_Defaults_Vector *defaults)
  115. {
  116.     int i=0;
  117.     char *value;
  118.     
  119.     while (defaults[i].type!=default_end) {
  120.         value=(char*)NXGetDefaultValue(app_name, defaults[i].name);
  121.         Read_Default_Value(defaults[i], value);
  122.         i++;
  123.     }
  124. }
  125.  
  126. void Reset_Defaults(Full_Defaults_Vector *defaults)
  127. {
  128.     int i=0;
  129.  
  130.     while (defaults[i].type!=default_end) {
  131.         Read_Default_Value(defaults[i], defaults[i].default_value);
  132.         i++;
  133.     }
  134. }
  135.  
  136. void Put_Full_Defaults(char *app_name, Full_Defaults_Vector *defaults)
  137. {
  138.     int i=0;
  139.     
  140.     while (defaults[i].type!=default_end) {
  141.         Write_Default_Value(app_name, defaults[i]);
  142.         i++;
  143.     }
  144. }
  145.  
  146. void Get_Default_By_Name(char *app_name, char *default_name, 
  147.     Full_Defaults_Vector *defaults)
  148. {
  149.     int i=0;
  150.     char *value;
  151.     
  152.     while (defaults[i].type!=default_end) {
  153.         if (!strcmp(defaults[i].name, default_name)) {
  154.             value=(char*)NXGetDefaultValue(app_name, default_name);
  155.             Read_Default_Value(defaults[i], value);
  156.             return;
  157.         }
  158.         i++;
  159.     }
  160.     fprintf(stderr, "Can't find the requested default\n");
  161. }
  162.  
  163. void Put_Default_By_Name(char *app_name, char *default_name,
  164.     Full_Defaults_Vector *defaults)
  165. {
  166.     int i=0;
  167.     
  168.     while (defaults[i].type!=default_end) {
  169.         if (!strcmp(defaults[i].name, default_name)) {
  170.             Write_Default_Value(app_name, defaults[i]);
  171.             return;
  172.         }
  173.         i++;
  174.     }
  175.     fprintf(stderr, "Can't find the requested default\n");
  176. }
  177.