home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / YADME10.LHA / YADME10 / src / subs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-19  |  7.3 KB  |  368 lines

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