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

  1. #import <appkit/Application.h>
  2. #import <appkit/appkit.h>
  3. #import <sys/file.h>
  4. #import <stdio.h>
  5. #import <stdlib.h>
  6. #import <string.h>
  7. #import <sys/wait.h>
  8. #import "ToyWinPPM.h"
  9. #import "ppm.h"
  10. #import "strfunc.h"
  11.  
  12.  
  13. static FILE *openPipe(const char **list, int *err)
  14. {
  15.     int pfd[2];
  16.     int pid;
  17.  
  18.     /* if (*err != 0) then fork() didn't called successfully */
  19.     *err = 0;
  20.     if (list == NULL || access(list[0], X_OK) < 0) {
  21.         *err = Err_FLT_EXEC;
  22.         return NULL;    /* not executable */
  23.     }
  24.     (void)pipe(pfd);
  25.     if ((pid = fork()) == 0) { /* child process */
  26.         (void)close(1);
  27.         dup(pfd[1]);
  28.         (void)close(pfd[0]);
  29.         (void)close(pfd[1]);
  30.         execv(list[0], &list[1]);
  31.         exit(1);    /* Error */
  32.     }else if (pid < 0) {    /* ERROR */
  33.         *err = Err_FLT_EXEC;
  34.         (void)close(pfd[0]);
  35.         (void)close(pfd[1]);
  36.         return NULL;
  37.     }
  38.     (void)close(pfd[1]);
  39.     return fdopen(pfd[0], "r");
  40. }
  41.  
  42.  
  43. @implementation ToyWinPPM
  44.  
  45. - (commonInfo *)drawToyWin:(const char *)fileName type:(int)type
  46.     map:(unsigned char **)map err:(int *)err
  47. {
  48.     FILE *fp = NULL;
  49.     commonInfo *cinf = NULL;
  50.     BOOL    waitchild = NO;
  51.     const char *ext = NULL;
  52.     int alert;
  53.  
  54.     *err = 0;
  55.     if (type == Type_ppm) {
  56.         if ((fp = fopen(fileName, "r")) == NULL) {
  57.             *err = Err_OPEN;
  58.             return NULL;
  59.         }
  60.     }else {
  61.         fp = openPipe(execList, err);
  62.         waitchild = (*err == 0);
  63.         ext = (extension && *extension) ? extension : execList[1];
  64.         free((void *)execList);
  65.         execList = NULL;
  66.         if (fp == NULL) {
  67.             if (*err == 0) *err = Err_OPEN;
  68.             if (waitchild)
  69.                 (void)wait(0);    /* openPipe() calles fork() */
  70.             return NULL;
  71.         }
  72.     }
  73.  
  74.     if ((cinf = loadPpmHeader(fp, err)) == NULL) {
  75.         (void)fclose(fp);
  76.         if (waitchild)
  77.             (void)wait(0);    /* openPipe() calles fork() */
  78.         return NULL;
  79.     }
  80.  
  81.     if (!makeMapOnly)
  82.         [self initLocateWindow:fileName
  83.             width:cinf->width height:cinf->height];
  84.  
  85. /* Bitmap data of planes in 'map[]' is one block of memory area.
  86.    map[0] is beginning of the area, and map[1] = map[0] + (size of plane),
  87.    and so on. But, if the image is monochrome, map[1] is NULL.
  88.    The area of map[0] and (commonInfo *)cinf are kept in an object of
  89.    ToyView, and freed by it.
  90. */
  91.     alert = ppmGetImage(fp, cinf, map, ext);
  92.     (void)fclose(fp);
  93.     if (waitchild)
  94.         (void)wait(0);    /* openPipe() calles fork() */
  95.     if (alert) /* •ä„ö⁄˛⁄ë */
  96.         warnAlert(fileName, alert);
  97.  
  98.     if (makeMapOnly)
  99.         return cinf;
  100.     if ([self drawView:map info: cinf] == nil)
  101.         *err = -1;
  102.     return cinf;
  103. }
  104.  
  105. - setExecList: (char **)list ext: (const char *)type
  106.     /* list is free-ed by this obj */
  107. {
  108.     execList = list;
  109.     extension = type;
  110.     return self;
  111. }
  112.  
  113. @end
  114.