Beispiel #1
0
ddstring_t *Str_Copy(ddstring_t *str, const ddstring_t *other)
{
    DENG_ASSERT(str);
    DENG_ASSERT(other);
    if(!str || !other) return str;

    Str_Free(str);

    if(!other->size)
    {
        // The original string has no memory allocated; it's a static string.
        allocateString(str, other->length, false);
        if(other->str)
            strcpy(str->str, other->str);
        str->length = other->length;
    }
    else
    {
        // Duplicate the other string's buffer in its entirety.
        str->str = str->memAlloc(other->size);
        memcpy(str->str, other->str, other->size);
        str->size = other->size;
        str->length = other->length;
    }
    return str;
}
Beispiel #2
0
ddstring_t *Str_PartAppend(ddstring_t *str, char const *append, int start, int count)
{
    int partLen;
    char *copied;

    DENG_ASSERT(str);
    DENG_ASSERT(append);

    if(!str || !append) return str;
    if(start < 0 || count <= 0) return str;

    copied = M_Malloc(count + 1);
    copied[0] = 0; // empty string

    strncat(copied, append + start, count);
    partLen = strlen(copied);

    allocateString(str, str->length + partLen + 1, true);
    memcpy(str->str + str->length, copied, partLen);
    str->length += partLen;

    // Terminate the appended part.
    str->str[str->length] = 0;

    M_Free(copied);
    return str;
}
Beispiel #3
0
ddstring_t *Str_Prepend(ddstring_t *str, char const *prepend)
{
    char *copied;
    size_t incoming;

    DENG_ASSERT(str);
    DENG_ASSERT(prepend);

    if(!str || !prepend) return str;

    incoming = strlen(prepend);
    if(incoming == 0)
        return str;

    copied = M_Malloc(incoming);
    memcpy(copied, prepend, incoming);

    allocateString(str, str->length + incoming, true);
    memmove(str->str + incoming, str->str, str->length + 1);
    memcpy(str->str, copied, incoming);
    str->length += incoming;

    M_Free(copied);
    return str;
}
Beispiel #4
0
ddstring_t *Str_ReserveNotPreserving(ddstring_t *str, int length)
{
    DENG_ASSERT(str);
    if(!str) return 0;

    if(length > 0)
    {
        allocateString(str, length, false);
    }
    return str;
}
Beispiel #5
0
char *concatenateStrings( char *aString, char *a2ndString )
{
	char *string;

	int len;

	len = strlen( aString ) + strlen( a2ndString ) + 1;
	string = allocateString( len );
	strcpy( string, aString );
	len = strlen(string);
	strcpy( string+len, a2ndString );

	return string;
}
Beispiel #6
0
/* ===== readFile =====
    This function inputs the data from the file
    Pre     hash, list, cars
    Post    nothing
 */
void readFile(HASH *hash, D_LIST *list, BST_TREE *cars){
	
	CAR car;
	FILE *fp;
	CAR *fill;
	D_NODE *pPre = list->tail;
	
    char input[250];
    fp = fopen("Cars.txt","r");
    if(!fp){
        printf("Error. Cannot Read File");
        exit(100);
    }
    
	while((fgets(input,sizeof(input), fp))){
		car = stringToCar(input);
		insert(hash,car);
		insertDNode(list, car);
		pPre = pPre->back;
		if(!BST_Retrieve(cars, &car))
		{
			fill = (CAR*)malloc(sizeof(CAR));
			if(!fill)
			{
	            printf("Memory FULL\n"),
	            exit(101);
	        }
			fill->name = allocateString(car.name);
			fill->brand = allocateString(car.brand);
			fill->cost = car.cost;
			strcpy(fill->plate, car.plate);
			BST_Insert(cars, fill);
			cars->count++;
		}
	}
}// readFile
Beispiel #7
0
ddstring_t *Str_Set(ddstring_t *str, char const *text)
{
    DENG_ASSERT(str);
    if(!str) return 0;

    {
    size_t incoming = strlen(text);
    char *copied = M_Malloc(incoming + 1); // take a copy in case text points to (a part of) str->str
    strcpy(copied, text);
    allocateString(str, incoming, false);
    strcpy(str->str, copied);
    str->length = incoming;
    M_Free(copied);
    return str;
    }
}
Beispiel #8
0
char *duplicateString( char *aString )
{
	char *value;
	int len;

	if( aString == NULL ) return NULL;

	len = strlen( aString );
	value = allocateString( len + 1 );

	if( len > 0 ) {
		strcpy( value, aString );
	}
	value[len] = 0;

	return value;
}
Beispiel #9
0
ddstring_t *Str_Append(ddstring_t *str, char const *append)
{
    DENG_ASSERT(str);
    if(!str) return 0;

    if(append && append[0])
    {
        size_t incoming = strlen(append);
        // Take a copy in case append_text points to (a part of) ds->str, which may
        // be invalidated by allocateString.
        char *copied = M_Malloc(incoming + 1);

        strcpy(copied, append);
        allocateString(str, str->length + incoming, true);
        strcpy(str->str + str->length, copied);
        str->length += incoming;

        M_Free(copied);
    }
    return str;
}
Beispiel #10
0
//===================================================================
// Prompts for a new company to insert.  If already exists, return
// error. If does not exist, ask user for all information for company
// and inserts into the BST and hashed array
//===================================================================
void insertCompany(DATA_HEAD *data)
{
    COMPANY *newCompany;
    int len;
    char name[MAX_CHARS];

    printf("Enter name of company: ");
    fgets(name, MAX_CHARS, stdin);
    len = strlen(name);
    if(name[len-1] == '\n' || name[len-1]=='\r')
            name[len-1]='\0'; // change '\n' to '\0'

    newCompany = (COMPANY*)malloc(sizeof(COMPANY));
    if(newCompany == NULL)
    {
        printf("Not enough memory, exiting program...\n");
        exit(1);
    }
    newCompany->companyName = allocateString(name);
    //if search tree function returns succes
    if(!searchHash(data, name, newCompany)) //company name has not been found, therefore does not exist in tree.
    {
        printf("Enter revenue per billion: ");
        scanf("%d", &(newCompany->revenuePerBillion));
        printf("Enter profits per million: ");
        scanf("%d", &(newCompany->profitPerMillion));
        printf("Enter number of employees: ");
        scanf("%d", &(newCompany->numberOfEmployees));
        insertManager(data, newCompany);
        (data->count)++;
        printf("%s added!\n", newCompany->companyName);
        printf("\nNumber Of Data Records: %d\n\n", data->count);
    }
    else
        printf("ERROR: DUPLICATE DATA\n");

    printf("\n");
}
Beispiel #11
0
//===================================================================
// Deletes a company from the BST/Hashed array (if it exists)
//===================================================================
void deleteCompany(DATA_HEAD *data)
{
    COMPANY *companyPtr;
    COMPANY *deletedPtr;
    int len;
    char name[MAX_CHARS];

    printf("Enter name of company: ");
    fgets(name, MAX_CHARS, stdin);
    len = strlen(name);
    if(name[len-1] == '\n' || name[len-1]=='\r')
            name[len-1]='\0'; // change '\n' to '\0'

    companyPtr = (COMPANY*)malloc(sizeof(COMPANY));
    if(companyPtr == NULL)
    {
        printf("Not enough memory, exiting program...\n");
        exit(1);
    }
    companyPtr->companyName = allocateString(name);

    deletedPtr = deleteManager(data, companyPtr);
    if(deletedPtr)
    {
        (data->count)--;
        printf("Deleted record: %-30s\n", deletedPtr->companyName);
        printf("\nNumber Of Data Records: %d\n", data->count);
        pushStack(data->pStack, deletedPtr);
    }
    else
        printf("Company is not in the list.\n");

    free(companyPtr->companyName);
    free(companyPtr);
    printf("\n");
}
Beispiel #12
0
const char* ToolBox::StringFactory::allocateString(const std::string &str)
{
    return allocateString(str.c_str(), str.size());
}
Beispiel #13
0
ptrI errorString(heapPo H, ptrI code) {
  if (!isvar(code)) {
    if (code == eINSUFARG)
      return allocateCString(H, "Insufficiently instantiated argument");
    else if (code == eINTNEEDD)
      return allocateCString(H, "Integer required");
    else if (code == eNUMNEEDD)
      return allocateCString(H, "Number required");
    else if (code == eVARNEEDD)
      return allocateCString(H, "Unbound variable required");
    else if (code == eSPACE)
      return allocateCString(H, "Out of heap space");
    else if (code == eUNIFY)
      return allocateCString(H, "Incomparible values in unification");
    else if (code == eDIVZERO)
      return allocateCString(H, "Division by zero");
    else if (code == eLSTNEEDD)
      return allocateCString(H, "List needed");
    else if (code == eTPLNEEDD)
      return allocateCString(H, "Tuple needed");
    else if (code == eSYMNEEDD)
      return allocateCString(H, "Symbol needed");
    else if (code == eSTRNEEDD)
      return allocateCString(H, "String required");
    else if (code == eCHRNEEDD)
      return allocateCString(H, "Character required");
    else if (code == eINVAL)
      return allocateCString(H, "invalid argument");
    else if (code == eNOPERM)
      return allocateCString(H, "permission denied");
    else if (code == eNOFILE)
      return allocateCString(H, "file not found");
    else if (code == eNOTDIR)
      return allocateCString(H, "not a directory");
    else if (code == eCFGERR)
      return allocateCString(H, "configuration problem");
    else if (code == eEOF)
      return allocateCString(H, "read past end-of-file");
    else if (code == eIOERROR)
      return allocateCString(H, "error on i/o");
    else if (code == eABORT)
      return allocateCString(H, "process aborted");
    else if (code == eNOTFND)
      return allocateCString(H, "not found");
    else if (code == eCODE)
      return allocateCString(H, "undefined program");
    else if (code == eFAIL)
      return allocateCString(H, "unexpected failure");
    else if (code == eHANDLE)
      return allocateCString(H, "not a valid handle");
    else if (code == eINVCODE)
      return allocateCString(H, "incorrect code type");
    else if (code == eASSIGN)
      return allocateCString(H, "assignment not allowed");
    else if (code == eDEAD)
      return allocateCString(H, "deadlock detected");
    else if (code == eSYSTEM)
      return allocateCString(H, "system overflow");
    else if (code == eDUPLICATE)
      return allocateCString(H, "duplicate request");
    else if (code == eNOIMPL)
      return allocateCString(H, "feature not implemented");
    else if (code == eNOTENUF)
      return allocateCString(H, "insufficient arguments given");
    else if (code == eCONNECT)
      return allocateCString(H, "cannot connect to host");
    else if (code == eINTRUPT)
      return allocateCString(H, "interrupted");
    else {
      char buf[MAX_MSG_LEN];

      strMsg(buf, NumberOf(buf), "Unknown error code: %w", &code);
      return allocateString(H, buf, uniStrLen(buf));
    }
  } else {
    char buf[MAX_MSG_LEN];

    strMsg(buf, NumberOf(buf), "Invalid error code: %w", &code);
    return allocateString(H, buf, uniStrLen(buf));
  }
}
Beispiel #14
0
int main(int argc, char *argv[])
{
    // expects no arguments because we are using a particular class file
    if (argc != 1)
    {
        fprintf(stderr, "Usage: heapTest\n");
        exit(-1);
    }

    // initialize: need to use a class with a user class that has at least
    // four fields
    initializeVM(10000, "userClass.class");

    // okay, begin the testing...

    Class A = getIthClass(4);

    Reference objectRef = allocateObject(getObjectClass());
    Reference integerRef = allocateObject(getIntegerClass());
    Reference aRef = allocateObject(A);
    Reference stringRef = allocateString("abcdefghijklmnopqrstuvwxyz");

    if (getObjectClass() != getClass(objectRef))
    {
        fprintf(stderr, "FAIL: getClass from objectRef\n");
    }

    if (getIntegerClass() != getClass(integerRef))
    {
        fprintf(stderr, "FAIL: getClass from integerRef\n");
    }

    if (getStringClass() != getClass(stringRef))
    {
        fprintf(stderr, "FAIL: getClass from stringRef\n");
    }

    if (A != getClass(aRef))
    {
        fprintf(stderr, "FAIL: getClass from aRef\n");
    }

    if (strcmp(getStringValue(stringRef), "abcdefghijklmnopqrstuvwxyz"))
    {
        fprintf(stderr, "FAIL: getStringValue\n");
    }

    putIntegerValue(integerRef, 1066);
    if (getIntegerValue(integerRef) != 1066)
    {
        fprintf(stderr, "FAIL: getIntegerValue\n");
    }

    putField(aRef, 0, integerRef);
    if (getField(aRef, 0) != integerRef)
    {
        fprintf(stderr, "FAIL: getField 0\n");
    }

    putField(aRef, 3, objectRef);
    if (getField(aRef, 3) != objectRef)
    {
        fprintf(stderr, "FAIL: getField 3\n");
    }

    printf("testing complete.\n");

    return 0;
}