home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Xconq 7.1.0 / src / xconq-7.1.0 / kernel / mac.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-07  |  7.8 KB  |  385 lines  |  [TEXT/R*ch]

  1. /* Mac-specific code for Xconq.
  2.    Copyright (C) 1992, 1993, 1994, 1995, 1996 Stanley T. Shebs.
  3.  
  4. Xconq is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.  See the file COPYING.  */
  8.  
  9. #include "config.h"
  10. #include "misc.h"
  11. #include "dir.h"
  12. #include "lisp.h"
  13. #include "module.h"
  14. #include "system.h"
  15. void mac_abort PARAMS ((void));
  16. extern void add_library_path PARAMS ((char *path));
  17.  
  18. #ifdef THINK_C
  19. #include <MacHeaders>
  20. #endif /* THINK_C */
  21.  
  22. #ifdef MPW
  23. #include <Types.h>
  24. #include <Resources.h>
  25. #include <Events.h>  /* for TickCount */
  26. #include <TextUtils.h>  /* for GetIndString */
  27. #endif /* MPW */
  28.  
  29. #include <signal.h>
  30.  
  31. /* We need this in order to find string resources that have filenames
  32.    in them. */
  33. #include "macdefs.h"
  34.  
  35. /* #include <sys/time.h> */
  36.  
  37. #ifndef c2p
  38. #define c2p(STR,PBUF) \
  39.   strcpy(((char *) PBUF) + 1, STR);  \
  40.   PBUF[0] = strlen(STR);
  41. #endif
  42.  
  43. #ifndef p2c
  44. #define p2c(PSTR,BUF)  \
  45.   strncpy(BUF, ((char *) (PSTR) + 1), PSTR[0]);  \
  46.   BUF[PSTR[0]] = '\0';
  47. #endif
  48.  
  49. /* The HFS volume that the program started with. */
  50.  
  51. short initialvrefnum;
  52.  
  53. static char *news_fname;
  54. static char *save_fname;
  55. static char *checkpoint_fname;
  56. static char *error_save_fname;
  57. static char *statistics_fname;
  58.  
  59. #ifndef XCONQLIB
  60. #define XCONQLIB ":lib"
  61. #endif
  62.  
  63. char *
  64. default_library_filename()
  65. {
  66.     return XCONQLIB;
  67. }
  68.  
  69. char *
  70. news_filename()
  71. {
  72.     Str255 tmpstr;
  73.     char tmpbuf[255];
  74.     
  75.     if (news_fname == NULL) {
  76.     GetIndString(tmpstr, sFilenames, siNews);
  77.     p2c(tmpstr, tmpbuf);
  78.     if (!empty_string(tmpbuf))
  79.       news_fname = copy_string(tmpbuf);
  80.     else
  81.       news_fname = "news.txt";
  82.     }
  83.     return news_fname;
  84. }
  85.  
  86. char *
  87. saved_game_filename()
  88. {
  89.     Str255 tmpstr;
  90.     char tmpbuf[255];
  91.     
  92.     if (save_fname == NULL) {
  93.     GetIndString(tmpstr, sFilenames, siSavedGame);
  94.     p2c(tmpstr, tmpbuf);
  95.     if (!empty_string(tmpbuf))
  96.       save_fname = copy_string(tmpbuf);
  97.     else
  98.       save_fname = "Saved Game";
  99.     }
  100.     return save_fname;
  101. }
  102.  
  103. char *
  104. checkpoint_filename()
  105. {
  106.     Str255 tmpstr;
  107.     char tmpbuf[255];
  108.  
  109.     if (checkpoint_fname == NULL) {
  110.     GetIndString(tmpstr, sFilenames, siCheckpoint);
  111.     p2c(tmpstr, tmpbuf);
  112.     if (!empty_string(tmpbuf))
  113.       checkpoint_fname = copy_string(tmpbuf);
  114.     else
  115.       checkpoint_fname = "Checkpoint";
  116.     }
  117.     return checkpoint_fname;
  118. }
  119.  
  120. char *
  121. error_save_filename()
  122. {
  123.     Str255 tmpstr;
  124.     char tmpbuf[255];
  125.  
  126.     if (error_save_fname == NULL) {
  127.     GetIndString(tmpstr, sFilenames, siErrorSave);
  128.     p2c(tmpstr, tmpbuf);
  129.     if (!empty_string(tmpbuf))
  130.       error_save_fname = copy_string(tmpbuf);
  131.     else
  132.       error_save_fname = "Error Save";
  133.     }
  134.     return error_save_fname;
  135. }
  136.  
  137. char *
  138. statistics_filename()
  139. {
  140.     Str255 tmpstr;
  141.     char tmpbuf[255];
  142.  
  143.     if (statistics_fname == NULL) {
  144.     GetIndString(tmpstr, sFilenames, siStatistics);
  145.     p2c(tmpstr, tmpbuf);
  146.     if (!empty_string(tmpbuf))
  147.       statistics_fname = copy_string(tmpbuf);
  148.     else
  149.       statistics_fname = "Statistics";
  150.     }
  151.     return statistics_fname;
  152. }
  153.  
  154. /* Attempt to open a library file. */
  155.  
  156. FILE *
  157. open_module_library_file(Module *module)
  158. {
  159.     short curvrefnum;
  160.     char fullnamebuf[255];
  161.     LibraryPath *p;
  162.     FILE *fp;
  163.     
  164.     /* Can't open anonymous library modules. */
  165.     if (module->name == NULL)
  166.       return NULL;
  167.     /* Generate library pathname. */
  168.     for_all_library_paths(p) {
  169.     make_pathname(p->path, module->name, "g", fullnamebuf);
  170.     /* Now try to open the file. */
  171.     fp = fopen(fullnamebuf, "r");
  172.     if (fp != NULL) {
  173.         /* Remember the filename where we found it. */
  174.         module->filename = copy_string(fullnamebuf);
  175.         return fp;
  176.     }
  177.     /* Try the same name in a different directory. */
  178.     GetVol(NULL, &curvrefnum);
  179.     SetVol(NULL, initialvrefnum);
  180.     fp = fopen(fullnamebuf, "r");
  181.     SetVol(NULL, curvrefnum);
  182.     if (fp != NULL) {
  183.         /* Remember the filename (what about volume?) where we found it. */
  184.         module->filename = copy_string(fullnamebuf);
  185.         return fp;
  186.     }
  187.     }
  188.     return NULL;
  189. }
  190.  
  191. FILE *
  192. open_module_explicit_file(Module *module)
  193. {
  194.     short curvrefnum;
  195.     char fullnamebuf[255];
  196.     LibraryPath *p;
  197.     FILE *fp = NULL;
  198.     
  199.     if (module->filename == NULL) {
  200.     /* Try guessing a filename, since none supplied. */
  201.     if (module->name != NULL) {
  202.         for_all_library_paths(p) {
  203.         make_pathname(p->path, module->name, "g", fullnamebuf);
  204.         /* Now try to open the file. */
  205.         fp = fopen(fullnamebuf, "r");
  206.         if (fp != NULL)
  207.           return fp;
  208.         GetVol(NULL, &curvrefnum);
  209.         SetVol(NULL, initialvrefnum);
  210.         fp = fopen(fullnamebuf, "r");
  211.         SetVol(NULL, curvrefnum);
  212.         if (fp != NULL)
  213.           return fp;
  214.         }
  215.     }
  216.     } else {
  217.     /* Try some other random ideas. */
  218.     sprintf(fullnamebuf, "%s", module->filename);
  219.     fp = fopen(module->filename, "r");
  220.     if (fp != NULL) {
  221.         add_library_path("");
  222.         return fp;
  223.     }
  224.     sprintf(fullnamebuf, ":%s", module->filename);
  225.     fp = fopen(fullnamebuf, "r");
  226.     if (fp != NULL)
  227.       return fp;
  228.     sprintf(fullnamebuf, "%s%s", ":lib:", module->filename);
  229.     fp = fopen(fullnamebuf, "r");
  230.     if (fp != NULL)
  231.       return fp;
  232.     /* Try opening a library module under where the program started. */
  233.     GetVol(NULL, &curvrefnum);
  234.     SetVol(NULL, initialvrefnum);
  235.     fp = fopen(fullnamebuf, "r");
  236.     SetVol(NULL, curvrefnum);
  237.     }
  238.     return fp;
  239. }
  240.  
  241. FILE *
  242. open_library_file(char *filename)
  243. {
  244.     short curvrefnum;
  245.     char fullnamebuf[255];
  246.     LibraryPath *p;
  247.     FILE *fp;
  248.     
  249.     /* Now try to open the file. */
  250.     fp = fopen(filename, "r");
  251.     if (fp != NULL)
  252.       return fp;
  253.     /* Generate library pathname. */
  254.     for_all_library_paths(p) {
  255.     make_pathname(p->path, filename, NULL, fullnamebuf);
  256.     /* Now try to open the file. */
  257.     fp = fopen(fullnamebuf, "r");
  258.     if (fp != NULL)
  259.       return fp;
  260.     /* Change to volume where the program started. */
  261.     GetVol(NULL, &curvrefnum);
  262.     SetVol(NULL, initialvrefnum);
  263.     fp = fopen(fullnamebuf, "r");
  264.     SetVol(NULL, curvrefnum);
  265.     if (fp != NULL)
  266.       return fp;
  267.     }
  268.     return NULL;
  269. }
  270.  
  271. FILE *
  272. open_scorefile_for_reading(char *name)
  273. {
  274.     short curvrefnum;
  275.     FILE *fp;
  276.     
  277.     GetVol(NULL, &curvrefnum);
  278.     SetVol(NULL, initialvrefnum);
  279.     /* Now try to open the file. */
  280.     fp = fopen(name, "r");
  281.     SetVol(NULL, curvrefnum);
  282.     return fp;
  283. }
  284.  
  285. FILE *
  286. open_scorefile_for_writing(char *name)
  287. {
  288.     short curvrefnum;
  289.     FILE *fp;
  290.     
  291.     GetVol(NULL, &curvrefnum);
  292.     SetVol(NULL, initialvrefnum);
  293.     /* Now try to open the file. */
  294.     fp = fopen(name, "a");
  295.     SetVol(NULL, curvrefnum);
  296.     return fp;
  297. }
  298.  
  299. void
  300. make_pathname(char *path, char *name, char *extn, char *pathbuf)
  301. {
  302.     strcpy(pathbuf, "");
  303.     if (!empty_string(path)) {
  304.     strcat(pathbuf, path);
  305.     strcat(pathbuf, ":");
  306.     }
  307.     strcat(pathbuf, name);
  308.     /* Don't add a second identical extension, but do add if extension
  309.        is different (in case we want "foo.12" -> "foo.12.g" for instance) */
  310.     if (strrchr(name, '.')
  311.     && extn
  312.     && strcmp(strrchr(name, '.') + 1, extn) == 0)
  313.       return;
  314.     if (!empty_string(extn)) {
  315.     strcat(pathbuf, ".");
  316.     strcat(pathbuf, extn);
  317.     }
  318. }
  319.  
  320. /* Remove a saved game from the system. */
  321.  
  322. void
  323. remove_saved_game()
  324. {
  325. }
  326.  
  327. void
  328. init_signal_handlers()
  329. {
  330. }
  331.  
  332. int last_ticks = 0;
  333.  
  334. int
  335. n_seconds_elapsed(n)
  336. int n;
  337. {
  338.     int ticks = TickCount();
  339.  
  340.     if (((ticks - last_ticks) / 60) > n) {
  341.     last_ticks = ticks;
  342.         return TRUE;
  343.     } else {
  344.     return FALSE;
  345.     }
  346. }
  347.  
  348. int last_ticks_for_ms = 0;
  349.  
  350. int
  351. n_ms_elapsed(n)
  352. int n;
  353. {
  354.     return (((TickCount() - last_ticks_for_ms) * 16) > n);
  355. }
  356.  
  357. void
  358. record_ms()
  359. {
  360.     last_ticks_for_ms = TickCount();
  361. }
  362.  
  363. /* Instead of coredumping, which is not a normal Mac facility, we
  364.    drop into Macsbug.  If we then "g" from Macsbug, the program will
  365.    exit cleanly. */
  366.  
  367. void
  368. mac_abort ()
  369. {
  370.     /* Make sure no output still buffered up, then zap into MacsBug. */
  371. #if 0 /* how to know if stdio in use? */
  372.     fflush(stdout);
  373.     fflush(stderr);
  374.     printf("## Abort! ##\n");
  375. #endif
  376. #ifdef MPW_SADE
  377.     SysError(8005);
  378. #else 
  379.     Debugger();
  380. #endif
  381.     /* "g" in MacsBug will then cause a regular error exit. */
  382.     exit(1);
  383. }
  384.  
  385.