Logo Search packages:      
Sourcecode: libgphoto2 version File versions

int gp_file_open ( CameraFile file,
const char *  filename 
)

Parameters:
file a CameraFile
filename 
Returns:
a gphoto2 error code.

Definition at line 511 of file gphoto2-file.c.

References CHECK_NULL, CHECK_RESULT, GP_ERROR, GP_ERROR_NO_MEMORY, GP_FILE_ACCESSTYPE_FD, GP_FILE_ACCESSTYPE_MEMORY, gp_file_clean(), gp_log(), GP_LOG_ERROR, and GP_OK.

{
        FILE *fp;
        char *name, *dot;
        long size, size_read;
        int  i;
      struct stat s;

        /*
         * mime types that cannot be determined by the filename
         * extension. Better hack would be to use library that examine
         * file content instead, like gnome-vfs mime handling, or
         * gnome-mime, whatever.
         * See also the GP_MIME_* definitions.
         */
        static char *mime_table[] = {
            "bmp",  GP_MIME_BMP,
            "jpg",  GP_MIME_JPEG,
            "tif",  GP_MIME_TIFF,
            "ppm",  GP_MIME_PPM,
            "pgm",  GP_MIME_PGM,
            "pnm",  GP_MIME_PNM,
            "png",  GP_MIME_PNG,
            "wav",  GP_MIME_WAV,
            "avi",  GP_MIME_AVI,
            "mp3",  GP_MIME_MP3,
            "wma",  GP_MIME_WMA,
            "asf",  GP_MIME_ASF,
            "ogg",  GP_MIME_OGG,
            "mpg",  GP_MIME_MPEG,
            NULL};

      CHECK_NULL (file && filename);

      CHECK_RESULT (gp_file_clean (file));

        fp = fopen(filename, "r");
        if (!fp)
            return (GP_ERROR);
      fseek (fp, 0, SEEK_END);
        size = ftell (fp);
        rewind (fp);

      switch (file->accesstype) {
      case GP_FILE_ACCESSTYPE_MEMORY:
            file->data = malloc (sizeof(char)*(size + 1));
            if (!file->data)
                  return (GP_ERROR_NO_MEMORY);
            size_read = fread (file->data, (size_t)sizeof(char), (size_t)size, fp);
            if (ferror(fp)) {
                  gp_file_clean (file);
                  fclose (fp);
                  return (GP_ERROR);
            }
            fclose(fp);
            file->size = size_read;
            file->data[size_read] = 0;
            break;
      case GP_FILE_ACCESSTYPE_FD: {
            if (file->fd == -1) {
                  file->fd = dup(fileno(fp));
                  fclose (fp);
                  break;
            }
            gp_log (GP_LOG_ERROR, "gp_file_open", "Needs to be initialized with fd=-1 to work");
            fclose (fp);
            return GP_ERROR;
      }
      default:
            break;
      }

        name = strrchr (filename, '/');
        if (name)
                strncpy (file->name, name + 1, sizeof (file->name));
           else
                strncpy (file->name, filename, sizeof (file->name));

        /* MIME lookup */
        dot = strrchr (filename, '.');
        if (dot) {
            for (i = 0; mime_table[i] ; i+=2)
                if (!strcasecmp (mime_table[i], dot+1)) {
                    strncpy (file->mime_type, mime_table[i+1], sizeof(file->mime_type));
                    break;
                }
            if (!mime_table[i])
                /*
                 * We did not found the type in the lookup table,
                 * so we use the file suffix as mime type.
                 * Note: This should probably use GP_MIME_UNKNOWN instead
                 * of returning a non-standard type.
                 */
                sprintf(file->mime_type, "image/%s", dot + 1);
        } else
            /*
             * Damn, no filename suffix...
             */
            strncpy (file->mime_type, GP_MIME_UNKNOWN,
                 sizeof (file->mime_type));

      if (stat (filename, &s) != -1) {
            file->mtime = s.st_mtime;
      } else {
            file->mtime = time (NULL);
      }

        return (GP_OK);
}


Generated by  Doxygen 1.6.0   Back to index