home *** CD-ROM | disk | FTP | other *** search
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % U U TTTTT IIIII L IIIII TTTTT Y Y %
- % U U T I L I T Y Y %
- % U U T I L I T Y %
- % U U T I L I T Y %
- % UUU T IIIII LLLLL IIIII T Y %
- % %
- % %
- % ImageMagick Utility Routines %
- % %
- % %
- % %
- % Software Design %
- % John Cristy %
- % January 1993 %
- % %
- % %
- % Copyright 1994 E. I. du Pont de Nemours & Company %
- % %
- % Permission to use, copy, modify, distribute, and sell this software and %
- % its documentation for any purpose is hereby granted without fee, %
- % provided that the above Copyright notice appear in all copies and that %
- % both that Copyright notice and this permission notice appear in %
- % supporting documentation, and that the name of E. I. du Pont de Nemours %
- % & Company not be used in advertising or publicity pertaining to %
- % distribution of the software without specific, written prior %
- % permission. E. I. du Pont de Nemours & Company makes no representations %
- % about the suitability of this software for any purpose. It is provided %
- % "as is" without express or implied warranty. %
- % %
- % E. I. du Pont de Nemours & Company disclaims all warranties with regard %
- % to this software, including all implied warranties of merchantability %
- % and fitness, in no event shall E. I. du Pont de Nemours & Company be %
- % liable for any special, indirect or consequential damages or any %
- % damages whatsoever resulting from loss of use, data or profits, whether %
- % in an action of contract, negligence or other tortuous action, arising %
- % out of or in connection with the use or performance of this software. %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- %
- */
-
- /*
- Include declarations.
- */
- #include "magick.h"
- #include "image.h"
- #include "utility.h"
- #include "X.h"
-
- /*
- Forward declarations.
- */
- unsigned int
- ReadData _Declare((char *,int,int,FILE *));
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % G l o b E x p e s s i o n %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function GlobExpression returns True if the expression matches the pattern.
- %
- % The format of the GlobExpression function is:
- %
- % GlobExpression(expression,pattern)
- %
- % A description of each parameter follows:
- %
- % o expression: Specifies a pointer to a text string containing a file name.
- %
- % o pattern: Specifies a pointer to a text string containing a pattern.
- %
- %
- */
- int GlobExpression(expression,pattern)
- char
- *expression,
- *pattern;
- {
- int
- done;
-
- if (pattern == (char *) NULL)
- return(True);
- if (strlen(pattern) == 0)
- return(True);
- if (strcmp(pattern,"*") == 0)
- return(True);
- done=False;
- while ((*pattern != '\0') && !done)
- {
- if (*expression == '\0')
- if ((*pattern != '{') && (*pattern != '*'))
- break;
- switch (*pattern)
- {
- case '\\':
- {
- pattern++;
- if (*pattern != '\0')
- pattern++;
- break;
- }
- case '*':
- {
- int
- status;
-
- pattern++;
- status=False;
- while ((*expression != '\0') && !status)
- status=GlobExpression(expression++,pattern);
- if (status)
- {
- while (*expression != '\0')
- expression++;
- while (*pattern != '\0')
- pattern++;
- }
- break;
- }
- case '[':
- {
- char
- c;
-
- pattern++;
- for ( ; ; )
- {
- if ((*pattern == '\0') || (*pattern == ']'))
- {
- done=True;
- break;
- }
- if (*pattern == '\\')
- {
- pattern++;
- if (*pattern == '\0')
- {
- done=True;
- break;
- }
- }
- if (*(pattern+1) == '-')
- {
- c=(*pattern);
- pattern+=2;
- if (*pattern == ']')
- {
- done=True;
- break;
- }
- if (*pattern == '\\')
- {
- pattern++;
- if (*pattern == '\0')
- {
- done=True;
- break;
- }
- }
- if ((*expression < c) || (*expression > *pattern))
- {
- pattern++;
- continue;
- }
- }
- else
- if (*pattern != *expression)
- {
- pattern++;
- continue;
- }
- pattern++;
- while ((*pattern != ']') && (*pattern != '\0'))
- {
- if ((*pattern == '\\') && (*(pattern+1) != '\0'))
- pattern++;
- pattern++;
- }
- if (*pattern != '\0')
- {
- pattern++;
- expression++;
- }
- break;
- }
- break;
- }
- case '?':
- {
- pattern++;
- expression++;
- break;
- }
- case '{':
- {
- int
- match;
-
- register char
- *p;
-
- pattern++;
- while ((*pattern != '}') && (*pattern != '\0'))
- {
- p=expression;
- match=True;
- while ((*p != '\0') && (*pattern != '\0') &&
- (*pattern != ',') && (*pattern != '}') && match)
- {
- if (*pattern == '\\')
- pattern++;
- match=(*pattern == *p);
- p++;
- pattern++;
- }
- if (*pattern == '\0')
- {
- match=False;
- done=True;
- break;
- }
- else
- if (match)
- {
- expression=p;
- while ((*pattern != '}') && (*pattern != '\0'))
- {
- pattern++;
- if (*pattern == '\\')
- {
- pattern++;
- if (*pattern == '}')
- pattern++;
- }
- }
- }
- else
- {
- while ((*pattern != '}') && (*pattern != ',') &&
- (*pattern != '\0'))
- {
- pattern++;
- if (*pattern == '\\')
- {
- pattern++;
- if ((*pattern == '}') || (*pattern == ','))
- pattern++;
- }
- }
- }
- if (*pattern != '\0')
- pattern++;
- }
- break;
- }
- default:
- {
- if (*expression != *pattern)
- done=True;
- else
- {
- expression++;
- pattern++;
- }
- }
- }
- }
- while (*pattern == '*')
- pattern++;
- return((*expression == '\0') && (*pattern == '\0'));
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % L i s t F i l e s %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function ListFiles reads the directory specified and returns a list
- % of filenames contained in the directory sorted in ascending alphabetic
- % order.
- %
- % The format of the ListFiles function is:
- %
- % filelist=ListFiles(directory,pattern,number_entries)
- %
- % A description of each parameter follows:
- %
- % o filelist: Function ListFiles returns a list of filenames contained
- % in the directory. If the directory specified cannot be read or it is
- % a file a NULL list is returned.
- %
- % o directory: Specifies a pointer to a text string containing a directory
- % name.
- %
- % o pattern: Specifies a pointer to a text string containing a pattern.
- %
- % o number_entries: This integer returns the number of filenames in the
- % list.
- %
- %
- */
- static int FileCompare(x,y)
- const void
- *x,
- *y;
- {
- register char
- *p,
- *q;
-
- p=(char *) *((char **) x);
- q=(char *) *((char **) y);
- while ((*p != '\0') && (*q != '\0') && (*p == *q))
- {
- p++;
- q++;
- }
- return(*p-(*q));
- }
-
- char **ListFiles(directory,pattern,number_entries)
- char
- *directory,
- *pattern;
-
- int
- *number_entries;
- {
- #ifndef vms
- #define UpDirectoryName ".."
-
- char
- **filelist,
- working_directory[MaxTextLength];
-
- DIR
- *current_directory;
-
- struct dirent
- *entry;
-
- struct stat
- file_status;
-
- unsigned int
- max_entries;
-
- /*
- Open directory.
- */
- *number_entries=0;
- current_directory=opendir(directory);
- if (current_directory == (DIR *) NULL)
- return((char **) NULL);
- /*
- Allocate filelist.
- */
- max_entries=2048;
- filelist=(char **) malloc(max_entries*sizeof(char *));
- if (filelist == (char **) NULL)
- {
- (void) closedir(current_directory);
- return((char **) NULL);
- }
- /*
- Save the current and change to the new directory.
- */
- (void) getcwd(working_directory,MaxTextLength-1);
- (void) chdir(directory);
- entry=readdir(current_directory);
- while (entry != (struct dirent *) NULL)
- {
- if (*entry->d_name == '.')
- {
- entry=readdir(current_directory);
- continue;
- }
- (void) stat(entry->d_name,&file_status);
- if (S_ISDIR(file_status.st_mode) || GlobExpression(entry->d_name,pattern))
- {
- if (*number_entries >= max_entries)
- {
- max_entries<<=1;
- filelist=(char **)
- realloc((char *) filelist,max_entries*sizeof(char *));
- if (filelist == (char **) NULL)
- {
- (void) closedir(current_directory);
- return((char **) NULL);
- }
- }
- filelist[*number_entries]=(char *) malloc(strlen(entry->d_name)+2);
- if (filelist[*number_entries] == (char *) NULL)
- break;
- (void) strcpy(filelist[*number_entries],entry->d_name);
- if (S_ISDIR(file_status.st_mode))
- (void) strcat(filelist[*number_entries],DirectorySeparator);
- (*number_entries)++;
- }
- entry=readdir(current_directory);
- }
- (void) closedir(current_directory);
- /*
- Sort filelist in ascending order.
- */
- (void) qsort((void *) filelist,*number_entries,sizeof(char **),FileCompare);
- /*
- Determine current working directory.
- */
- (void) chdir(working_directory);
- (void) chdir(directory);
- (void) getcwd(directory,MaxTextLength-1);
- return(filelist);
- #else
- return((char **) NULL);
- #endif
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % L S B F i r s t R e a d L o n g %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function LSBFirstReadLong reads a long value as a 32 bit quantity in
- % least-significant byte first order.
- %
- % The format of the LSBFirstReadLong routine is:
- %
- % value=LSBFirstReadLong(file)
- %
- % A description of each parameter follows.
- %
- % o value: Function LSBFirstReadLong returns an unsigned long read from
- % the file.
- %
- % o file: Specifies the file to read the data from.
- %
- %
- */
- unsigned long LSBFirstReadLong(file)
- FILE
- *file;
- {
- unsigned char
- buffer[4];
-
- unsigned int
- status;
-
- unsigned long
- value;
-
- status=ReadData((char *) buffer,1,4,file);
- if (status == False)
- return((unsigned long) ~0);
- value=(unsigned int) (buffer[3] << 24);
- value|=(unsigned int) (buffer[2] << 16);
- value|=(unsigned int) (buffer[1] << 8);
- value|=(unsigned int) (buffer[0]);
- return(value);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % L S B F i r s t R e a d S h o r t %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function LSBFirstReadShort reads a short value as a 16 bit quantity in
- % least-significant byte first order.
- %
- % The format of the LSBFirstReadShort routine is:
- %
- % value=LSBFirstReadShort(file)
- %
- % A description of each parameter follows.
- %
- % o value: Function LSBFirstReadShort returns an unsigned short read from
- % the file.
- %
- % o file: Specifies the file to read the data from.
- %
- %
- */
- unsigned short LSBFirstReadShort(file)
- FILE
- *file;
- {
- unsigned char
- buffer[2];
-
- unsigned int
- status;
-
- unsigned short
- value;
-
- status=ReadData((char *) buffer,1,2,file);
- if (status == False)
- return((unsigned short) ~0);
- value=(unsigned short) (buffer[1] << 8);
- value|=(unsigned short) (buffer[0]);
- return(value);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % L S B F i r s t W r i t e L o n g %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function LSBFirstWriteLong writes a long value as a 32 bit quantity in
- % least-significant byte first order.
- %
- % The format of the LSBFirstWriteLong routine is:
- %
- % LSBFirstWriteLong(value,file)
- %
- % A description of each parameter follows.
- %
- % o value: Specifies the value to write.
- %
- % o file: Specifies the file to write the data to.
- %
- %
- */
- void LSBFirstWriteLong(value,file)
- unsigned long
- value;
-
- FILE
- *file;
- {
- unsigned char
- buffer[4];
-
- buffer[0]=(unsigned char) (value);
- buffer[1]=(unsigned char) ((value) >> 8);
- buffer[2]=(unsigned char) ((value) >> 16);
- buffer[3]=(unsigned char) ((value) >> 24);
- (void) fwrite((char *) buffer,1,4,file);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % L S B F i r s t W r i t e S h o r t %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function LSBFirstWriteShort writes a long value as a 16 bit quantity in
- % least-significant byte first order.
- %
- % The format of the LSBFirstWriteShort routine is:
- %
- % LSBFirstWriteShort(value,file)
- %
- % A description of each parameter follows.
- %
- % o value: Specifies the value to write.
- %
- % o file: Specifies the file to write the data to.
- %
- %
- */
- void LSBFirstWriteShort(value,file)
- unsigned int
- value;
-
- FILE
- *file;
- {
- unsigned char
- buffer[2];
-
- buffer[0]=(unsigned char) (value);
- buffer[1]=(unsigned char) ((value) >> 8);
- (void) fwrite((char *) buffer,1,2,file);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % M S B F i r s t O r d e r L o n g %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function MSBFirstOrderLong converts a least-significant byte first buffer
- % of integers to most-significant byte first.
- %
- % The format of the MSBFirstOrderLong routine is:
- %
- % MSBFirstOrderLong(p,length);
- %
- % A description of each parameter follows.
- %
- % o p: Specifies a pointer to a buffer of integers.
- %
- % o length: Specifies the length of the buffer.
- %
- %
- */
- void MSBFirstOrderLong(p,length)
- register char
- *p;
-
- register unsigned int
- length;
- {
- register char
- c,
- *q,
- *sp;
-
- q=p+length;
- while (p < q)
- {
- sp=p+3;
- c=(*sp);
- *sp=(*p);
- *p++=c;
- sp=p+1;
- c=(*sp);
- *sp=(*p);
- *p++=c;
- p+=2;
- }
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % M S B F i r s t O r d e r S h o r t %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function MSBFirstOrderShort converts a least-significant byte first buffer
- % of integers to most-significant byte first.
- %
- % The format of the MSBFirstOrderShort routine is:
- %
- % MSBFirstOrderLongShort(p,length);
- %
- % A description of each parameter follows.
- %
- % o p: Specifies a pointer to a buffer of integers.
- %
- % o length: Specifies the length of the buffer.
- %
- %
- */
- void MSBFirstOrderShort(p,length)
- register char
- *p;
-
- register unsigned int
- length;
- {
- register char
- c,
- *q;
-
- q=p+length;
- while (p < q)
- {
- c=(*p);
- *p=(*(p+1));
- p++;
- *p++=c;
- }
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % M S B F i r s t R e a d S h o r t %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function MSBFirstReadShort reads a short value as a 16 bit quantity in
- % most-significant byte first order.
- %
- % The format of the MSBFirstReadShort routine is:
- %
- % value=MSBFirstReadShort(file)
- %
- % A description of each parameter follows.
- %
- % o value: Function MSBFirstReadShort returns an unsigned short read from
- % the file.
- %
- % o file: Specifies the file to read the data from.
- %
- %
- */
- unsigned short MSBFirstReadShort(file)
- FILE
- *file;
- {
- unsigned char
- buffer[2];
-
- unsigned int
- status;
-
- unsigned short
- value;
-
- status=ReadData((char *) buffer,1,2,file);
- if (status == False)
- return((unsigned short) ~0);
- value=(unsigned int) (buffer[0] << 8);
- value|=(unsigned int) (buffer[1]);
- return(value);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % M S B F i r s t R e a d L o n g %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function MSBFirstReadLong reads a long value as a 32 bit quantity in
- % most-significant byte first order.
- %
- % The format of the MSBFirstReadLong routine is:
- %
- % value=MSBFirstReadLong(file)
- %
- % A description of each parameter follows.
- %
- % o value: Function MSBFirstReadLong returns an unsigned long read from
- % the file.
- %
- % o file: Specifies the file to read the data from.
- %
- %
- */
- unsigned long MSBFirstReadLong(file)
- FILE
- *file;
- {
- unsigned char
- buffer[4];
-
- unsigned int
- status;
-
- unsigned long
- value;
-
- status=ReadData((char *) buffer,1,4,file);
- if (status == False)
- return((unsigned long) ~0);
- value=(unsigned int) (buffer[0] << 24);
- value|=(unsigned int) (buffer[1] << 16);
- value|=(unsigned int) (buffer[2] << 8);
- value|=(unsigned int) (buffer[3]);
- return(value);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % M S B F i r s t W r i t e L o n g %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function MSBFirstWriteLong writes a long value as a 32 bit quantity in
- % most-significant byte first order.
- %
- % The format of the MSBFirstWriteLong routine is:
- %
- % MSBFirstWriteLong(value,file)
- %
- % A description of each parameter follows.
- %
- % o value: Specifies the value to write.
- %
- % o file: Specifies the file to write the data to.
- %
- %
- */
- void MSBFirstWriteLong(value,file)
- unsigned long
- value;
-
- FILE
- *file;
- {
- unsigned char
- buffer[4];
-
- buffer[0]=(unsigned char) ((value) >> 24);
- buffer[1]=(unsigned char) ((value) >> 16);
- buffer[2]=(unsigned char) ((value) >> 8);
- buffer[3]=(unsigned char) (value);
- (void) fwrite((char *) buffer,1,4,file);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % M S B F i r s t W r i t e S h o r t %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function MSBFirstWriteShort writes a long value as a 16 bit quantity in
- % most-significant byte first order.
- %
- % The format of the MSBFirstWriteShort routine is:
- %
- % MSBFirstWriteShort(value,file)
- %
- % A description of each parameter follows.
- %
- % o value: Specifies the value to write.
- %
- % o file: Specifies the file to write the data to.
- %
- %
- */
- void MSBFirstWriteShort(value,file)
- unsigned int
- value;
-
- FILE
- *file;
- {
- unsigned char
- buffer[2];
-
- buffer[0]=(unsigned char) ((value) >> 8);
- buffer[1]=(unsigned char) (value);
- (void) fwrite((char *) buffer,1,2,file);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % R e a d D a t a %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function ReadData reads data from the image file and returns it. If it
- % cannot read the requested number of items, False is returned indicating
- % an error.
- %
- % The format of the ReadData routine is:
- %
- % status=ReadData(data,size,number_items,file)
- %
- % A description of each parameter follows:
- %
- % o status: Function ReadData returns True if all the data requested
- % is obtained without error, otherwise False.
- %
- % o data: Specifies an area to place the information reuested from
- % the file.
- %
- % o size: Specifies an integer representing the length of an
- % individual item to be read from the file.
- %
- % o number_items: Specifies an integer representing the number of items
- % to read from the file.
- %
- % o file: Specifies a file to read the data.
- %
- %
- */
- unsigned int ReadData(data,size,number_items,file)
- char
- *data;
-
- int
- size,
- number_items;
-
- FILE
- *file;
- {
- size*=number_items;
- while (size > 0)
- {
- number_items=fread(data,1,size,file);
- if (number_items <= 0)
- return(False);
- size-=number_items;
- data+=number_items;
- }
- return(True);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % R e a d D a t a B l o c k %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function ReadDataBlock reads data from the image file and returns it. The
- % amount of data is determined by first reading a count byte. If
- % ReadDataBlock cannot read the requested number of items, `-1' is returned
- % indicating an error.
- %
- % The format of the ReadData routine is:
- %
- % status=ReadData(data,file)
- %
- % A description of each parameter follows:
- %
- % o status: Function ReadData returns the number of characters read
- % unless there is an error, otherwise `-1'.
- %
- % o data: Specifies an area to place the information reuested from
- % the file.
- %
- % o file: Specifies a file to read the data.
- %
- %
- */
- int ReadDataBlock(data,file)
- char
- *data;
-
- FILE
- *file;
- {
- unsigned char
- count;
-
- unsigned int
- status;
-
- status=ReadData((char *) &count,1,1,file);
- if (status == False)
- return(-1);
- if (count == 0)
- return(0);
- status=ReadData(data,1,(int) count,file);
- if (status == False)
- return(-1);
- return(count);
- }
-
- /*
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- % %
- % %
- % %
- % S t r i n g T o L i s t %
- % %
- % %
- % %
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %
- % Function StringToList converts a text string into a list by segmenting the
- % text string at each carriage return discovered.
- %
- % The format of the StringToList routine is:
- %
- % list=StringToList(text)
- %
- % A description of each parameter follows:
- %
- % o list: Function StringToList returns the string list unless an error
- % occurs, otherwise NULL.
- %
- % o text: Specifies the string to segment into a list.
- %
- %
- */
- char **StringToList(text)
- char
- *text;
- {
- char
- **textlist;
-
- register char
- *p,
- *q;
-
- register int
- i;
-
- unsigned int
- lines;
-
- if (text == (char *) NULL)
- return((char **) NULL);
- lines=1;
- for (p=text; *p != '\0'; p++)
- if (*p == '\n')
- lines++;
- textlist=(char **) malloc((lines+1)*sizeof(char *));
- if (textlist == (char **) NULL)
- {
- Warning("Unable to convert text","Memory allocation failed");
- return((char **) NULL);
- }
- p=text;
- for (i=0; i < lines; i++)
- {
- for (q=p; *q != '\0'; q++)
- if (*q == '\n')
- break;
- textlist[i]=(char *) malloc((q-p+1)*sizeof(char));
- if (textlist[i] == (char *) NULL)
- {
- Warning("Unable to convert text","Memory allocation failed");
- return((char **) NULL);
- }
- (void) strncpy(textlist[i],p,q-p);
- textlist[i][q-p]='\0';
- p=q+1;
- }
- textlist[i]=(char *) NULL;
- return(textlist);
- }
-