- For my solution I decided to read
and store JPEG file information
into a structure called, Images.
If the system hasn't defined a maximum
length for the path or filename,
the header file sets constants for both
PATH_MAX and NAME_MAX.
The BUFFSIZE constant,
represents the maximum string length
for an image caption.
Which can be up to 2,000 characters.
Also, note the UNIX definition.
If you're compiling under Windows,
change the definition to MSDOS,
and save that before compiling.
To store the image structures,
I use the second and most terrifying
C Language construct after an array of pointers,
the Linked List.
The structure for that list is defined
in the header file as Images.
It stores the image name, filename extension,
caption, directory, and of course, a link
to the next item in the list.
The source code for PicNsort C
is divided into three parts.
The first part is handled in the main
function, which includes checking
for command line arguments.
The second part is handled by the
finddir function, which recursively
scans directories collecting data on JPEG files.
The final part, is the Output Results Routine
which outputs the results, as its name implies.
The finddir function recursively searches
the dame directory tree.
It skips over any .DOT files.
When a subdirectory is found,
the function is called recursively
to continue scanning.
Otherwise, the filename is scanned
for a JPEG or PNG filename extension.
If so, the filename, extension, and path
are saved into a new structure
which is obtained by calling the add_item function.
Now, I use two strstr functions
in these three if statements.
The goal is to locate a JPEG or PNG file
by its extension.
A better way to find the extension would be
to use the strcase str function
which compares strings while ignoring their case.
But that function isn't found in every
standard C library.
The add_item function serves two purposes.
First, it builds the linked list.
Space is allocated for the first item in the list
or allocated for the next item.
The allocate_ image_ structure function
handles the details of grabbing
a new chuck of memory.
Data is then copied into the structure
save for the caption member.
The second purpose of the add_item function
is to spawn the EXIF tool program
and fetch its results.
The command is built and placed into the
command buffer.
I use the popen function to run EXIF tool
that way the command's output can be captured
by using fgets.
The command must be run twice.
Once each to account for both exif fields.
The first caption abstract, and the second description.
When no caption is available,
a dummy caption is assigned the text is later
scanned for and discarded as dealing with a
captionless image file isn't part of the assignment.
The fix_caption function copies the caption part
of the string from the 34th character position
to the first position and then caps the string
with a null character.
The final text is then added to the image
structure in the linked list.
The final function is, and does, output results.
It processes the linked list
from the first item to the last.
I chose not to sort the linked list
which would be a pain.
Instead, I loop through the alphabet
and match captions to that letter.
The loop also keeps track of the current
matches so that you don't see double output.
I call this method a Forced Sort.
Output changes whether the move
or copy options are set, if so then
the appropriate move or copy commands
are used in the format appropriate
for the operating system.
Otherwise the data is dumped from a linked list
without any scripting commands.
All the information is present in this code
to actually move or copy the files.
Still, I chose to output text commands
that gives the user an option of reviewing
or editing the script,
which helps keep things flexible.