Example #1
0
/*
extract a specified directory path given the level
NOTE: the level can be positive or negative
*/
char* extractDirectory(char* path, int level)
{
	/* length of path string */
	int length = strlen(path);
	
	/* current directory level */
	int currentLevel = 0;
	
	/* used to store directory separator index */
	int targetDirectorySeparatorIndex = 0;
	
	/* iterator */
	int i;
	
	/* iterate through the directory levels */
	for (i=0; i<abs(level); i++)
	{
		/* negative level */
		if (level < 0)
		{
			targetDirectorySeparatorIndex = lastIndexOf(path,'\\');
			path = substring(path, 0, targetDirectorySeparatorIndex);
		}
		
		/* positive level */
		else
		{
			/* first iteration - special case */
			if (targetDirectorySeparatorIndex == 0)
			{
				targetDirectorySeparatorIndex = indexOfWithStart(path,'\\', targetDirectorySeparatorIndex);
			}
			
			/* all other iterations - must add 1*/
			else
			{
				targetDirectorySeparatorIndex = indexOfWithStart(path,'\\', targetDirectorySeparatorIndex + 1);
			}
		}
	}
	
	/* if a positive level, must extract the directory string now */
	if (level > 0)
	{
		path = substring(path, 0, targetDirectorySeparatorIndex);
	}
	
	return path;
}
Example #2
0
int occuranceOf(char *string, char *delimiter){
	int index, start;
	int delimiterCount=0;
	start = 0;
	while (1){
		index = indexOfWithStart(string, delimiter, start);
		if (index != -1){
			delimiterCount ++;
		} else {
			break;
		}
		start = index + 1;
	}
	return delimiterCount;
}
Example #3
0
char *replace(char *string, char *replaceFor, char *replaceWith){
	int occurance;
	int mallocLength = 0, lengthString = 0, lengthReplaceFor = 0, lengthReplaceWith=0;
	char *retVal;
	int start, index, count, prevCount;
	int x;

	occurance = occuranceOf(string, replaceFor);
	lengthString = length(string);
	lengthReplaceFor = length(replaceFor);
	lengthReplaceWith = length(replaceWith);
	mallocLength = lengthString - lengthReplaceFor * occurance + lengthReplaceWith * occurance;
	retVal = (char*) malloc(mallocLength+1);

	start = 0;
	prevCount = 0;
	count = 0;
	while (1){
		index = indexOfWithStart(string, replaceFor, start);
		if (index != -1){
			for (x = 0; x<index - prevCount; x++){
				retVal[count] = string[x+prevCount];
				count++;
			}
			for (x=0; x<lengthReplaceWith; x++){
				retVal[count] = replaceWith[x];
				count++;
			}
			prevCount = index + lengthReplaceFor;
		} else {
			break;
		}
		start = index + lengthReplaceFor;
	}
	char *temp = subString(string, prevCount, length(string)-prevCount);
	for (x=0; x<length(temp); x++){
		retVal[count] = temp[x];
		count++;
	}
	free(temp);
	retVal[count] = '\0';
	return retVal;
}