static void writeTokens(DynArray_T oTokens) { int i; int iLength; struct Token *psToken; assert(oTokens != NULL); iLength = DynArray_getLength(oTokens); printf("Numbers: "); for (i = 0; i < iLength; i++) { psToken = DynArray_get(oTokens, i); if (psToken->eType == TOKEN_NUMBER) printf("%s ", psToken->pcValue); } printf("\n"); printf("Words: "); for (i = 0; i < iLength; i++) { psToken = DynArray_get(oTokens, i); if (psToken->eType == TOKEN_WORD) printf("%s ", psToken->pcValue); } printf("\n"); }
static int test_DynArray_getset() { printf("Testing DynArray_get(), DynArray_set(): "); int result = 1; DynArray_t da; DynArray_init(&da, 10); // Set a value and retrieve it int set = 123; DynArray_set(&da, 5, &set); int get = *((int*) DynArray_get(&da, 5) ); result &= (set == get); // Try to retrieve a value beyond DynArray length int* nullGet = DynArray_get(&da, 15); result &= nullGet == NULL; // Try storing a uintptr uintptr_t setPtr = 125; uintptr_t getPtr; DynArray_set(&da, 7, (void*) setPtr); getPtr = (uintptr_t) DynArray_get(&da, 7); result &= getPtr == 125; DynArray_uninit(&da); printf("%d\n", result); return result; }
void Command_free(Command_T oCommand) { int iLength; int i; char* pcArg; assert(oCommand != NULL); assert(oCommand->pcName != NULL); assert(oCommand->oArgs != NULL); /* free the command name */ free(oCommand->pcName); /* free the stdin redirect if not null */ if (oCommand->pcStdIn != NULL) free(oCommand->pcStdIn); /* free the stdout redirect if not null */ if (oCommand->pcStdOut != NULL) free(oCommand->pcStdOut); iLength = DynArray_getLength(oCommand->oArgs); for (i = 0; i < iLength; i++) { pcArg = DynArray_get(oCommand->oArgs, i); free(pcArg); } DynArray_free(oCommand->oArgs); }
int main(void) { DynArray* test; test = DynArray_create(); DynArray_append(test, 5); DynArray_append(test, 7); DynArray_append(test, 8); printf("%d ", DynArray_get(test, 3)); printf("%d", DynArray_get(test, 2)); printf("%d", DynArray_get(test, 1)); printf("%d", DynArray_get(test, 5)); DynArray_destroy(test); // DynArray (array,7); //DynArray.expandarray(array, 7); return (EXIT_SUCCESS); }
void hm_destroy(hashmap *map){ int i, j; if(map){ if(map->slots){ for(i = 0; i < DynArray_count(map->slots); i++){ DynArray *slot = DynArray_get(map->slots, i); if(bucket){ for(j = 0; j < DynArray_count(slot); j++){ free(DynArray_get(slot, j)); } DynArray_destroy(slot); } } DynArray_destroy(map->buckets); } free(map); } }
static void freeTokens(DynArray_T oTokens) { int i; int iLength; struct Token *psToken; assert(oTokens != NULL); iLength = DynArray_getLength(oTokens); for (i = 0; i < iLength; i++) { psToken = DynArray_get(oTokens, i); free(psToken->pcValue); free(psToken); } }
int main(int argc, char *argv[]) { char *pcLine; DynArray_T oTokens; DynArray_T oArgs; int iArgLength; int i; Command_T oCommand; pcPgmName = argv[0]; /* Write to stdout a prompt consisting of a percent sign and a space. */ fprintf(stdout, "%% "); /* Read a line (that is, an array of characters) from stdin. */ while ((pcLine = Token_readLine(stdin)) != NULL) { /* Write that line (array of characters) to stdout, and flush the stdout buffer. */ fprintf(stdout, "%s\n", pcLine); fflush(stdout); /* Pass the line (array of characters) to your lexical analyzer to create a DynArray object containing tokens. */ oTokens = Lex_analyze(pcLine); oCommand = Syn_analyze(oTokens); if (oCommand != NULL) { if (Command_getName(oCommand) != NULL) fprintf(stdout, "Command name: %s\n", Command_getName(oCommand)); if (Command_getArgs(oCommand) != NULL) { oArgs = Command_getArgs(oCommand); iArgLength = DynArray_getLength(oArgs); for (i = 0; i < iArgLength; i++) { fprintf(stdout, "Command arg: %s\n", (char *)DynArray_get(oArgs, i)); } } if (Command_getStdIn(oCommand) != NULL) { fprintf(stdout, "Command stdin: %s\n", Command_getStdIn(oCommand)); } if (Command_getStdOut(oCommand) != NULL) { fprintf(stdout, "Command stdout: %s\n", Command_getStdOut(oCommand)); } Command_free(oCommand); } if (oTokens != NULL) { Token_freeTokens(oTokens); DynArray_free(oTokens); } fprintf(stdout, "%% "); free(pcLine); } fprintf(stdout, "\n"); return 0; }
Command_T Command_new(char *pcName, DynArray_T oArgs, char *pcStdIn, char *pcStdOut) { int i; int argsLength; /* length of oArgs array */ char* pcArg; size_t pcArgLength; /* track how much memory to give pcArg */ Command_T oCommand; /* a command must have a name */ assert(pcName != NULL); assert(oArgs != NULL); oCommand = (Command_T) malloc(sizeof(struct Command)); if (oCommand == NULL) { fprintf(stderr, "%s: Can't allocate memory for command\n", pcPgmName); exit(EXIT_FAILURE); } /* allocate memory for and set the command name */ oCommand->pcName = (char*)malloc(strlen(pcName) + 1); if (oCommand->pcName == NULL) { fprintf(stderr, "%s: Can't allocate memory for command\n", pcPgmName); exit(EXIT_FAILURE); } strcpy(oCommand->pcName, pcName); /* allocate memory for and set the command arguments*/ argsLength = DynArray_getLength(oArgs); oCommand->oArgs = DynArray_new(0); for (i = 0; i < argsLength; i++) { pcArgLength = strlen((char*) DynArray_get(oArgs, i)); pcArg = (char*) malloc(pcArgLength + 1); strcpy(pcArg, DynArray_get(oArgs, i)); if (pcArg == NULL) { fprintf(stderr, "%s: Can't allocate memory for command args\n", pcPgmName); exit(EXIT_FAILURE); } DynArray_add(oCommand->oArgs, pcArg); } /* allocate memory for and set the command stdin redirect */ if (pcStdIn == NULL) { oCommand->pcStdIn = NULL; } else { oCommand->pcStdIn = (char*)malloc(strlen(pcStdIn) + 1); if (oCommand->pcStdIn == NULL) { fprintf(stderr, "%s: Can't allocate memory for command\n", pcPgmName); exit(EXIT_FAILURE); } strcpy(oCommand->pcStdIn, pcStdIn); } /* allocate memory for and set the command stdout redirect*/ if (pcStdOut == NULL) { oCommand->pcStdOut = NULL; } else { oCommand->pcStdOut = (char*)malloc(strlen(pcStdOut) + 1); if (oCommand->pcStdOut == NULL) { fprintf(stderr, "%s: Can't allocate memory for command\n", pcPgmName); exit(EXIT_FAILURE); } strcpy(oCommand->pcStdOut, pcStdOut); } return oCommand; }
int main(int argc, char *argv[]) /* Read lines from the .ishrc file residing in the HOME directory until EOF is reached. Write each line that is read to stdout and execute each line. Read a line from stdin and execute it. Repeat until EOF. Return 0. */ { char acLine[MAX_LINE_SIZE]; char *pcTemp; DynArray_T oHistoryList; FILE *psFile; int i; void (*pfRet)(int); pfRet = signal(SIGINT, SIG_IGN); if(pfRet == SIG_ERR) {perror(argv[0]); exit(EXIT_FAILURE); } oHistoryList = DynArray_new(0); pcTemp = getIshrc(); psFile = fopen(pcTemp, "r"); free(pcTemp); while (psFile != NULL && fgets(acLine, MAX_LINE_SIZE, psFile) != NULL) { /* Remove '\n' if acLine ends with '\n'. This is done so the commands in the history list do not end with '\n', which is necessary to properly expand !commandprefix. */ if(acLine[strlen(acLine)-1] == '\n') acLine[strlen(acLine)-1] = '\0'; printf("%% %s\n", acLine); /* Explicitly flush the stdout buffer so we can test ish properly by redirecting the output to a file. */ fflush(stdout); performCommand(acLine, oHistoryList, argv[0]); } printf("%% "); fflush(stdout); while (fgets(acLine, MAX_LINE_SIZE, stdin) != NULL) { /* Remove '\n' if acLine ends with '\n'. This is done so the commands in the history list do not end with '\n', which is necessary to properly expand !commandprefix. */ if(acLine[strlen(acLine)-1] == '\n') acLine[strlen(acLine)-1] = '\0'; performCommand(acLine, oHistoryList, argv[0]); printf("%% "); fflush(stdout); } printf("\n"); fflush(stdout); for(i = 0; i < DynArray_getLength(oHistoryList); i++) free(DynArray_get(oHistoryList, i)); DynArray_free(oHistoryList); return 0; }