1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
#include "Filename.h"
inline char* myStrCpy(char* caBuf, const char* str, int iBufSize)
{
int iBufSizeMinus1 = iBufSize - 1;
char* returnV = strncpy(caBuf, str, iBufSizeMinus1);
caBuf[iBufSizeMinus1] = '\0';
return(returnV);
}
/*
* Get the filename from the next line of a file list.
* If there are no more files, return "\0"
*/
bool GetNextFilenameFromListFile(ifstream &ifile, char* filenameBuffer)
{
// test file object to see if the filename we get is correct
ifstream testfile;
if (ifile.good()) {
do {
ifile.getline(filenameBuffer, MAX_FILE_PATH);
if (filenameBuffer[0] != '\0' && filenameBuffer[0] != EOF) {
if (filenameBuffer[0] == '#') // # follows by comments
continue;
testfile.open(filenameBuffer);
if (!testfile.good()) {
cout << filenameBuffer << " doesn't exist or permission deny!!" << endl;
testfile.close();
continue; //get next file name
} else {
testfile.close();
break;
}
} else if (ifile.eof()) {
ifile.close();
filenameBuffer[0] = '\0';
return(false);
}
} while (strcmp(filenameBuffer, "") == 0 || filenameBuffer[0] == '#');
} else {
return(false);
}
return(true);
}
bool GetNextFilenamePairFromListFile(ifstream &ifile, char* filenameBuffer1, char* filenameBuffer2)
{
ifstream testfile1;
ifstream testfile2;
bool getValidFilePair = false;
filenameBuffer1[0] = '\0';
filenameBuffer2[0] = '\0';
if (ifile.good()) {
do {
char filenameBuffer[FILENAME_MAX];
ifile.getline(filenameBuffer, MAX_FILE_PATH);
if (filenameBuffer[0] != '\0' && filenameBuffer[0] != EOF) {
if (filenameBuffer[0] != '#') { // Not a comment
char* pch = strtok(filenameBuffer, "\t, ");
if (pch != NULL)
strcpy(filenameBuffer1, pch);
pch = strtok(NULL, "\t, ");
if (pch != NULL)
strcpy(filenameBuffer2, pch);
if (filenameBuffer1[0] != '\0' && filenameBuffer2[0] != '\0') {
if (!fileExist(filenameBuffer1)) {
cout << filenameBuffer1 << " doesn't exist !!" << endl;
filenameBuffer1[0] = '\0';
} else if (!fileExist(filenameBuffer2)) {
cout << filenameBuffer2 << " doesn't exist !!" << endl;
filenameBuffer2[0] = '\0';
} else {
getValidFilePair = true;
}
}
}
}
if (ifile.eof()) {
ifile.close();
break;
}
} while (!getValidFilePair);
} else {
cout << "Can't get new file" << endl;
}
return(getValidFilePair);
}
bool GetNextFilenamePairFromListFile(const char* filename, char* filenameBuffer1, char* filenameBuffer2) {
ifstream readsFileList(filename);
if(readsFileList.bad()) {
return(false);
}
bool bMatePairedReads = GetNextFilenamePairFromListFile(readsFileList, filenameBuffer1, filenameBuffer2);
readsFileList.close();
return(bMatePairedReads);
}
int chExtName(char* filename, const char* Extname)
{
int i = 0;
//Give Ext name starts with '.'
if (filename != NULL && Extname != NULL) {
for (i = (int)strlen(filename); i > 0; i--) {
if (filename[i] == '.')
break;
}
if (i > 0)
strcpy(&(filename[i]), Extname);
else {
//no extended filename
int l = (int)strlen(filename);
strcpy(&(filename[l]), Extname);
//concatenate the Extended file at the end
}
return((int)strlen(filename));
}
return(-1);
}
string chExtName(string filename, string Extname)
{
char fileNameBuf[FILENAME_MAX];
myStrCpy(fileNameBuf, filename.c_str(), FILENAME_MAX);
chExtName(fileNameBuf, Extname.c_str());
return(string(fileNameBuf));
}
char* addPath(const char* filename, const char* directory, char* path)
{
char tmpbuffer[MAX_FILE_PATH];
myStrCpy(tmpbuffer, filename, MAX_FILE_PATH);
//in case path and filename or the same buffer
sprintf(path, "%s//%s", directory, tmpbuffer);
return(path);
}
string getFullPath(string directory, string filename)
{
#if defined WIN32 || defined WIN64
string fullPath = directory.append("\\").append(filename);
#else
string fullPath = directory.append("/").append(filename);
#endif
return(fullPath);
}
// return the pointer to the start of a file name, given a path
const char* getPtrBasename(const char* Path)
{
int i = 0;
for (i = (int)strlen(Path) - 1; i >= 0; i--) {
char c = Path[i];
if (isspace(c) ||
iscntrl(c) ||\
(c == '<') ||\
(c == '>') ||\
(c == ':') ||\
(c == '"') ||\
(c == '|') ||\
(c == '?') ||\
(c == '*') ||\
(c == '%') ||\
(c == '/') ||\
(c == '\\'))
{
break; // if not alpha, digit, '_', '.' or space
}
}
return(&Path[i + 1]);
}
// get the file name from the path without extended file name like .txt
int getBasename(const char* Path, char* fileNameWithoutExt)
{
// i and j are the start and end of the filename in the path.
int j = 0;
if (Path != NULL && fileNameWithoutExt != NULL) {
const char* fileName = getPtrBasename(Path);
for (j = (int)strlen(fileName); j >= 0; j--) {
if (fileName[j] == '.')
break;
} // get the start position of ext file
strcpy(fileNameWithoutExt, fileName);
// if there is no ext name
if (j > 0) {
fileNameWithoutExt[j] = '\0';
}
}
return((int)strlen(fileNameWithoutExt));
}
string getBasename(const char* Path)
{
char fileNameWithoutExt[FILENAME_MAX];
getBasename(Path, fileNameWithoutExt);
return(string(fileNameWithoutExt));
}
//Get the name from the path and store in Title
//removed the number at the end of the Filename
int getTitleFromPath(char* Path, char* Title)
{
int i = 0;
getBasename(Path, Title);
for (i = (int)strlen(Title); i > 0; i--) {
if (Title[i] == '_') {
if (atoi(&(Title[i+1])) > 0) {
//remove the '_' and every number after it
for (; Title[i] == '_'; i--)
Title[i] = '\0';
}
return(0);
}
}
return(0);
}
const char* getExtName(const char* filename)
{
// Return the char pointer point to the postfix of the char array
// Ex: given a.b.txt return const char* ".txt"
// Ex: given abc return const char* "abc"
int i = 0;
for (i = (int)strlen(filename); i > 0; i--) {
if (filename[i] == '.')
break;
}
return(&(filename[i]));
}
bool hasTheExtName(const char* filename, const char* extName)
{
const char* realExtName = getExtName(filename);
int i;
for (i = 0; extName[i] != '\0' && realExtName[i] != '\0'; i++) {
if (tolower(extName[i]) != tolower(realExtName[i]))
return(false);
}
if (i == 0) { //No ExtName
return (false);
} else {
return(extName[i] == realExtName[i]); // one may be the prefix of the other
}
}
bool fileExist(const char* filename)
{
if (filename != NULL && filename[0] != 0) {
ifstream iFile;
iFile.open(filename, ifstream::in);
// iFile.close();
if (iFile.fail()) {
return(false); // not exist
} else {
iFile.close();
return (true); //exist;
}
}
return(false); // wrong file name
}
bool checkFileExist(const char* filename)
{
if (!fileExist(filename)) {
cout << filename << " dosent exist.\n" << endl;
return(false);
} else {
return(true);
}
}
void filenameLize(char* string)
{
for (int i = 0; string[i] != '\0'; i++) {
char c = string[i];
if (!(isalnum(c) || c == '.')) {
if (i == 0) { // If the first character is non_valid, replace to 'Z'
strcpy(string, "NULL_Filename");
} else { // Truncated the string if a space, tab or comma
if (c == ' ' || c == '\t' || c == ',' || c == '\n' || c == '\r') {
string[i] = '\0';
break;
} else { // else replace to '_'
string[i] = '_';
}
}
}
}
}
bool isPathWritable(const char* filename)
{
FILE *fp = NULL;
fp = fopen(filename, "w");
if (fp == NULL) {
return false;
} else {
fclose(fp);
return true;
}
}
bool checkPathCharsAreValid(const char* pathStr)
{
// null path will return true
for (int i = 0; pathStr[i] != '\0'; i++) {
char c = pathStr[i];
if (isspace(c) || iscntrl(c)) {
return(false);
}
switch (c) {
case '&':
case '"':
case '*':
case '?':
case ':':
case '>':
case '<':
case '\'':
return(false);
default:
break;
}
}
return(true);
}
bool dirExist(const char* strFolderPath)
{
if ( fileExist(strFolderPath)) {
struct stat status;
stat( strFolderPath, &status );
if ( status.st_mode & S_IFDIR ) {
return(true);
}
}
return(false);
}
|