TarzaNN
TarzaNN neural network simulator
|
00001 /**************************************************************************** 00002 ** 00003 ** Copyright C 2002-2012 Laboratory for Active and Attentive Vision (LAAV), Department of Computer Science and Engineering, York University, Toronto, ON, Canada. 00004 ** All rights reserved. 00005 ** 00006 ** This file is part of the TarzaNN Neural Network Simulator. 00007 ** 00008 ** This file may be distributed and/or modified under the terms of the 00009 ** GNU General Public License version 2 as published by the Free Software 00010 ** Foundation and appearing in the file LICENSE.GPL included in the 00011 ** packaging of this file. 00012 ** 00013 ** See http://www.tarzann.org/gpl/ for GPL licensing information. 00014 ** 00015 ** Contact info@tarzann.org if any conditions of this licensing are 00016 ** not clear to you. 00017 ** 00018 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 00019 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 00020 ** 00021 ****************************************************************************/ 00022 00023 #ifndef DIRECTORY_H 00024 #define DIRECTORY_H 00025 00027 struct DirectoryEntry { 00028 char *name; 00029 int type; 00030 00031 DirectoryEntry() { 00032 name = NULL; 00033 type = 0; 00034 } 00035 ~DirectoryEntry() { 00036 delete[] name; 00037 } 00038 }; 00039 00044 00045 class Directory { 00046 public: 00047 // flags 00048 static const int F_HIDDEN; // show hidden files 00049 00050 // types 00051 static const int T_FILE; // regular file 00052 static const int T_DIR; // a directory 00053 static const int T_OTHER; // not a file or a directory 00054 00055 static int pathType(char *path); 00056 00057 Directory(char *path = NULL, int flags = 0); 00058 ~Directory(); 00059 00061 int size(); 00062 00064 char *getPath(); 00065 00067 00073 bool setPath(char *path); 00074 00076 int getFlags(); 00077 00079 00083 bool setFlags(int flags); 00084 00086 00090 char *getName(int i); 00091 00093 00097 char *getFullPath(int i); 00098 00100 int getType(int i); 00101 00102 #ifdef _WIN32 00103 #define RECOG_SEP "/\\" 00104 #define USE_SEP '\\' 00105 #else 00106 #define RECOG_SEP "/" // separators we recognize 00107 #define USE_SEP '/' // separator we will actually use 00108 #endif 00109 00110 // no allocation 00111 static char *get_basename(char *path) { 00112 char *x; 00113 x = path + strlen(path) - 1; 00114 // Skip tailing path separators... 00115 while (x >= path && strchr(RECOG_SEP,*x)) 00116 x--; 00117 if (x < path) 00118 return path; 00119 while (x >= path && !strchr(RECOG_SEP,*x)) 00120 x--; 00121 if (x < path) 00122 return path; 00123 return x+1; 00124 } 00125 00126 // allocation 00127 static char *get_dirname(char *path) { 00128 char *x, *work; 00129 x = path + strlen(path) - 1; 00130 // Remove tailing path separators... 00131 while (x > path && strchr(RECOG_SEP,*x)) 00132 x--; 00133 if (x <= path) { 00134 char *p = new char[2]; 00135 /* "root" directory */ 00136 p[0] = USE_SEP; 00137 p[1] = '\0'; 00138 return p; 00139 } 00140 // Create copy to work with 00141 work = copystr(path); 00142 work[x-path+1] = '\0'; 00143 x = work + strlen(work) - 1; 00144 while (x >= work && !strchr(RECOG_SEP,*x)) 00145 x--; 00146 if (x < work) { 00147 // no directory 00148 delete work; 00149 const char* s1 = "."; 00150 return copystr(s1); 00151 } 00152 00153 while (x >= work && strchr(RECOG_SEP,*x)) 00154 x--; 00155 x++; 00156 if (x == work) { 00157 // root directory 00158 delete work; 00159 char *p = new char[2]; 00160 p[0] = USE_SEP; 00161 p[1] = '\0'; 00162 return p; 00163 } else { 00164 *x = '\0'; 00165 return work; 00166 } 00167 } 00168 00169 static int matches_sequence(char *file, char *prefix, char *suffix, 00170 long *seqno_r) { 00171 int np = strlen(prefix), ns = strlen(suffix), nf = strlen(file); 00172 if (nf <= np+ns) 00173 return 0; /* file name is too short */ 00174 if (strncmp(file,prefix,np)) 00175 return 0; /* nope - prefix doesn't match */ 00176 char *x = file + strlen(file) - ns; 00177 if (strcmp(x,suffix)) 00178 return 0; /* nope - suffix doesn't match */ 00179 char *buf = new char[x-file-np+1]; 00180 memcpy(buf,file+np,x-file-np); 00181 buf[x-file-np] = '\0'; 00182 unsigned long seqno; 00183 char *chk; 00184 seqno = strtol(buf,&chk,10); 00185 if (*chk != '\0') { 00186 delete[] buf; 00187 return 0; /* not a number */ 00188 } 00189 if (seqno_r) 00190 *seqno_r = seqno; 00191 delete[] buf; 00192 return 1; /* match */ 00193 } 00194 static char *copystr(const char *s) { 00195 if (!s) 00196 return NULL; 00197 char *s2 = new char[strlen(s)+1]; 00198 if (!s2) 00199 abort(); 00200 strcpy(s2,s); 00201 return s2; 00202 } 00203 00204 private: 00205 void clear(); 00206 void append(char *name, int type); 00207 00208 #ifdef WIN32 00209 void fix_dir_separators(char* path); 00210 #endif 00211 00212 char *path; 00213 int flags; 00214 DirectoryEntry *entries; 00215 int e_spc, e_use; 00216 00217 }; 00218 #endif /* DIRECTORY_H */