Exemplo n.º 1
0
//this is the main function
int main(int argc,char **argv)
{

    FILE *intersectIPstream;
    Hash *mp1,*mp2;
    mp1=parseFileDataStream(INPUTSTREAM1);
    mp2=parseFileDataStream(INPUTSTREAM2);
    if(!mp1||!mp2)
    {
        perror("either of the file is not read properly");
        return -1;
    }
    intersectIPstream=getIntersection(mp1,mp2);
    hashDestroy(mp1);
    hashDestroy(mp2);
    mp1=mp2=NULL;
    fclose(intersectIPstream);

    return  0;
}
Exemplo n.º 2
0
int main(int argc, char const *argv[]) {

	// default to a table of 1,000 elements.
	size_t size = 1000;

	if (argc > 1) {
		size = atoi(argv[1]);
		if (!size) {
			// couldn't convert argv[1] to a number
			fprintf(stderr, USAGE);
			exit(EXIT_FAILURE);
		}
	}

	// create a new hash table
	Hash *h = hashCreate(size);

	if (!h) {
		// failed to create hash table
		fprintf(stderr, "failed to create hash table\n");
		exit(EXIT_FAILURE);
	} else {
		printf("Successfully created a hash table of size %zu.\n",size);
		FILE *opt;
		if ((opt = fopen(OPTIONS_FILE,"r"))) {
			char c;
			while((c = fgetc(opt)) != EOF) {
				putchar(c);
			}
			fclose(opt);
		} else {
			fprintf(stderr, "failed to open %s\n", OPTIONS_FILE);
			exit(EXIT_FAILURE);
		}
	}

	// create some variables
	char *command, *key, *obj, *res;
	
	bool hasSet,hasGet,onOwn;
	hasSet = hasGet = onOwn = false;

	bool keepGoing = true;

	while(keepGoing) {
		// initialize the variables and print the prompt
		command = key = obj = res = NULL;

		// encourage the user to try out the different commands
		if (!hasSet) {
			printf("%% (type 'set') ");
		} else if (!hasGet) {
			printf("%% (type 'get') ");
		} else if (!onOwn) {
			onOwn = true;
			printf("You're on your own now... available commands: set, get, delete, load\n");
			printf("%% ");
		} else {
			printf("%% ");
		}

		// read the command
		command = readString();

		// process a set command
		if (!strcasecmp(command,"set")) {
			hasSet = true;
			printf("\tkey: ");
			key = readString();

			printf("\tvalue: ");
			obj = readString();

			printf("\tresult: ");
			printf(hashSet(h,key,obj) ? "SUCCESS\n" : "FAILURE\n");

		// process a get command
		} else if (!strcasecmp(command,"get")) {
			hasGet = true;
			printf("\tkey: ");
			key = readString();
			if ((res = (char *) hashGet(h,key))){
				printf("\tresult: %s\n", res);
			} else {
				printf("\tresult: NULL\n");
			}

		// process a delete command
		} else if (!strcasecmp(command,"delete")) {
			printf("\tkey: ");
			key = readString();
			if ((res = (char *) hashDelete(h,key))){
				printf("\tresult: %s\n", res);
				free(res);
			} else {
				printf("\tresult: NULL\n");
			}

		// process a load command
		} else if (!strcasecmp(command,"load")) {
			printf("\tresult: %f\n", hashLoad(h));

		// let the user exit
		} else if (!strcasecmp(command,"exit") || !strcasecmp(command,"quit")) {
			keepGoing = false;			

		// invalid command
		} else if (strlen(command) > 0) {
			printf("invalid command\n");
		} 

		// free the command and key, as necessary
		if (command) { free(command); }
		if (key) { free(key); }

	}

	hashDestroy(h,destroy);
	return EXIT_SUCCESS;
}
Exemplo n.º 3
0
int compress(FILE* inputFile, FILE* outputFile, int compressionLevel)
{
    struct BitwiseBufferedFile* w = openBitwiseBufferedFile(NULL, 1, -1, outputFile);
    uint8_t readByte[LOCAL_BYTE_BUFFER_LENGTH];
    size_t indexLength = INITIAL_INDEX_LENGTH;
    size_t bufferedBytes;
    int byteIndex = 0;
    struct LZ78HashTableEntry* hashTable;
    uint32_t childIndex = 257;
    uint32_t lookupIndex = ROOT_INDEX;
    uint32_t indexLengthMask = (1 << INITIAL_INDEX_LENGTH) - 1;
    uint32_t child;
    if((compressionLevel < MIN_COMPRESSION_LEVEL || compressionLevel > MAX_COMPRESSION_LEVEL) || inputFile == NULL || w == NULL)
    {
        errno = EINVAL;
        if(w != NULL) closeBitwiseBufferedFile(w);
        return -1;
    }
    uint32_t hashTableEntries = compressionParameters[compressionLevel - MIN_COMPRESSION_LEVEL].hashTableEntries;
    uint32_t moduloMask = hashTableEntries - 1;
    uint32_t maxChild = compressionParameters[compressionLevel - MIN_COMPRESSION_LEVEL].maxChild;
    hashTable = hashCreate(hashTableEntries, moduloMask);
    if(hashTable == NULL)
    {
        closeBitwiseBufferedFile(w);
        return -1;
    }
    if(writeBitBuffer(w, (uint32_t)compressionLevel, 3) == -1)
        goto exceptionHandler;
    while(!feof(inputFile) && !ferror(inputFile))
    {
        bufferedBytes = fread(readByte, 1, LOCAL_BYTE_BUFFER_LENGTH, inputFile);
        for(byteIndex = 0; byteIndex < bufferedBytes; byteIndex++)
        {
            child = hashLookup(hashTable, lookupIndex, readByte[byteIndex], moduloMask);
            if(child != ROOT_INDEX) lookupIndex = child; //ROOT_INDEX means NOT FOUND
            else
            {
                if(writeBitBuffer(w, lookupIndex, indexLength) == -1 || hashInsert(hashTable, lookupIndex, readByte[byteIndex], childIndex, moduloMask) == -1)
                    goto exceptionHandler;
                childIndex++;
                if((childIndex & indexLengthMask) == 0) //A power of 2 is reached
                {
                    //The length of the transmitted index is incremented
                    indexLength++;
                    //The next power of 2 mask is set
                    indexLengthMask = (indexLengthMask << 1) | 1;
                }
                //readByte value is also the right index to start with next time
                //because you have to start from the last character recognized
                lookupIndex = readByte[byteIndex] + 1; //ROOT_INDEX = 0 so ASCII characters are indexed in [1, 257]
                if (childIndex == maxChild) //hash table is full
                {
                    if(hashReset(hashTable, hashTableEntries, moduloMask) == NULL)
                        goto exceptionHandler; //hash table was not successfully created
                    childIndex = FIRST_CHILD; //starts from the beginning
                    indexLength = INITIAL_INDEX_LENGTH;
                    indexLengthMask = (1 << INITIAL_INDEX_LENGTH) - 1;
                }
            }
        }
    }
    if(ferror(inputFile))
    {
        errno = EBADFD;
        goto exceptionHandler;
    }
    if(writeBitBuffer(w, lookupIndex, indexLength) == -1 || writeBitBuffer(w, ROOT_INDEX, indexLength) == -1)
        goto exceptionHandler;
    hashDestroy(hashTable, hashTableEntries);
    return closeBitwiseBufferedFile(w);

    exceptionHandler:
        hashDestroy(hashTable, hashTableEntries);
        closeBitwiseBufferedFile(w);
        return -1;
}