unsigned int findFileBlockInPath(char *fileName) { char * path; char * directoryName; int tokens,directoryEntryNumber; unsigned int directoryBlockNumber; directoryBlock directory; int tokenPosition = 2; fileName = getData(fileName); path = (char*)mallocFS(mystrlen(fileName)); path = getData(strcpy(fileName, path)); tokens= qtytoken(path, '/'); directoryName = getData(mystrtok(path, '/', tokenPosition)); directoryEntryNumber = 0; directoryBlockNumber = ROOTBLOCK; readsector(2, &directory); if (directoryName[0] == '\0') return directoryBlockNumber; while (tokenPosition <= tokens) { directoryEntryNumber = findFileInDirectory(directory, directoryName); if (directoryEntryNumber == -1) { return -1; } directoryBlockNumber = directory.entries[directoryEntryNumber].fileBlockStart; readsector(directoryBlockNumber, (char *)&directory); directoryName = getData(mystrtok(path, '/', tokenPosition + 1)); tokenPosition++; } return directoryBlockNumber; }
char *mystrtok(char * str, char delim, int pos) { int tokenPosition = 0; int totalTokens = qtytoken(str, delim), count = 1, posStr = 0; int len = stlen(str, posStr, delim); char * token = (char*)mallocFS(len); if (len == 1) return str; while (str[posStr] != '\0') { if (str[posStr] == delim) { count++; if (count < pos)token = (char*)mallocFS(len); } else if (count == pos){ token[tokenPosition] = str[posStr]; tokenPosition++; } posStr++; } return token; }
char * Utilities::strtok(char * str, char delim, int pos) { int totalTokens = qtytoken(str,delim), count = 1, posStr = 0, posToken = 0; int len = stlen(str, posStr, delim); char * token; if(len==0) return '\0'; //token = (char*)mallocFS(len); token = new char[len]; while(str[posStr]!='\0') { if(str[posStr]==delim) { int len = stlen(str, posStr, delim); count++; //token =((count<pos)) ? (char*)mallocFS(len) : token; if(count<pos)token = new char[len]; } else if(count==pos){ token[posToken] = str[posStr]; posToken++; } posStr++; } return token; }
char *Utilities::mystrtok(char * str, char delim, int pos) { int totalTokens = qtytoken(str,delim), count = 1, posStr = 0; int len = stlen(str, posStr, delim); char * token = new char[len]; if(len == 1) return str; int posToken = 0; while(str[posStr]!='\0') { if(str[posStr]==delim) { count++; //token =((count<pos)) ? (char*)malloc(stlen(str, posStr, delim)) : token; if(count<pos)token = new char[len]; } else if(count==pos){ token[posToken] = str[posStr]; posToken++; } posStr++; } //printstring(token); return token; }
char * getPathName(char * pathName, int type) { int lengthName = mystrlen(pathName); char *path = (char*)mallocFS(lengthName); int tokens = qtytoken(pathName, '/'); int tokenPosition = 2; int i = 0; char *dirName = (char*)mallocFS(lengthName); if (tokens == 1 && type == 1) { path[0] = pathName[0]; path[1] = '\0'; } if (tokens == 2) { path[0] = '/'; path = getData(path); } while (tokenPosition <= tokens - 1) { char *token = mystrtok(pathName, '/', tokenPosition); if (tokenPosition == 2) path = getData(mistrcat("/", token)); else path = getData(mistrcat(path, token)); if (tokenPosition < tokens - 1) { path = getData(path); path = mistrcat(path, "/"); path = getData(path); } tokenPosition++; } if (type == 1)//retorna path sin la ultima carpeta o archivo { return path; } else { dirName = mystrtok(pathName, '/', tokens); dirName = getData(dirName); return dirName; } }
char * strtok(char * str, char delim, int pos) { int totalTokens = qtytoken(str,delim), count = 1, posStr = 0; char * token = (char*)malloc(stlen(str, posStr, delim)); int posToken = 0; while(str[posStr]!='\0') { if(str[posStr]==delim) { int len = stlen(str, posStr, delim); count++; token =((count<pos)) ? (char*)malloc(stlen(str, posStr, delim)) : token; } else if(count==pos){ token[posToken] = str[posStr]; posToken++; } posStr++; } return token; }