Files
libgphoto2/frontends/command-line/foreach.c
Lutz Mueller abe27e1c98 2001-08-22 Lutz M�ller <urc8@rz.uni-karlsruhe.de>
* */*: Small parameter changes to make gphoto2 API more consistent.
        More to follow.


git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@2046 67ed7778-7388-44ab-90cf-0a291f65f57c
2001-08-22 18:09:42 +00:00

139 lines
2.8 KiB
C

#include <string.h>
#include <stdio.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "main.h"
#include "actions.h"
#include "globals.h"
#include "range.h"
#include "foreach.h"
int
for_each_subfolder (char *folder, folder_action faction,
image_action iaction, int recurse)
{
CameraList folderlist;
char prefix[1024], subfolder[1024];
int i, res;
prefix[0] = 0;
subfolder[0] = 0;
if (folder != NULL && *folder != '\0')
strcat(prefix, folder);
else
strcat(prefix, glob_folder);
res = gp_camera_folder_list_folders (glob_camera, prefix, &folderlist);
if (res != GP_OK)
return (res);
if ((glob_quiet)&&(faction == print_folder))
printf ("%i\n", gp_list_count(&folderlist));
for (i = 0; i < gp_list_count(&folderlist); i++) {
const char *name;
gp_list_get_name (&folderlist, i, &name);
sprintf(subfolder, "%s/%s", prefix, name);
res = faction (subfolder, iaction, recurse);
if (res != GP_OK)
return (res);
if (recurse)
for_each_subfolder (subfolder, faction, iaction,
recurse);
}
return (GP_OK);
}
int
for_each_image (char *folder, image_action iaction, int reverse)
{
CameraList filelist;
int i, res;
const char *name;
res = gp_camera_folder_list_files (glob_camera, folder, &filelist);
if (res != GP_OK)
return (res);
if (reverse) {
for (i = gp_list_count (&filelist) - 1; 0 <= i; i--) {
gp_list_get_name (&filelist, i, &name);
res = iaction (folder, (char*)name);
if (res != GP_OK)
return (res);
}
} else {
for (i = 0; i < gp_list_count (&filelist); i++) {
gp_list_get_name (&filelist, i, &name);
res = iaction (folder, (char*)name);
if (res != GP_OK)
return (res);
}
}
return (GP_OK);
}
int
for_each_image_in_range (char *folder, char *range, image_action action,
int reverse)
{
char index[MAX_IMAGE_NUMBER];
int i, max = 0;
int res;
const char *name;
CameraList filelist;
memset(index, 0, MAX_IMAGE_NUMBER);
res = parse_range (range, index);
if (res != GP_OK) {
cli_error_print("Invalid range");
return (res);
}
res = gp_camera_folder_list_files (glob_camera, folder, &filelist);
if (res != GP_OK)
return (res);
for (max = MAX_IMAGE_NUMBER - 1; !index[max]; max--) {}
if ((res = gp_list_count(&filelist)) < 0)
return (res);
if (res < max + 1) {
cli_error_print ("Picture number %i is too large. "
"Available %i picture(s).", max + 1, res);
return (GP_ERROR_FILE_NOT_FOUND);
}
if (reverse) {
for (i = max; 0 <= i; i--)
if (index[i]) {
gp_list_get_name (&filelist, i, &name);
res = action (folder, (char*)name);
if (res != GP_OK)
return (res);
}
} else
for (i = 0; i <= max; i++)
if (index[i]) {
gp_list_get_name (&filelist, i, &name);
res = action (folder, (char*)name);
if (res != GP_OK)
return (res);
}
return (GP_OK);
}