Пример #1
0
GImage *GImageRead(char * filename) {
    /* Go read an input image file. Return NULL if cannot guess file type */
    /* First try filename dot3 extension then try sniffing if can't guess */
    char *mime;

    if ( filename!=NULL && GFileExists(filename) &&
            ((mime=GIOguessMimeType(filename)) || (mime=GIOGetMimeType(filename))) ) {

        if ( strcasecmp(mime,"image/bmp")==0 ) {
            free(mime);
            return( GImageReadBmp(filename) );
        } else if ( strcasecmp(mime,"image/x-xbitmap")==0 ) {
            free(mime);
            return( GImageReadXbm(filename) );
        } else if ( strcasecmp(mime,"image/x-xpixmap")==0 ) {
            free(mime);
            return( GImageReadXpm(filename) );
#ifndef _NO_LIBTIFF
        } else if ( strcasecmp(mime,"image/tiff")==0 ) {
            free(mime);
            return( GImageReadTiff(filename) );
#endif
#ifndef _NO_LIBJPEG
        } else if ( strcasecmp(mime,"image/jpeg")==0 ) {
            free(mime);
            return( GImageReadJpeg(filename) );
#endif
#ifndef _NO_LIBPNG
        } else if ( strcasecmp(mime,"image/png")==0 ) {
            free(mime);
            return( GImageReadPng(filename) );
#endif
#ifndef _NO_LIBUNGIF
        } else if ( strcasecmp(mime,"image/gif")==0 ) {
            free(mime);
            return( GImageReadGif(filename) );
#endif
        } else if ( strcasecmp(mime,"image/x-cmu-raster")==0 || \
                    strcasecmp(mime,"image/x-sun-raster")==0 ) {
            free(mime);
            return( GImageReadRas(filename) );		/* Sun raster */
        } else if ( strcasecmp(mime,"image/x-rgb")==0 || \
                    strcasecmp(mime,"image/x-sgi")==0 ) {
            free(mime);
            return( GImageReadRgb(filename) );		/* SGI format */
        }
        free(mime);
    }

    return( NULL );
}
Пример #2
0
static void _gio_file_dir(GIOControl *gc,char *path) {
    DIR *dir;
    struct dirent *ent;
    GDirEntry *head=NULL, *last=NULL, *cur;
    char *buffer, *ept, *temp;
    struct stat statb;

    dir = opendir(path);
    if ( dir==NULL ) {
	_GIO_reporterror(gc,errno);
return;
    }

    buffer = (char *) malloc(strlen(path)+FILENAME_MAX+3);
    strcpy(buffer,path);
    ept = buffer+strlen(buffer);
    if ( ept[-1]!='/' )
	*ept++ = '/';

    while (( ent = readdir(dir))!=NULL ) {
	cur = (GDirEntry *) calloc(1,sizeof(GDirEntry));
	cur->name = def2u_copy(ent->d_name);
	strcpy(ept,ent->d_name);
	stat(buffer,&statb);
	cur->hasdir = cur->hasexe = cur->hasmode = cur->hassize = cur->hastime = true;
	cur->size    = statb.st_size;
	cur->mode    = statb.st_mode;
	cur->modtime = statb.st_mtime;
	cur->isdir   = S_ISDIR(cur->mode);
	cur->isexe   = !cur->isdir && (cur->mode & 0100);
	temp = NULL;
	// Things go badly if we open a pipe or a device. So we don't.
#ifdef __MINGW32__
	//Symlinks behave differently on Windows and are transparent, so no S_ISLNK.
	if (S_ISREG(statb.st_mode) || S_ISDIR(statb.st_mode)) {
#else
	if (S_ISREG(statb.st_mode) || S_ISDIR(statb.st_mode) || S_ISLNK(statb.st_mode)) {
#endif
	  // We look at the file and try to determine a MIME type.
	  if ( (temp=GIOguessMimeType(buffer)) || (temp=GIOGetMimeType(buffer)) ) {
	      cur->mimetype = u_copy(c_to_u(temp));
	      free(temp);
	  }
	}
	if ( last==NULL )
	    head = last = cur;
	else {
	    last->next = cur;
	    last = cur;
	}
    }
#if __CygWin
    /* Under cygwin we should give the user access to /cygdrive, even though */
    /*  a diropen("/") will not find it */
    if ( strcmp(path,"/")==0 ) {
	cur = (GDirEntry *) calloc(1,sizeof(GDirEntry));
	cur->name = def2u_copy("cygdrive");
	strcpy(ept,"cygdrive");
	stat(buffer,&statb);
	cur->hasdir = cur->hasexe = cur->hasmode = cur->hassize = cur->hastime = true;
	cur->size    = statb.st_size;
	cur->mode    = statb.st_mode;
	cur->modtime = statb.st_mtime;
	cur->isdir   = S_ISDIR(cur->mode);
	cur->isexe   = !cur->isdir && (cur->mode & 0100);
	if ( last==NULL )
	    head = last = cur;
	else {
	    last->next = cur;
	    last = cur;
	}
    }
#endif
    closedir(dir);
    free(buffer);
    gc->iodata = head;
    gc->direntrydata = true;
    gc->return_code = 200;
    gc->done = true;
    (gc->receivedata)(gc);
}

static void _gio_file_statfile(GIOControl *gc,char *path) {
    GDirEntry *cur;
    struct stat statb;

    if ( stat(path,&statb)==-1 ) {
	_GIO_reporterror(gc,errno);
    } else {
	cur = (GDirEntry *) calloc(1,sizeof(GDirEntry));
	cur->name = uc_copy(GFileNameTail(path));
	cur->hasdir = cur->hasexe = cur->hasmode = cur->hassize = cur->hastime = true;
	cur->size    = statb.st_size;
	cur->mode    = statb.st_mode;
	cur->modtime = statb.st_mtime;
	cur->isdir   = S_ISDIR(cur->mode);
	cur->isexe   = !cur->isdir && (cur->mode & 0100);
	gc->iodata = cur;
	gc->direntrydata = true;
	gc->return_code = 200;
	gc->done = true;
	(gc->receivedata)(gc);
    }
}
Пример #3
0
static void _gio_file_dir(GIOControl *gc,char *path) {
    DIR *dir;
    struct dirent *ent;
    GDirEntry *head=NULL, *last=NULL, *cur;
    char *buffer, *ept;
    struct stat statb;

    dir = opendir(path);
    if ( dir==NULL ) {
	_GIO_reporterror(gc,errno);
return;
    }

    buffer = (char *) galloc(strlen(path)+FILENAME_MAX+3);
    strcpy(buffer,path);
    ept = buffer+strlen(buffer);
    if ( ept[-1]!='/' )
	*ept++ = '/';

    while (( ent = readdir(dir))!=NULL ) {
	cur = (GDirEntry *) gcalloc(1,sizeof(GDirEntry));
	cur->name = def2u_copy(ent->d_name);
	strcpy(ept,ent->d_name);
	stat(buffer,&statb);
	cur->hasdir = cur->hasexe = cur->hasmode = cur->hassize = cur->hastime = true;
	cur->size    = statb.st_size;
	cur->mode    = statb.st_mode;
	cur->modtime = statb.st_mtime;
	cur->isdir   = S_ISDIR(cur->mode);
	cur->isexe   = !cur->isdir && (cur->mode & 0100);
	cur->mimetype= u_copy(c_to_u(GIOGetMimeType(buffer, false)));
	
	if ( last==NULL )
	    head = last = cur;
	else {
	    last->next = cur;
	    last = cur;
	}
    }
#if __CygWin
    /* Under cygwin we should give the user access to /cygdrive, even though */
    /*  a diropen("/") will not find it */
    if ( strcmp(path,"/")==0 ) {
	cur = (GDirEntry *) gcalloc(1,sizeof(GDirEntry));
	cur->name = def2u_copy("cygdrive");
	strcpy(ept,"cygdrive");
	stat(buffer,&statb);
	cur->hasdir = cur->hasexe = cur->hasmode = cur->hassize = cur->hastime = true;
	cur->size    = statb.st_size;
	cur->mode    = statb.st_mode;
	cur->modtime = statb.st_mtime;
	cur->isdir   = S_ISDIR(cur->mode);
	cur->isexe   = !cur->isdir && (cur->mode & 0100);
	if ( last==NULL )
	    head = last = cur;
	else {
	    last->next = cur;
	    last = cur;
	}
    }
#endif
    closedir(dir);
    free(buffer);
    gc->iodata = head;
    gc->direntrydata = true;
    gc->return_code = 200;
    gc->done = true;
    (gc->receivedata)(gc);
}
Пример #4
0
GImage *GImageRead(char * filename) {
/* Go read an input image file. Return NULL if cannot guess file type */
    char *mime, *pt;

    if (filename == NULL )
	return( NULL );

    /* Try finding correct routine to use based on GTK mime type */
    if ( GFileExists(filename) ) {
	mime=GIOGetMimeType(filename, true);

	if ( strcasecmp(mime,"image/bmp")==0 )
	    return( GImageReadBmp(filename) );
	else if ( strcasecmp(mime,"image/x-xbitmap")==0 )
	    return( GImageReadXbm(filename) );
	else if ( strcasecmp(mime,"image/x-xpixmap")==0 )
	    return( GImageReadXpm(filename) );
	else if ( strcasecmp(mime,"image/tiff")==0 )
	    return( GImageReadTiff(filename) );
	else if ( strcasecmp(mime,"image/jpeg")==0 )
	    return( GImageReadJpeg(filename) );
	else if ( strcasecmp(mime,"image/png")==0 )
	    return( GImageReadPng(filename) );
	else if ( strcasecmp(mime,"image/gif")==0 )
	    return( GImageReadGif(filename) );
	else if ( strcasecmp(mime,"image/x-cmu-raster")==0 || \
		  strcasecmp(mime,"image/x-sun-raster")==0 )
	    return( GImageReadRas(filename) );		/* Sun raster */
	else if ( strcasecmp(mime,"image/x-rgb")==0 || \
		  strcasecmp(mime,"image/x-sgi")==0 )
	    return( GImageReadRgb(filename) );		/* SGI format */
    }

    /* Try finding correct routine to use based on filename suffix */
    if ( (pt=strrchr(filename,'.'))!=NULL ) {

	if ( strmatch(pt,".bmp")==0 )
	    return( GImageReadBmp(filename) );
	else if ( strmatch(pt,".xbm")==0 )
	    return( GImageReadXbm(filename) );
	else if ( strmatch(pt,".xpm")==0 )
	    return( GImageReadXpm(filename) );
	else if ( strmatch(pt,".tiff")==0 || strmatch(pt,".tif")==0 )
	    return( GImageReadTiff(filename) );
	else if ( strmatch(pt,".jpeg")==0 || strmatch(pt,".jpg")==0 )
	    return( GImageReadJpeg(filename) );
	else if ( strmatch(pt,".png")==0 )
	    return( GImageReadPng(filename) );
	else if ( strmatch(pt,".gif")==0 )
	    return( GImageReadGif(filename) );
	else if ( strmatch(pt,".ras")==0 || strmatch(pt,".im1")==0 || \
		  strmatch(pt,".im8")==0 || strmatch(pt,".im24")==0 || \
		  strmatch(pt,".im32")==0 || strmatch(pt,".rs")==0 || \
		  strmatch(pt,".sun")==0 )
	    return( GImageReadRas(filename) );		/* Sun raster */
	else if ( strmatch(pt,".rgb")==0 || strmatch(pt,".rgba")==0 || \
		  strmatch(pt,".sgi")==0 || strmatch(pt,".bw")==0 )
	    return( GImageReadRgb(filename) );		/* SGI format */
    }

    return( NULL );
}