Untitled
unknown
c_cpp
8 months ago
3.7 kB
2
Indexable
Never
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "assignment2.h" int main(int argc, char *argv[]){ struct personalInfo* myInfo = (struct personalInfo*)malloc(sizeof(struct personalInfo)); myInfo->firstName = argv[1]; myInfo->lastName = argv[2]; myInfo->studentID = 921080157; myInfo->level = SENIOR; myInfo->languages = KNOWLEDGE_OF_C +KNOWLEDGE_OF_JAVA + KNOWLEDGE_OF_JAVASCRIPT + KNOWLEDGE_OF_CPLUSPLUS + KNOWLEDGE_OF_HTML + KNOWLEDGE_OF_MIPS_ASSEMBLER; memcpy(myInfo->message, argv[3], 100); int result = writePersonalInfo(myInfo); free(myInfo); char* bufferPtr = malloc(BLOCK_SIZE); // this keeps track of how much space is being used in the buffer size_t bufferIndex = 0; size_t spaceLeft = BLOCK_SIZE; size_t spaceTaken = 0; int bool = 1; printf("\n"); while(bool == 1){ const char* linePtr = getNext(); //get line from getNext() //check if linePtr is null, if so exit loop if(linePtr == NULL){ printf("NULL REACHED\n"); commitBlock(bufferPtr); bool = 0; } else{ //get the size of the line using strlen size_t lineLength = strlen(linePtr); /* now that we know the size of the line, we need to figure out if there is enough space in the buffer to add it */ if(lineLength <= spaceLeft){ printf("CASE 1 REACHED\n"); //add line to buffer memcpy(bufferPtr + bufferIndex, linePtr, lineLength); //update buffer index bufferIndex += lineLength; //update space left spaceLeft -= lineLength; } else if(lineLength > spaceLeft){ printf("to do\n"); if(spaceLeft == 0){ printf("CASE 1 REACHED\n"); //buffer is filled so commit it commitBlock(bufferPtr); bufferIndex = 0; spaceLeft = BLOCK_SIZE; } else{ printf("CASE 2 REACHED\n"); /* we need to figure out how much of the line to add to fill the buffer and then use the remaining bytes to overwrite the buffer from the start */ //add the amount in spaceLeft of bytes from linePtr to buffer memcpy(bufferPtr + bufferIndex, linePtr, spaceLeft); bufferIndex += spaceLeft; //figure out remainaing bytes of line to add printf("Line Length is %zu\n", lineLength); printf("Space left is %zu\n", spaceLeft); size_t remainingBytes = lineLength - spaceLeft; printf("Remaining bytes is %zu\n", remainingBytes); commitBlock(bufferPtr); //buffer is filled so commit it bufferIndex = 0; //reset bufferIndex spaceLeft = BLOCK_SIZE; //reset spaceLeft //add remaining bytes to the start of the buffer memcpy(bufferPtr, linePtr + remainingBytes, remainingBytes); bufferIndex += remainingBytes; spaceLeft -= remainingBytes; remainingBytes = 0; //reset remainingBytes } } } } free(bufferPtr); return checkIt(); }
Leave a Comment