Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "history.h"
- void h_add(char** param)
- {
- #ifdef DEBUG
- // Print that we're in the h_add function
- printf("~ Add to command history\n");
- #endif
- /*
- * Count length of input parameter
- */
- int cmdlen = 0, i;
- int paramlen = arraylen((void**)param); // The length of param
- for(i = 0; i < paramlen; i++)
- {
- // Increase the length with the length of this word plus a space
- cmdlen += strlen(*(param + i)) + 1;
- }
- // Remove the trailing space character
- cmdlen--;
- /*
- * Build the command to a string
- */
- char* cmdstring = malloc(sizeof(char) * cmdlen + 1);
- int cmds_index = 0;
- // Loop through param
- for(i = 0; i < paramlen; i++)
- {
- int ci;
- for(ci = 0; ci < strlen(*(param + i)); ci++)
- {
- // Copy the character at this index to cmdstring
- *(cmdstring + cmds_index) = *(*(param + i) + ci);
- cmds_index++;
- }
- }
- // Terminate the string
- *(cmdstring + cmds_index) = '\0';
- /*
- * Allocate the new meta block
- */
- // If a meta block already exists,
- if(h_meta != NULL)
- {
- // Save the previous meta block pointer in a temprary variable
- h_metablock* temp = h_meta;
- // Allocate for the new block and set the old one as next
- h_meta = (h_metablock*)malloc(sizeof(h_metablock));
- h_meta->next = temp;
- h_meta->length = cmdlen;
- }
- // Otherwise just allocate
- else
- {
- h_meta = (h_metablock*)malloc(sizeof(h_metablock));
- h_meta->next = NULL;
- h_meta->length = cmdlen;
- }
- /*
- * Put the command into data blocks
- */
- // The amount of blocks required to store the command. Adding 1 for the
- // nullbyte on cmdlen
- int breq = ((cmdlen + 1) - ((cmdlen + 1) % BLOCK_SIZE)) / BLOCK_SIZE + 1;
- #ifdef DEBUG
- printf("~\tBlocks required to store command: %d\n", breq);
- #endif
- // Allocate block indexes for this command
- int* bindexes = h_allocate(breq);
- #ifdef DEBUG
- // In debug mode, print out the allocated block indexes
- printf("~\tAllocated block indexes: ");
- for(i = 0; i < breq; i++)
- {
- printf(" %d", *(bindexes + i));
- }
- printf("\n");
- #endif
- // Keep track of how much of the command string had been written to the
- // data blocks
- int cmdsw = 0;
- // Save indexes in meta file and write the command text into the blocks
- for(i = 0; i < breq; i++)
- {
- // Save the block index in the meta block
- h_meta->dataindex[i] = bindexes[i];
- // Keep track of the data block index we're in
- int bi;
- // Keep writing as long as less than the current block has been written
- // and less than the command string length has been written
- for(bi = 0; cmdsw < i * BLOCK_SIZE && cmdsw < cmdlen+1; cmdsw++, bi++)
- {
- // Set the character at this data block position to the current
- // command string character
- h_data[i][bi] = *(cmdstring + cmdsw);
- }
- }
- // Set a -1 to terminate array
- h_meta->dataindex[i] = -1;
- }
- int* h_allocate(int blocks)
- {
- // Declare variables for the loop
- int i, found;
- // Allocate enough space to hold the needed indices
- int* indices = malloc(sizeof(int) * blocks);
- // Set label before the bitmap search to allow retries
- searchbitmap:
- // Loop until we're out of bits or we've found all the bits we need
- for(i = 0, found = 0; i < BLOCKS && found < blocks; i++)
- {
- // If this block is available, use if for this command
- if(h_bitmap[i] == 0)
- {
- // Mark this bit as taken
- h_bitmap[i] = 1;
- // Set this index in the meta block
- indices[found] = i;
- found++;
- }
- }
- // Didn't find enough block
- if(found < blocks)
- {
- printf("Couldn't find enough blocks!!!\n");
- exit(0);
- }
- return indices;
- }
- // TODO: Finish this function
- void h_removeoldtest()
- {
- // Create a pointer for the oldest meta block and set it to the newest
- h_metablock* oldest = h_meta;
- // Follow the linked list until reaching the end
- while(oldest->next) oldest = oldest->next;
- }
- // TODO: Finish this function
- char* h_meta_getcmd(h_metablock* metablock)
- {
- #ifdef DEBUG
- // Print out the data grid
- printf("Printing out history data grid here!!\n");
- #endif
- return NULL;
- // Allocate a string of proper size
- char* output = malloc(sizeof(char) * metablock->length + 1);
- while(1)
- {
- }
- }
- char* h_cmd_index(int const index)
- {
- // Fetch the meta block at the input index
- h_metablock* head = h_meta;
- int i;
- for(i = 0; i < index; i++)
- head = head->next;
- // Fetch the command at this meta block
- char* output = h_meta_getcmd(head);
- // And return it
- return output;
- }
- void h_free()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement