home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Graphics / ToyViewer-2.6a / src / Exttab.m < prev    next >
Encoding:
Text File  |  1996-03-20  |  3.3 KB  |  187 lines

  1. #import  "Exttab.h"
  2. #import  <stdio.h>
  3. #import  <string.h>
  4. #import  "strfunc.h"
  5.  
  6. /*
  7. Format:
  8.  
  9. <extension> <path_of_command> <args> ...
  10.  
  11. One of args must be '$', which is replaced by the image file.
  12. If the first character of path is '~', it is extended to the home
  13. directory. If '@', it is extended to the application directory.
  14.  
  15. Example...
  16. jpg    /usr/local/bin/djpeg $
  17. g3    /usr/local/pbmplus/g3topbm -reversebits $
  18. qwe    ~/bin/qwe2ppm $ -
  19. */
  20.  
  21. #define  MAXCOMM    512
  22.  
  23. static char *homeDir, *appDir;
  24.  
  25. static char *get_comm(const char *buf, int *a)
  26. {
  27.     int i, j, n, len;
  28.     char tmp[MAXCOMM + 128], *p;
  29.  
  30.     if ((len = strlen(buf)) < 3)
  31.         return NULL;
  32.     for (i = j = n = 0; i < len && buf[i]; ) {
  33.         while (buf[i] && buf[i] <= ' ') i++;
  34.         if (buf[i] > ' ') {
  35.             if (++n == 2) { /* path */
  36.                 if (buf[i] == '~') p = homeDir;
  37.                 else if (buf[i] == '@') p = appDir;
  38.                 else p = NULL;
  39.                 if (p) {
  40.                     i++;
  41.                     while (*p)
  42.                         tmp[j++] = *p++;
  43.                 }
  44.             }
  45.             while (buf[i] > ' ')
  46.                 tmp[j++] = buf[i++];
  47.             tmp[j++] = 0;
  48.         }
  49.     }
  50.     *a = n;
  51.     tmp[j++] = 0;    /* double NULL */
  52.     p = (char *)malloc(j);
  53.     for (i = 0; i < j; i++)
  54.         p[i] = tmp[i];
  55.     return p;
  56. }
  57.  
  58.  
  59. @implementation Exttab
  60.  
  61. - init
  62. {
  63.     entry = 0;
  64.     table = NULL;
  65.     args = NULL;
  66.     homeDir = getenv("HOME");
  67.     appDir = str_dup(NXArgv[0]); /* alloc */
  68.     (void) dircopy(appDir, NXArgv[0], NO);
  69.     return self;
  70. }
  71.  
  72. - (int)readExtData: (const char *)filename
  73. {
  74.     FILE *fp;
  75.     char buf[MAXCOMM];
  76.     char **tab, *p;
  77.     int *a;
  78.     int count, newentry;
  79.  
  80.     if ((fp = fopen(filename, "r")) == NULL)
  81.         return 0;
  82.     for (count = 0; fgets(buf, MAXCOMM, fp); )
  83.         if (buf[0] >= ' ' && buf[0] != '#') count++;
  84.     if (count == 0) {
  85.         fclose(fp);
  86.         return 0;
  87.     }
  88.     rewind(fp);
  89.     newentry = entry + count + 1;
  90.     tab = (char **)malloc(sizeof(char *) * newentry);
  91.     a = (int *)malloc(sizeof(int) * newentry);
  92.     for (count = 0; fgets(buf, MAXCOMM, fp); ) {
  93.         if (! (buf[0] >= ' ' && buf[0] != '#'))
  94.             continue;
  95.         if ((p = get_comm(buf, &a[count])) != NULL)
  96.             tab[count++] = p;
  97.     }
  98.     if (count == 0) {
  99.         free((void *)tab);
  100.         free((void *)a);
  101.         fclose(fp);
  102.         return 0;
  103.     }
  104.     if (entry > 0) {
  105.         int x, k = count;
  106.         for (x = 0; table[x]; x++) {
  107.             tab[k] = table[x];
  108.             a[k] = args[x];
  109.             k++;
  110.         }
  111.         free((void *)table);
  112.         free((void *)args);
  113.         entry = k;
  114.     }else
  115.         entry = count;
  116.     tab[entry] = NULL;
  117.     table = tab;
  118.     args = a;
  119.     return count;
  120. }
  121.  
  122. - (char **)table
  123. {
  124.     return table;
  125. }
  126.  
  127. - (int)entry
  128. {
  129.     return entry;
  130. }
  131.  
  132. - (char **)execListAlloc: (const char *)type with: (const char *)filename
  133. {
  134.     int i, n;
  135.     char **list, *p;
  136.  
  137.     if (table == NULL)
  138.         return NULL;
  139.     for (n = 0; ; n++) {
  140.         if (table[n] == NULL) return NULL;
  141.         if (strcmp(table[n], type) == 0)
  142.             break;
  143.     }
  144.     list = (char **)malloc(sizeof(char *) * (args[n] + 1));
  145.     for (i = 1, p = table[n]; ; i++) {
  146.         while (*p) p++;
  147.         if (*++p == 0) { /* double NULL */
  148.             list[i] = NULL;
  149.             break;
  150.         }
  151.         list[i] = (*p == '$') ? (char *)filename : p;
  152.     }
  153.     p = list[0] = list[1];
  154.     while (*p) {
  155.         if (*p++ == '/')
  156.             list[1] = p;
  157.     }
  158.     return list;
  159. }
  160.  
  161. @end
  162.  
  163. #ifdef TEST_ALONE
  164. main()
  165. {
  166.     id tab;
  167.     int i, n;
  168.     char **ex;
  169.     static char *sample[] = { "jpg", "g3", "none", NULL };
  170.  
  171.     tab = [[Exttab alloc] init];
  172.     [tab readExtData: "./test1"];
  173.     [tab readExtData: "./test2"];
  174.     for (n = 0; sample[n]; n++) {
  175.         ex = [tab execListAlloc: sample[n] with: "ImageFile"];
  176.         if (ex == NULL) {
  177.             printf("Error\n");
  178.             continue;
  179.         }
  180.         for (i = 0; ex[i]; i++)
  181.             printf("(%s) ", ex[i]);
  182.         putchar('\n');
  183.         free((void *)ex);
  184.     }
  185. }
  186. #endif
  187.