Annotation of researchv10no/libpicfile/picopen_r.c, revision 1.1.1.1

1.1       root        1: #include <picfile.h>
                      2: #include <stdio.h>
                      3: #include <libc.h>
                      4: #define        NLINE   1024            /* maximum length of header line */
                      5: PWR_error(f, buf)
                      6: PICFILE *f;
                      7: char *buf;
                      8: {
                      9:        return 0;
                     10: }
                     11: PICFILE *picopen_r(file)
                     12: char *file;
                     13: {
                     14:        char line[NLINE+1];
                     15:        register char *lp;
                     16:        char *nchan, *window, *command;
                     17:        int x0, y0, x1, y1, i, n;
                     18:        PICFILE *p=(PICFILE *)malloc(sizeof(PICFILE));
                     19:        if(p==0){
                     20:                PIC_error="Can't allocate PICFILE";
                     21:                return 0;
                     22:        }
                     23:        p->flags=PIC_NOCLOSE|PIC_INPUT;
                     24:        p->argc=-1;
                     25:        p->cmap=0;
                     26:        if(strcmp(file, "IN")==0)
                     27:                p->fd=0;
                     28:        else{
                     29:                p->fd=open(file, 0);
                     30:                if(p->fd<0){
                     31:                Error:
                     32:                        if(!(p->flags&PIC_NOCLOSE)) close(p->fd);
                     33:                        if(p->argc>=0){
                     34:                                for(i=0;i!=p->argc;i++) free(p->argv[i]);
                     35:                                free((char *)p->argv);
                     36:                        }
                     37:                        if(p->cmap) free(p->cmap);
                     38:                        free((char *)p);
                     39:                        return 0;
                     40:                }
                     41:                p->flags=PIC_INPUT;
                     42:        }
                     43:        p->argc=0;
                     44:        p->argv=(char **)malloc(sizeof(char *));
                     45:        p->argv[0]=0;
                     46:        for(;;){
                     47:                lp=line;
                     48:                do{
                     49:                        if(lp==&line[NLINE] || read(p->fd, lp, 1)!=1){
                     50:                                PIC_error="Can't read header";
                     51:                                goto Error;
                     52:                        }
                     53:                }while(*lp++!='\n');
                     54:                if(lp==line+1)
                     55:                        break;
                     56:                lp[-1]='\0';
                     57:                lp=strchr(line, '=');
                     58:                if(lp==0){
                     59:                        PIC_error="Ill-formed header line";
                     60:                        goto Error;
                     61:                }
                     62:                *lp++='\0';
                     63:                picputprop(p, line, lp);
                     64:        }
                     65:        if(strncmp(p->argv[0], "TYPE=", 5)!=0){
                     66:                PIC_error="First header line not TYPE=";
                     67:                goto Error;
                     68:        }
                     69:        p->type=p->argv[0]+5;
                     70:        for(i=0;PIC_conf[i].type;i++)
                     71:                if(strcmp(p->type, PIC_conf[i].type)==0) break;
                     72:        if(!PIC_conf[i].type){
                     73:                PIC_error="Illegal TYPE";
                     74:                goto Error;
                     75:        }
                     76:        p->rd=PIC_conf[i].rd;
                     77:        p->wr=PWR_error;
                     78:        p->cl=PIC_conf[i].cl;
                     79:        p->line=0;
                     80:        p->buf=0;
                     81:        p->chan=picgetprop(p, "CHAN");
                     82:        nchan=picgetprop(p, "NCHAN");
                     83:        if(PIC_conf[i].nchan){
                     84:                p->nchan=PIC_conf[i].nchan;
                     85:                if(nchan && p->nchan!=atoi(nchan)){
                     86:                        PIC_error="wrong NCHAN for this TYPE";
                     87:                        goto Error;
                     88:                }
                     89:                if(p->chan==0) switch(p->nchan){
                     90:                case 1: p->chan="m"; break;
                     91:                case 3: p->chan="rgb"; break;
                     92:                }
                     93:        }
                     94:        else if(nchan)
                     95:                p->nchan=atoi(nchan);
                     96:        else if(p->chan)
                     97:                p->nchan=strlen(p->chan);
                     98:        else{
                     99:                PIC_error="Neither CHAN nor NCHAN in header";
                    100:                goto Error;
                    101:        }
                    102:        if(p->chan==0 && p->nchan<=16)
                    103:                p->chan="????????????????"+16-p->nchan;
                    104:        window=picgetprop(p, "WINDOW");
                    105:        if(window==0){
                    106:                PIC_error="No WINDOW in header";
                    107:                goto Error;
                    108:        }
                    109:        if(sscanf(window, "%d%d%d%d", &x0, &y0, &x1, &y1)!=4) goto Error;
                    110:        if(x0<x1){
                    111:                p->x=x0;
                    112:                p->width=x1-x0;
                    113:        }
                    114:        else{
                    115:                p->x=x1;
                    116:                p->width=x0-x1;
                    117:        }
                    118:        if(y0<y1){
                    119:                p->y=y0;
                    120:                p->height=y1-y0;
                    121:        }
                    122:        else{
                    123:                p->y=y1;
                    124:                p->height=y0-y1;
                    125:        }
                    126:        if(picgetprop(p, "CMAP")){
                    127:                p->cmap=malloc(3*256);
                    128:                if(p->cmap==0){
                    129:                        PIC_error="Can't allocate color map";
                    130:                        goto Error;
                    131:                }
                    132:                i=0;
                    133:                lp=p->cmap;
                    134:                while(i!=3*256){
                    135:                        n=read(p->fd, lp, 3*256-i);
                    136:                        if(n<=0){
                    137:                                PIC_error="Can't read color map";
                    138:                                goto Error;
                    139:                        }
                    140:                        i+=n;
                    141:                        lp+=n;
                    142:                }
                    143:        }
                    144:        else
                    145:                p->cmap=0;
                    146:        command=picgetprop(p, "COMMAND");
                    147:        if(command==0){
                    148:                sprintf(line, "magically-create %s", file);
                    149:                command=line;
                    150:        }
                    151:        if(PIC_command==0){
                    152:                PIC_command=malloc(strlen(command)+1);
                    153:                if(PIC_command)
                    154:                        strcpy(PIC_command, command);
                    155:        }
                    156:        else{
                    157:                PIC_command=realloc(PIC_command, strlen(PIC_command)+strlen(command)+2);
                    158:                if(PIC_command){
                    159:                        strcat(PIC_command, "\n");
                    160:                        strcat(PIC_command, command);
                    161:                }
                    162:        }
                    163:        return p;
                    164: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.