Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

file.c File Reference

#include "tk.h"
#include "all.h"
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <time.h>
#include <string.h>
#include <dirent.h>
#include <strings.h>

Go to the source code of this file.


Defines

#define MAX_FILES   1000
#define MAX_NAME_LEN   256
#define MAX_STRING_LEN   MAX_NAME_LEN
#define TRUE   1
#define FALSE   0

Typedefs

typedef int boolean

Functions

void ResetPath (void)
int ListDirectory (ClientData nulldata, Tcl_Interp *interp, int argc, char **argv)
int ChangeDirectory (ClientData nulldata, Tcl_Interp *interp, int argc, char **argv)
void SortFiles (int numStrings, char strings[MAX_FILES][MAX_NAME_LEN], boolean *dirList, int permute[])
void UpdatePath (Tcl_Interp *interp, char *directory)
boolean MatchesGlob (char *string, char *glob)
int SetBrowseGlob (ClientData nulldata, Tcl_Interp *interp, int argc, char **argv)

Variables

char currentPath [MAXPATHLEN]
char globString [1024]
DIR * dfd

Define Documentation

#define FALSE   0
 

Definition at line 63 of file file.c.

Referenced by ListDirectory(), and MatchesGlob().

#define MAX_FILES   1000
 

Definition at line 57 of file file.c.

Referenced by ListDirectory(), and SortFiles().

#define MAX_NAME_LEN   256
 

Definition at line 58 of file file.c.

Referenced by ListDirectory(), and SortFiles().

#define MAX_STRING_LEN   MAX_NAME_LEN
 

Definition at line 59 of file file.c.

#define TRUE   1
 

Definition at line 62 of file file.c.

Referenced by ListDirectory(), and MatchesGlob().


Typedef Documentation

typedef int boolean
 

Definition at line 61 of file file.c.


Function Documentation

int ChangeDirectory (  ClientData    nulldata,
Tcl_Interp *    interp,
int    argc,
char **    argv
) 
 

Definition at line 135 of file file.c.

References argc, currentPath, dfd, and UpdatePath().

00137 {
00138     char *directory = argv[1];
00139 
00140     UpdatePath(interp, directory);
00141 
00142     fprintf(stdout, "Opening directory: '%s'\n", currentPath);
00143 
00144     dfd = opendir(currentPath);
00145     if ( dfd == NULL )
00146     {
00147         fprintf(stderr, "can't open '%s'\n", currentPath);
00148         return TCL_OK;  /* shouldn't, really */
00149     }
00150 
00151     return TCL_OK;
00152 }

int ListDirectory (  ClientData    nulldata,
Tcl_Interp *    interp,
int    argc,
char **    argv
) 
 

Definition at line 155 of file file.c.

References argc, currentPath, dfd, FALSE, fileName, globString, MatchesGlob(), MAX_FILES, MAX_NAME_LEN, MAXPATHLEN, S_ISDIR, SortFiles(), stbuf, and TRUE.

00157 {
00158     struct dirent *dp;
00159     struct stat stbuf;
00160     char command[256];
00161     char fileName[MAX_FILES][MAX_NAME_LEN];
00162     boolean dirList[MAX_FILES];
00163     int permute[MAX_FILES];
00164     int fileCount = 0;
00165     register int index;
00166     char fullName[MAXPATHLEN];
00167     char    *restPtr;
00168 
00169     sprintf(command, "ShowCurrentDirectory %s", currentPath);
00170     Tcl_Eval(interp, command, 0, (char **) NULL);
00171 
00172     if ( dfd == NULL )
00173     {
00174         fprintf(stderr, "TRIED TO LIST NULL DIRECTORY\n");
00175 
00176         return TCL_OK;
00177     }
00178 
00179 /* check if root directory */
00180     if ( strlen(currentPath) != 1 )
00181     {
00182         sprintf(fileName[fileCount], "../");
00183         dirList[fileCount] = TRUE;
00184         fileCount++;
00185     }
00186 
00187     strcpy(fullName, currentPath);
00188     restPtr = &fullName[strlen(fullName)];
00189 
00190     while ( (dp = readdir(dfd)) != NULL )
00191     {
00192         strcpy(restPtr, dp->d_name);
00193         stat(fullName, &stbuf);
00194 
00195         if ( dp->d_name[0] != '.' )
00196         {
00197             if ( S_ISDIR(stbuf.st_mode) )
00198             {
00199                 sprintf(fileName[fileCount], "%s/", dp->d_name);
00200                 dirList[fileCount] = TRUE;
00201                 fileCount++;
00202             }
00203             else
00204             {
00205                 if ( MatchesGlob(dp->d_name, globString) )
00206                 {
00207                     strcpy(fileName[fileCount], dp->d_name);
00208                     dirList[fileCount] = FALSE;
00209                     fileCount++;
00210                 }
00211             }
00212         }
00213     }
00214 
00215     SortFiles(fileCount, fileName, dirList, permute);
00216 
00217     for ( index = 0; index < fileCount; index++ )
00218     {
00219         sprintf(command, "AddBrowseFile %s", fileName[permute[index]]);
00220         Tcl_Eval(interp, command, 0, (char **) NULL);
00221     }
00222 
00223     closedir(dfd);
00224 
00225     return TCL_OK;
00226 }

boolean MatchesGlob (  char *    string,
char *    glob
)  [static]
 

Definition at line 306 of file file.c.

References FALSE, and TRUE.

Referenced by ListDirectory().

00307 {
00308     char    *stringRight, *globRight;
00309 
00310     while ( (*glob != '\0') && (*glob != '*') )     /* match left side */
00311     {
00312         if ( (*string == '\0') || (*string != *glob) )
00313             return FALSE;
00314         string++;
00315         glob++;
00316     }
00317 
00318     if ( *glob == '\0' )        /* no star */
00319         return TRUE;
00320 
00321     /* now match right side */
00322     stringRight = &string[strlen(string)-1];
00323     globRight = &glob[strlen(glob)-1];
00324 
00325     while ( *globRight != '*' )
00326     {
00327         if ( (stringRight < string) || (*stringRight != *globRight) )
00328             return FALSE;
00329         globRight--;
00330         stringRight--;
00331     }
00332 
00333     return TRUE;
00334 }

void ResetPath (  void    ) 
 

Definition at line 84 of file file.c.

References currentPath, and dfd.

00085 {
00086     if ( getwd(currentPath) == 0 )
00087     {
00088         fprintf(stderr, "Error getting pathname!!!\n");
00089         exit(1);
00090     }
00091 
00092     strcpy(&currentPath[strlen(currentPath)], "/");
00093 
00094     dfd = opendir(currentPath);
00095     if ( dfd == NULL )
00096     {
00097         fprintf(stderr, "can't open '%s'\n", currentPath);
00098         exit(1);
00099     }
00100 }

int SetBrowseGlob (  ClientData    nulldata,
Tcl_Interp *    interp,
int    argc,
char **    argv
) 
 

Definition at line 288 of file file.c.

References argc, and globString.

00290 {
00291     if (argc == 2 )
00292     {
00293         strcpy(globString, argv[1]);
00294 
00295         fprintf(stdout, "GLOB:  %s\n", globString);
00296 
00297         return TCL_OK;
00298     }
00299 
00300         Tcl_AppendResult (interp, 
00301             "wrong args: should be \"", argv[0]," string\"", (char *) NULL);
00302         return TCL_ERROR;
00303 }

void SortFiles (  int    numStrings,
char    strings[MAX_FILES][MAX_NAME_LEN],
boolean *    dirList,
int    permute[]
) 
 

Definition at line 229 of file file.c.

References i, MAX_FILES, and MAX_NAME_LEN.

Referenced by ListDirectory().

00231 {
00232     register int i, j;
00233     int temp;
00234     int numDirs;
00235     int ptr;
00236 
00237     for ( i = 0; i < numStrings; i++ )
00238         permute[i] = i;
00239 
00240     /* put all directories at front */
00241     numDirs = 0;
00242     ptr = numStrings-1;
00243     while ( numDirs != ptr )
00244     {
00245         /* go past dirs */
00246         while ( (numDirs < ptr) && (dirList[permute[numDirs]]) )
00247             numDirs++;
00248 
00249         /* go past non-dirs */
00250         while ( (numDirs < ptr) && (! dirList[permute[ptr]]) )
00251             ptr--;
00252 
00253         if ( numDirs != ptr )
00254         {
00255             temp = permute[numDirs];
00256             permute[numDirs] = ptr;
00257             permute[ptr] = temp;
00258         }
00259     }
00260 
00261     if ( dirList[permute[numDirs]] )
00262         numDirs++;
00263 
00264     for ( i = 0; i < numDirs; i++ )
00265         for ( j = i+1; j < numDirs; j++ )
00266         {
00267             if ( strcmp(&strings[permute[j]][0], &strings[permute[i]][0]) < 0 )
00268             {
00269                 temp = permute[j];
00270                 permute[j] = permute[i];
00271                 permute[i] = temp;
00272             }
00273         }
00274 
00275     for ( i = numDirs; i < numStrings; i++ )
00276         for ( j = i+1; j < numStrings; j++ )
00277         {
00278             if ( strcmp(&strings[permute[j]][0], &strings[permute[i]][0]) < 0 )
00279             {
00280                 temp = permute[j];
00281                 permute[j] = permute[i];
00282                 permute[i] = temp;
00283             }
00284         }
00285 }

void UpdatePath (  Tcl_Interp *    interp,
char *    directory
)  [static]
 

Definition at line 103 of file file.c.

References currentPath.

Referenced by ChangeDirectory().

00104 {
00105     int length;
00106     char *charPtr;
00107 
00108     length = strlen(currentPath);
00109 
00110     if ( strcmp(directory, "./") == 0 )
00111         return /* nothing */ ;
00112     else if ( strcmp(directory, "../") == 0 )
00113     {
00114         /* delete backwards up to '/' */
00115 
00116         if ( length < 2 )
00117         {
00118             fprintf(stderr, "Error:  backing up from root directory!!!\n");
00119             exit(1);
00120         }
00121 
00122         charPtr = &currentPath[length-2];
00123         while ( (charPtr != currentPath) && (*charPtr != '/') )
00124             charPtr--;
00125         charPtr++;      /* leave the '/' */
00126         *charPtr = '\0';
00127     }
00128     else
00129     {
00130         strcpy(&currentPath[length], directory);
00131     }
00132 }

Variable Documentation

char currentPath[MAXPATHLEN]
 

Definition at line 65 of file file.c.

Referenced by ChangeDirectory(), ListDirectory(), ResetPath(), and UpdatePath().

DIR* dfd [static]
 

Definition at line 69 of file file.c.

Referenced by ChangeDirectory(), ListDirectory(), and ResetPath().

char globString[1024]
 

Definition at line 67 of file file.c.

Referenced by ListDirectory(), and SetBrowseGlob().

 

Powered by Plone

This site conforms to the following standards: