home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 389.lha / dme_v1.40 / src / subs.c < prev    next >
C/C++ Source or Header  |  1990-07-03  |  7KB  |  404 lines

  1.  
  2. /*
  3.  *  SUBS.C
  4.  *
  5.  *    (C)Copyright 1987 by Matthew Dillon, All Rights Reserved
  6.  *
  7.  *  Subroutines.
  8.  */
  9.  
  10. #include "defs.h"
  11. #include <devices/inputevent.h>
  12. #include <libraries/dos.h>
  13. #include <libraries/dosextens.h>
  14.  
  15. typedef struct FileInfoBlock FIB;
  16. typedef struct Process         PROC;
  17.  
  18. extern void *OpenFont();
  19. extern void *OpenDiskFont();
  20. extern void *OpenLibrary();
  21.  
  22. /*
  23.  *  Create DME's text icon.
  24.  */
  25.  
  26. void
  27. makemygadget(gad)
  28. struct Gadget *gad;
  29. {
  30.     static unsigned long ga[] = {
  31.     0xFFFFFFFF,    /* 32 pixels across */
  32.     0x80FDCBFD,
  33.     0xFFFDDFFD,
  34.     0x80000001,
  35.     0x80DFDDDF,
  36.     0x80000001,
  37.     0xBC0EF00B,
  38.     0x80000001,
  39.     0xBFC00CDD,
  40.     0x80000001,
  41.     0xA00DF00F,
  42.     0x80000001,
  43.     0x80000001,
  44.  
  45.     0x80000001,
  46.     0x80FDCBFD,
  47.     0xFFFDDFFD,
  48.     0x80000001,
  49.     0x80DFDDDF,
  50.     0x80000001,
  51.     0xBC0EF00B,
  52.     0x80000001,
  53.     0xBFC00CDD,
  54.     0x80000001,
  55.     0xA00DF00F,
  56.     0x80000001,
  57.     0xFFFFFFFF
  58.     };
  59.     static struct Image image = {
  60.     0, 0, 20, 16, 2, (unsigned short *)ga, 3, 0, NULL
  61.     };
  62.     clrmem(gad, sizeof(struct Gadget));
  63.     gad->Width = 20;
  64.     gad->Height = 17;
  65.     gad->Flags    = GADGIMAGE|GADGHCOMP;
  66.     gad->GadgetType   = BOOLGADGET;
  67.     gad->Activation = RELVERIFY|GADGIMMEDIATE;
  68.     gad->GadgetRender = (APTR)ℑ
  69. }
  70.  
  71. /*
  72.  * return index of first non space.  Returns 0 if no spaces found.
  73.  */
  74.  
  75. firstns(str)
  76. char *str;
  77. {
  78.     short i;
  79.  
  80.     for (i = 0; str[i] && str[i] == ' '; ++i);
  81.     if (str[i] == 0)
  82.     i = 0;
  83.     return((int)i);
  84. }
  85.  
  86. /*
  87.  *  Return index of last non-space, 0 if no spaces.
  88.  */
  89.  
  90. lastns(str)
  91. char *str;
  92. {
  93.     short i;
  94.  
  95.     for (i = strlen(str) - 1; i > 0 && str[i] == ' '; --i);
  96.     if (i < 0)
  97.     i = 0;
  98.     return((int)i);
  99. }
  100.  
  101. /*
  102.  *  Return length of word under cursor
  103.  */
  104.  
  105. wordlen(str)
  106. char *str;
  107. {
  108.     short i;
  109.  
  110.     for (i = 0; *str && *str != ' '; ++i, ++str);
  111.     return((int)i);
  112. }
  113.  
  114. /*
  115.  *  Find the path from some root device to a specific filename (src), and
  116.  *  stick the result in (dest).  If unable to backtrace along directories,
  117.  *  simply copy (src) into (dest)
  118.  *
  119.  *  Returns (1) if able to backtrace, (0) if not.
  120.  */
  121.  
  122. getpathto(src, dest)
  123. char *src, *dest;
  124. {
  125.     BPTR flock, pflock;
  126.     short len, total;
  127.     FIB *fib = (FIB *)malloc(sizeof(FIB));
  128.     char c;
  129.  
  130.     dest[0] = 0;
  131.     total = 1;
  132.     flock = Lock(src, ACCESS_READ);
  133.     if (flock == NULL) {                           /* missing file?    */
  134.     for (len = strlen(src); len >= 0; --len) {
  135.         if (src[len] == '/') {
  136.         --len;
  137.         break;
  138.         }
  139.         if (src[len] == ':')
  140.         break;
  141.     }
  142.     if (c = src[len + 1]) {
  143.         strcpy(dest, src+len+2);
  144.         total = strlen(dest)+1;
  145.     }
  146.     src[len + 1] = 0;
  147.     flock = Lock(src, ACCESS_READ);
  148.     src[len + 1] = c;
  149.     }
  150.     if (flock) {
  151.     do {
  152.         pflock = ParentDir(flock);
  153.         if (Examine(flock, fib) == 0)
  154.         fib->fib_FileName[0] = 0;
  155.         if (fib->fib_FileName[0] == 0)
  156.         strcpy(fib->fib_FileName, "ram");
  157.         len = strlen(fib->fib_FileName);
  158.         movmem(dest, dest + len + 1, total);
  159.         dest[len] = (pflock) ? '/' : ':';
  160.         movmem(fib->fib_FileName, dest, len);
  161.         total += len + 1;
  162.         UnLock(flock);
  163.         flock = pflock;
  164.     } while(pflock);
  165.     len = strlen(dest) - 1;
  166.     if (dest[len] == '/')
  167.         dest[len] = 0;
  168.     return(1);
  169.     }
  170.     strcpy(dest, src);
  171.     return(0);
  172. }
  173.  
  174. /*
  175.  *  Allocation routines and other shortcuts
  176.  */
  177.  
  178. void *
  179. allocb(bytes)
  180. {
  181.     return(AllocMem(bytes, MEMF_CLEAR|MEMF_PUBLIC));
  182. }
  183.  
  184. void *
  185. allocl(lwords)
  186. {
  187.     return(AllocMem(lwords<<2, MEMF_CLEAR|MEMF_PUBLIC));
  188. }
  189.  
  190. void
  191. bmovl(s,d,n)
  192. void *s, *d;
  193. long n;
  194. {
  195.     movmem(s, d, n << 2);
  196. }
  197.  
  198. /*
  199.  *  Remove tabs in a buffer
  200.  */
  201.  
  202. detab(ibuf, obuf, maxlen)
  203. char *ibuf, *obuf;
  204. {
  205.     short i, j;
  206.  
  207.     maxlen -= 2;
  208.     for (i = j = 0; ibuf[i] && j < maxlen; ++i) {
  209.     if (ibuf[i] == 9) {
  210.         do {
  211.         obuf[j++] = ' ';
  212.         } while ((j & 7) && j < maxlen);
  213.     } else {
  214.         obuf[j++] = ibuf[i];
  215.     }
  216.     }
  217.     if (j && obuf[j-1] == '\n')
  218.     --j;
  219.     while (j && obuf[j-1] == ' ')
  220.     --j;
  221.     obuf[j] = 0;
  222.     return((int)j);
  223. }
  224.  
  225. xefgets(fi, buf, max)
  226. FILE *fi;
  227. char *buf;
  228. int max;
  229. {
  230.     char ebuf[256];
  231.  
  232.     if (fgets(ebuf, max, fi))
  233.     return(detab(ebuf, buf, max));
  234.     return(-1);
  235. }
  236.  
  237. ncstrcmp(s1, s2)
  238. ubyte *s1, *s2;
  239. {
  240.     ubyte c1, c2;
  241.  
  242.     for (;;) {
  243.     c1 = *s1;
  244.     c2 = *s2;
  245.     if (c1 >= 'A' && c1 <= 'Z') c1 |= 0x20;
  246.     if (c2 >= 'A' && c2 <= 'Z') c2 |= 0x20;
  247.     if (c1 != c2)
  248.         break;
  249.     if ((c1|c2) == 0)
  250.         return(0);
  251.     ++s1;
  252.     ++s2;
  253.     }
  254.     if (c1 < c2)
  255.     return(-1);
  256.     if (c1 > c2)
  257.     return(1);
  258. }
  259.  
  260. ED *
  261. finded(str, doff)
  262. char *str;
  263. {
  264.     ED *ed;
  265.  
  266.     for (ed = (ED *)DBase.mlh_Head; ed->Node.mln_Succ; ed = (ED *)ed->Node.mln_Succ) {
  267.     if (strlen(ed->Name) >= doff && ncstrcmp(str, ed->Name+doff) == 0)
  268.         return(ed);
  269.     }
  270.     return(NULL);
  271. }
  272.  
  273. void
  274. mountrequest(bool)
  275. int bool;
  276. {
  277.     static APTR original_pr_WindowPtr = NULL;
  278.     register PROC *proc;
  279.  
  280.     proc = (PROC *)FindTask(0);
  281.     if (!bool && proc->pr_WindowPtr != (APTR)-1) {
  282.     original_pr_WindowPtr = proc->pr_WindowPtr;
  283.     proc->pr_WindowPtr = (APTR)-1;
  284.     }
  285.     if (bool && proc->pr_WindowPtr == (APTR)-1)
  286.     proc->pr_WindowPtr = original_pr_WindowPtr;
  287. }
  288.  
  289. char *
  290. GetDEnv(ename)
  291. char *ename;
  292. {
  293.     long envLock = Lock("env:", SHARED_LOCK);
  294.     char *str = NULL;
  295.  
  296.     if (envLock) {
  297.     long oldLock = CurrentDir(envLock);
  298.     FILE *fi = fopen(ename, "r");
  299.     long siz;
  300.     if (fi) {
  301.         fseek(fi, 0L, 2);
  302.         siz = ftell(fi);
  303.         fseek(fi, 0L, 0);
  304.         if (siz > 0 && (str = malloc(siz + 1))) {
  305.         fread(str, siz, 1, fi);
  306.         str[siz] = 0;
  307.         }
  308.         fclose(fi);
  309.     }
  310.     UnLock(CurrentDir(oldLock));
  311.     }
  312.     return(str);
  313. }
  314.  
  315. void
  316. SetDEnv(ename, econt)
  317. char *ename;
  318. char *econt;
  319. {
  320.     long envLock = Lock("env:", SHARED_LOCK);
  321.  
  322.     if (envLock) {
  323.     long oldLock = CurrentDir(envLock);
  324.     FILE *fi = fopen(ename, "w");
  325.  
  326.     if (fi) {
  327.         fwrite(econt, strlen(econt), 1, fi);
  328.         fclose(fi);
  329.     }
  330.     UnLock(CurrentDir(oldLock));
  331.     }
  332. }
  333.  
  334.  
  335. /*
  336.  *  GETFONT()
  337.  *
  338.  *  This function properly searches resident and disk fonts for the
  339.  *  font.
  340.  */
  341.  
  342. struct Library *DiskfontBase;
  343.  
  344. FONT *
  345. GetFont(name, size)
  346. char *name;
  347. short size;
  348. {
  349.     FONT *font1;
  350.     TA Ta;
  351.     short libwasopen = (DiskfontBase != (void *)NULL);
  352.  
  353.     Ta.ta_Name    = (UBYTE *)name;
  354.     Ta.ta_YSize = size;
  355.     Ta.ta_Style = 0;
  356.     Ta.ta_Flags = 0;
  357.  
  358.     font1 = OpenFont(&Ta);
  359.     if (font1 == NULL || font1->tf_YSize != Ta.ta_YSize) {
  360.     FONT *font2;
  361.  
  362.     if (libwasopen || (DiskfontBase = OpenLibrary("diskfont.library", 0))) {
  363.         if (font2 = OpenDiskFont(&Ta)) {
  364.         if (font1)
  365.             CloseFont(font1);
  366.         font1 = font2;
  367.         }
  368.         if (libwasopen == 0)
  369.         CloseLibrary(DiskfontBase);
  370.     }
  371.     }
  372.     return(font1);
  373. }
  374.  
  375. /*
  376.  *  DEAD.C
  377.  */
  378.  
  379. int
  380. DeadKeyConvert(msg,buf,bufsize,keymap)
  381. struct IntuiMessage *msg;
  382. UBYTE *buf;
  383. int bufsize;
  384. struct KeyMap *keymap;
  385. {
  386.     static struct InputEvent ievent = { NULL, IECLASS_RAWKEY };
  387.     if (msg->Class != RAWKEY)
  388.     return(-2);
  389.     ievent.ie_Code = msg->Code;
  390.     ievent.ie_Qualifier = msg->Qualifier;
  391.     ievent.ie_position.ie_addr = *((APTR *)msg->IAddress);
  392.     return(RawKeyConvert(&ievent,(char *)buf,bufsize,keymap));
  393. }
  394.  
  395. void *
  396. clrmem(ptr, bytes)
  397. void *ptr;
  398. long bytes;
  399. {
  400.     setmem(ptr, bytes, 0);
  401.     return(ptr);
  402. }
  403.  
  404.