Beispiel #1
0
/** Returns a string consisting of a copy of the file part of path.  
*** Does not change path.  Backslashes are converted into forward slashes.
**/
char *abasename (const char *path) {
	char *result = NULL;
	char *tmp    = NULL;
	size_t ii;
	
	if (strxnullorempty(path)) {
		result = astrcpy(".");
	}
	else {
		tmp = strxreplace(astrcpy(path), '\\', '/');
		if (strchr(tmp, '/') == NULL) {
			result = astrcpy(path);
		}
		else if (strxequals(tmp, "/") || 
			strxequals(tmp, ".") || 
			strxequals(tmp, "..")
		) {
			result = astrcpy(tmp);
		}
		else {
			if (tmp[strlen(tmp) - 1] == '/') tmp[strlen(tmp) - 1] = '\0';
			ii = strlen(tmp);
			while (ii >= 0) {
				if (tmp[ii] == '/') {
					result = astrmid(tmp, ii + 1, 0);
					break;
				}
				ii--;
			}
		}
	}
	mu_free(tmp);
	return result;
}
Beispiel #2
0
/** Returns a new string consisting of the rightmost count characters of the 
*** source string.  Equivalent to BASIC Right$() function
**/
char *astrright(const char *src, size_t count){
	if (count >= strlen(src)) {
		return astrcpy(src);
	}
	else{
		return astrcpy(&src[strlen(src) - count]);
	}
}
Beispiel #3
0
/** Removes "." and ".." from a path */
char *getCanonicalPath(const char *path){
	char   *prefix = NULL;
	char   *rest   = NULL;
	char   *tmp    = NULL;
	size_t offset  = 0;
	char   **src   = NULL;
	List   *dst    = NULL;
	size_t ii = 0;
	Buffer *buf    = NULL;
	char   *result = NULL;
	
	if (path != NULL && strlen(path) > 0) {
		tmp = strxreplace(astrcpy(path), '\\', '/');
		if (isDosPath(tmp) || isUncPath(tmp)) {
			if (isDosPath(tmp)) {
				prefix = getPathPart(tmp, PATH_DRIVE);
				offset = 0;
			}
			else if (isUncPath(tmp)) {
				prefix = getPathPart(tmp, PATH_HOST);
				offset = 2;
			}
			rest = astrcpy(strchr(&tmp[offset], '/'));
		}
		else {
			rest = astrcpy(tmp);
		}
		src = astrtok(rest, "/");
		dst = new_List();
		while (src[ii] != NULL) {
			if (strxequals(src[ii], "..")) {
				List_remove(dst, -1, false);
			}
			else if (!strxequals(src[ii], ".")) {
				List_append(dst, src[ii]);
			}
			ii++;
		}
		buf = new_Buffer(0);
		if (prefix != NULL) {
			Buffer_appendString(buf, prefix);
		}
		for (ii = 0; ii < List_length(dst); ++ii) {
			Buffer_appendString(buf, List_get(dst, ii));
			if (ii != (List_length(dst) - 1)) {
				Buffer_appendChar(buf, '/');
			}
		}
		result = astrcpy(buf->data);
		delete_Buffer(buf);
		delete_List(dst, false);
		astrtokfree(src);
		mu_free(prefix);
		mu_free(rest);
		mu_free(tmp);
	}
	return result;
}
Beispiel #4
0
/** Joins two paths together, allowing for relative/absolute paths */
char *getCombinedPath(const char *path1, const char *path2) {
	char   *tmp1   = NULL;
	char   *tmp2   = NULL;
	char   *tmp    = NULL;
	Buffer *buf    = NULL;
	char   *result = NULL;
	char   *final  = NULL;
	
	if (!strxnullorempty(path1) && !strxnullorempty(path2)) {
		tmp1 = strxreplace(astrcpy(path1), '\\', '/');
		tmp2 = strxreplace(astrcpy(path2), '\\', '/');
		if (isDosPath(tmp2) || isUncPath(tmp2)) {
			/* Path2 is a fully-qualified DOS/Windows path; return it */
			result = astrcpy(tmp2);
		}
		else {
			buf = new_Buffer(0);
			if (tmp2[0] == '/') {
				/* Path2 is an absolute path.  If Path1 is a DOS/Windows or 
				** UNC path, prepend the drive letter or hostname; otherwise 
				** return it.
				*/
				if (isDosPath(tmp1)) {
					tmp = getPathPart(tmp1, PATH_DRIVE);
					Buffer_appendString(buf, tmp);
					Buffer_appendString(buf, tmp2);
					mu_free(tmp);
				}
				else if (isUncPath(tmp1)) {
					tmp = getPathPart(tmp1, PATH_HOST);
					Buffer_appendString(buf, "//");
					Buffer_appendString(buf, tmp);
					Buffer_appendString(buf, tmp2);
					mu_free(tmp);
				}
				else {
					Buffer_appendString(buf, tmp2);
				}
			}
			else {
				/* Simply concatenate the two paths */
				Buffer_appendString(buf, tmp1);
				if (tmp1[strlen(tmp1) - 1] != '/') {
					Buffer_appendChar(buf, '/');
				}
				Buffer_appendString(buf, tmp2);
			}
			result = astrcpy(buf->data);
			delete_Buffer(buf);
		}
		mu_free(tmp1);
		mu_free(tmp2);
	}
	/* Remove any "." and ".." in the path */
	final = getCanonicalPath(result);
Beispiel #5
0
char *astrright(const char *src, size_t count){
	/* Returns a new string consisting of the rightmost count         */
	/* characters of the source string.                               */
	static const char fnName[] __attribute__((unused)) = "astrright()";
	char   *rightString = NULL;
	
	if(count >= strlen(src)){
		return astrcpy(src);
	}
	else{
		return astrcpy(&src[strlen(src) - count]);
	}
	
	return rightString;
}
Beispiel #6
0
char *astrleft(const char *src, size_t count){
	/* Returns a new string consisting of the leftmost count          */
	/* characters of the source string.                               */
	static const char fnName[] __attribute__((unused)) = "astrleft()";
	char   *leftString = NULL;
	size_t actualCount;
	
	if(count == 0){
		return NULL;
	}
	else if(count >= strlen(src)){
		return astrcpy(src);
	}
	else{
		if((leftString = (char *)malloc((count + 1) * sizeof(char))) == NULL){
			fatal(fnName, "Out of memory!");
		}
		else{
			if(count >= strlen(src)){
				actualCount = strlen(src);
			}
			else{
				actualCount = count;
			}
			memcpy(leftString, src, actualCount);
			leftString[actualCount] = '\0';
		}
	}
	return leftString;
}
Beispiel #7
0
int main(void) {
  char *container = NULL;
  char text[] = "Once upon a time in a dark night something happened";
  char other_text[] = " and happened some more";
  astrcpy(&container, text);
  printf("Copied: %s\n", container);
  astrcat(&container, other_text);
  printf("Concatenated: %s\n", container);
  return 0;
}
Beispiel #8
0
/** Returns True if path is of the form //server/share, False otherwise */
bool isUncPath(const char *path) {
	char *tmp   = NULL;
	bool result = false;
	
	if (path != NULL && strlen(path) > 2) {
		tmp = strxreplace(astrcpy(path), '\\', '/');
		result = ((tmp[0] == '/') && (tmp[1] == '/') ? true : false);
		mu_free(tmp);
	}
	return result;
}
Beispiel #9
0
/** Returns a copy of string src up to a maximum length of n characters */
char *astrncpy(const char *src, size_t n) {
	char *result = NULL;
	
	if (strlen(src) <= n) {
		return astrcpy(src);
	}
	else {
		result = (char *)mu_malloc((n + 1) * sizeof(char));
		strncpy(result, src, n);
		return result;
	}
}
Beispiel #10
0
/** Returns a copy of the string with trailing whitespace removed */
char *astrrtrim(const char *s){
	char   *result = NULL;
	size_t ii;
	
	for (ii = strlen(s) - 1; ii >= 0; --ii) {
		if (!isspace(s[ii])) {
			result = astrleft(s, ii + 1);
			break;
		}
	}
	if(result == NULL) result = astrcpy("");
	return result;
}
Beispiel #11
0
/** Returns a copy of the string passed to it (equivalent to strdup() found in 
*** some standard libraries)
**/
char *astrcpy(const char *src){
	char *inputCopy = NULL;
	
	if (src == NULL){
		Logging_warnf("%s(): Trying to copy NULL string!", __FUNCTION__);
		inputCopy = astrcpy("");
	}
	else {
		inputCopy = (char *)mu_malloc((strlen(src) + 1) * sizeof(char));
		strcpy(inputCopy, src);
	}
	return inputCopy;
}
Beispiel #12
0
/** Returns a copy of the string with leading whitespace removed */
char *astrltrim(const char *s){
	char   *result = NULL;
	size_t ii;
	
	for (ii = 0; ii < strlen(s); ++ii) {
		if (!isspace(s[ii])) {
			result = astrmid(s, ii, 0);
			break;
		}
	}
	if(result == NULL) result = astrcpy("");
	return result;
}
Beispiel #13
0
/** Returns a new string consisting of the leftmost count characters of the 
*** source string; equivalent to BASIC Left$() function
**/
char *astrleft(const char *src, size_t count) {
	char   *leftString = NULL;
	size_t actualCount;
	
	if (count == 0) {
		return astrcpy("");
	}
	else if (count >= strlen(src)) {
		return astrcpy(src);
	}
	else {
		leftString = (char *)mu_malloc((count + 1) * sizeof(char));
		if (count >= strlen(src)) {
			actualCount = strlen(src);
		}
		else {
			actualCount = count;
		}
		memcpy(leftString, src, actualCount);
		leftString[actualCount] = '\0';
	}
	return leftString;
}
Beispiel #14
0
/** Returns a new string comprising of the two strings passed to it joined 
*** together
**/
char *astrcat(const char *src1, const char *src2) {
	char   *catString = NULL;
	size_t catLength;
	
	if (src1 == NULL && src2 == NULL) {
		Logging_warnf("%s(): Arguments are NULL!", __FUNCTION__);
		catString = astrcpy("");
	}
	else if (src1 == NULL && src2 != NULL) {
		Logging_warnf("%s(): First argument is NULL!", __FUNCTION__);
		catString = astrcpy(src2);
	}
	else if (src2 == NULL && src1 != NULL) {
		Logging_warnf("%s(): Second argument is NULL!", __FUNCTION__);
		catString = astrcpy(src1);
	}
	else {
		catLength = strlen(src1) + strlen(src2) + 1;
		catString = (char *)mu_malloc(catLength * sizeof(char));
		strcpy(catString, src1);
		strcat(catString, src2);
	}
	return catString;
}
Beispiel #15
0
/** Creates a empty section */
Section *new_Section(Section *parent, char *dir){
	Section *s = NULL;
	s = (Section *)mu_malloc(sizeof(Section));
	s->type = BILE_SECTION;
	s->parent = parent;
	if(parent == NULL)
		s->variables = new_Vars(NULL);
	else
		s->variables = new_Vars(parent->variables);
	s->dir = astrcpy(dir);
	s->sections  = new_List();
	s->indexes   = new_List();
	s->stories   = new_List();
	return s;
}
Beispiel #16
0
int main( void )
{
    //Generate strings
    char first_lit[] = "Fortran?";
    char second[] = "Now that's a name I've not heard in a long time.";

    //Convert first string to array (so we can get at the pointer to the strings pointer)
    char *first; //Declare pointer to pointer array that represents the first string

    first = malloc( ( strlen( first_lit ) + 1 ) * sizeof( char ) ); //Allocate space for the pointer array
    strcpy( first, first_lit ); //Assign values to the pointer locations

    //Copy copyme into base
    if ( astrcpy( &first, second ) ) puts( first );

    free( first );
}
Beispiel #17
0
HASHPTR InsertHash (HASHPTR symbol_tbl[], int size, char symbol[], int key)
{
    register HASHPTR hp;
    register int h;

    //printf("in insert !\n");
    if (key ==0) key = keyvalue (symbol);
    //printf("key = %d\n",key);
    h = key % size;

    hp = hashalloc(); 
    //printf("Created!\n");
    
	hp->key = key;
	hp->next = symbol_tbl[h];//την πρωτη φορα θα δειξει σε NULL meta ayto den allazei
	hp->symbol = astrcpy (hp->symbol, symbol);
   
	symbol_tbl[h]=hp;

	return(hp);
	
}
Beispiel #18
0
int main(int argc, char *argv[]){
	char testString[] = "This is a test";
	char *aCopy    = NULL;
	char *leftBit  = NULL;
	char *midBit   = NULL;
	char *rightBit = NULL;
	
	aCopy = astrcpy(testString);
	if(aCopy != NULL){
		leftBit = astrleft(testString, 4);
		if(leftBit != NULL){
			printf("Left(4): \"%s\"\n", leftBit);
		}
		else{
			printf("Error calling astrleft()\n");
		}
		midBit = astrmid(aCopy, 2, 6);
		if(midBit != NULL){
			printf("Mid(2, 6): \"%s\"\n", midBit);
		}
		else{
			printf("Error calling astrmid()\n");
		}
		rightBit = astrright(aCopy, 2);
		if(rightBit != NULL){
			printf("Right(2): \"%s\"\n", rightBit);
		}
		else{
			printf("Error calling astrright()\n");
		}
	}
	if(aCopy != NULL) free(aCopy);
	if(leftBit != NULL) free(leftBit);
	if(midBit != NULL) free(midBit);
	if(rightBit != NULL) free(rightBit);
	exit(EXIT_SUCCESS);
}
Beispiel #19
0
/** Returns a string consisting of a copy of s with all characters converted 
*** to upper case.
**/
char *astrupper(const char *s) {
	return strxupper(astrcpy(s));
}
Beispiel #20
0
/** Returns a part of a directory path */
char *getPathPart(const char *path, PathPart part) {
	char   *result = NULL;
	char   *tmp1   = NULL;
	char   *tmp2   = NULL;
	size_t pos     = 0;
	
	if (path != NULL && strlen(path) > 0) {
		tmp1 = strxreplace(astrcpy(path), '\\', '/');
		switch(part){
			case PATH_HOST:
				if (isUncPath(tmp1)) {
					tmp2 = &tmp1[2];
					if (strxpos(tmp2, '/', &pos)) {
						result = astrleft(tmp2, pos);
					}
					else {
						result = astrcpy(tmp2);
					}
				}
				else {
					result = astrcpy("");
				}
				break;
			case PATH_DRIVE:
				if (isDosPath(tmp1)) {
					result = astrleft(tmp1, 2);
				}
				else {
					result = astrcpy("");
				}
				break;
			case PATH_DIR:
				if (isUncPath(tmp1) || isDosPath(tmp1)) {
					tmp2 = strchr(&tmp1[2], '/');
				}
				else {
					tmp2 = tmp1;
				}
				if (tmp2 != NULL) {
					if (strxrpos(tmp2, '/', &pos)) {
						if (pos == 0) {
							result = astrcpy(tmp2);
						}
						else {
							result = astrleft(tmp2, pos);
						}
					}
					else {
						result = astrcpy("");
					}
				}
				else {
					result = astrcpy("");
				}
				break;
			case PATH_FILE:
			case PATH_FILEONLY:
				if ((tmp2 = strrchr(tmp1, '/')) != NULL) {
					result = &tmp2[1];
				}
				else {
					result = tmp1;
				}
				if (part == PATH_FILE) {
					result = astrcpy(result);
				}
				else if (part == PATH_FILEONLY) {
					/* Strip off file extension */
					if (strxrpos(result, '.', &pos)) {
						if(pos == 0) {
							result = astrcpy(result);
						}
						else {
							result = astrleft(result, pos);
						}
					}
					else {
						result = astrcpy(result);
					}
				}
				break;
			case PATH_EXT:
				if ((tmp2 = strrchr(tmp1, '.')) != NULL) {
					if (strchr(tmp2, '/') == NULL)  {
						result = astrcpy(&tmp2[1]);
					}
					else {
						result = astrcpy("");
					}
				}
				else {
					result = astrcpy("");
				}
				break;
			default:
				result = astrcpy("");
				break;
		}
		mu_free(tmp1);
	}
	return result;
}
Beispiel #21
0
/** Returns a string consisting of a copy of s with the first and last 
*** characters removed.  Does not actually check if said characters are 
*** quotes before doing so!
**/
char *astrunquote(const char *s) {
	if (strlen(s) == 2) return astrcpy("");
	else return astrmid(s, 1, strlen(s) - 2);
}
Beispiel #22
0
/** Returns a string consisting of a copy of s with all characters converted 
*** to lower case.
**/
char *astrlower(const char *s) {
	return strxlower(astrcpy(s));
}