/* The function displays buffer contents */ void display (Buffer *ptrBuffer){ printf("\nPrinting buffer parameters:\n\n"); printf("The capacity of the buffer is: %d\n",b_capacity(ptrBuffer)); printf("The current size of the buffer is: %d\n",b_size(ptrBuffer)); printf("\nPrinting buffer contents:\n\n"); b_print(ptrBuffer); }
/* The function display buffer contents */ void display (Buffer *ptrBuffer){ printf("\nPrinting input buffer parameters:\n\n"); printf("The capacity of the buffer is: %d\n",b_capacity(ptrBuffer)); printf("The current size of the buffer is: %d\n",b_size(ptrBuffer)); printf("The reallocation flag is: %d\n",b_rflag(ptrBuffer)); printf("\nPrinting input buffer contents:\n\n"); b_print(ptrBuffer); }
PUBLIC void test_b_size() /* The name says it all. */ { b_fsize size; errstat err; capability tmp_cap; (void)strcpy(testname, "test_b_size()"); if (verbose) printf("\n---- %s ----\n", testname); /* Get size of empty file. */ err = b_size(&empty_cap, &size); if (test_good(err, "1a")) TEST_ASSERT(size == ZERO, TEST_SERIOUS, ("%s, 1a: size should be 0 but is %ld\n", testname, (long)size)); /* Get size of file with one byte. */ err = b_size(&one_cap, &size); if (test_good(err, "1b")) TEST_ASSERT(size == ONE, TEST_SERIOUS, ("%s, 1b: size should be 1 but is %ld\n", testname, (long)size)); /* Get size of normal committed file. */ err = b_size(&commit_cap, &size); if (test_good(err, "1c")) TEST_ASSERT(size == AVG_SIZE, TEST_SERIOUS, ("%s, 1c: size should be %ld but is %ld\n", testname, (long)AVG_SIZE, (long)size)); /* Get size with minimum required rights. */ err = std_restrict(&commit_cap, BS_RGT_READ, &tmp_cap); if (test_good(err, "1d, std_restrict()")) { err = b_size(&tmp_cap, &size); if (test_good(err, "1d")) TEST_ASSERT(size == AVG_SIZE, TEST_SERIOUS, ("%s, 1d: size should be %ld but is %ld\n", testname, (long)AVG_SIZE, (long)size)); } }/* test_b_size() */
void display(Buffer *ptr_Buffer){ printf("\nPrinting buffer parameters:\n\n"); printf("The capacity of the buffer is: %d\n", b_capacity(ptr_Buffer)); printf("The current size of the buffer is: %d\n", b_size(ptr_Buffer)); printf("The operational mode of the buffer is: %d\n", b_mode(ptr_Buffer)); printf("The increment factor of the buffer is: %u\n", b_inc_factor(ptr_Buffer)); printf("The current mark of the buffer is: %d\n", b_mark(ptr_Buffer)); /*printf("The reallocation flag is: %d\n",b_rflag(ptr_Buffer));*/ printf("\nPrinting buffer contents:\n\n"); b_print(ptr_Buffer); }
/* * NAME: vol->geometry() * DESCRIPTION: determine volume location and size (possibly in a partition) */ int v_geometry(hfsvol *vol, int pnum) { Partition map; unsigned long bnum = 0; int found; vol->pnum = pnum; if (pnum == 0) { vol->vstart = 0; vol->vlen = b_size(vol); if (vol->vlen == 0) goto fail; goto done; } while (pnum--) { found = m_findpmentry(vol, "Apple_HFS", &map, &bnum); if (found == -1 || ! found) goto fail; } vol->vstart = map.pmPyPartStart; vol->vlen = map.pmPartBlkCnt; if (map.pmDataCnt > 0) { if (map.pmLgDataStart + map.pmDataCnt > map.pmPartBlkCnt) ERROR(EINVAL, "partition data overflows partition"); vol->vstart += map.pmLgDataStart; vol->vlen = map.pmDataCnt; } if (vol->vlen == 0) ERROR(EINVAL, "volume partition is empty"); done: return 0; fail: return -1; }
int main(int argc, char **argv){ Buffer *ptr_Buffer; /* pointer to Buffer structure */ FILE *fi; /* input file handle */ int loadsize = 0; /*the size of the file loaded in the buffer */ int ansi_c = !ANSI_C; /* ANSI C compliancy flag */ /* Check if the compiler option is set to compile ANSI C */ /* __DATE__, __TIME__, __LINE__, __FILE__, __STDC__ are predefined preprocessor macros*/ if (ansi_c){ err_printf("Date: %s Time: %s", __DATE__, __TIME__); err_printf("ERROR: Compiler is not ANSI C compliant!\n"); exit(1); } /* missing file name or/and mode parameter */ if (argc <= 2){ err_printf("\nDate: %s Time: %s", __DATE__, __TIME__); err_printf("\nRuntime error at line %d in file %s\n", __LINE__, __FILE__); err_printf("%s\b\b\b\b%s%s", argv[0], ": ", "Missing parameters."); err_printf("Usage: platybt source_file_name mode"); exit(1); } /* create a source code input buffer */ switch (*argv[2]){ /*gets the first caracter of the mode argument*/ case 'f': case 'a': case 'm': break; default: err_printf("%s%s%s", argv[0], ": ", "Wrong mode parameter."); exit(1); } /*create the input buffer */ ptr_Buffer = b_create(INIT_CAPACITY, INC_FACTOR, *argv[2]); if (ptr_Buffer == NULL){ err_printf("%s%s%s", argv[0], ": ", "Could not create buffer."); exit(1); } /* open the source file */ if ((fi = fopen(argv[1], "r")) == NULL){ err_printf("%s%s%s%s", argv[0], ": ", "Cannot open file: ", argv[1]); exit(1); } /* load a source file into the input buffer */ printf("Reading file %s ....Please wait\n", argv[1]); loadsize = b_load(fi, ptr_Buffer); if (loadsize == R_FAIL_1) err_printf("%s%s%s", argv[0], ": ", "Error in loading buffer."); /* close the source file */ fclose(fi); /*find the size of the file */ if (loadsize == LOAD_FAIL){ printf("The input file %s %s\n", argv[1], "is not completely loaded."); printf("Input file size: %ld\n", get_filesize(argv[1])); } /* set a mark at the last char in the buffer*/ b_setmark(ptr_Buffer, b_size(ptr_Buffer)); /* display the contents of the input buffer */ display(ptr_Buffer); /* pack the buffer * if possible, add end-of-file character (EOF) to the buffer * display again */ if (b_pack(ptr_Buffer)){ if (!b_addc(ptr_Buffer, EOF)) err_printf("%s%s%s", argv[0], ": ", "Error in writing to buffer."); display(ptr_Buffer); } /* destroy the buffer */ b_destroy(ptr_Buffer); /* make the buffer invalid It is not necessary here because the function terminates anyway, but will prevent run-time errors and crashes in future expansions */ ptr_Buffer = NULL; /*return success */ return (0); }
PUBLIC void test_combi() { errstat err; bool ok, ok1; capability new_cap, tmp_cap; b_fsize size; (void)strcpy(testname, "test_many()"); if (verbose) printf("\n---- %s ----\n", testname); /* Create an uncommitted file with the same contents as the original. */ err = b_create(&commit_cap, write_buf, AVG_SIZE, 0, &new_cap); if (test_good(err, "1a, b_create()")) { TEST_ASSERT(ok = !obj_cmp(&new_cap, &commit_cap), TEST_SERIOUS, ("%s, 1a: new object expected\n", testname)); /* Modify it. */ err = b_modify(&new_cap, AVG_OFFS, write_buf, AUX_SIZE, 0, &tmp_cap); if (test_good(err, "1a, b_modify()")) { /* The capability shouldn't change. */ TEST_ASSERT(ok1 = cap_cmp(&tmp_cap, &new_cap), TEST_SERIOUS, ("%s, 1a: previous capability expected\n", testname)); /* Clean up possible debris. */ if (!ok1) { if (ok) { err = std_destroy(&new_cap); (void)test_good(err, "std_destroy()"); } new_cap = tmp_cap; err = std_destroy(&tmp_cap); (void)test_good(err, "std_destroy()"); TEST_ASSERT(ok = !obj_cmp(&new_cap, &commit_cap), TEST_SERIOUS, ("%s, 1a: new object expected\n", testname)); } /* Delete some. */ err = b_delete(&new_cap, AVG_OFFS, AUX_SIZE, 0, &tmp_cap); if (test_good(err, "1a, b_delete()")) { TEST_ASSERT(ok1 = cap_cmp(&tmp_cap, &new_cap), TEST_SERIOUS, ("%s, 1a: previous capability expected\n", testname)); if (!ok1) { if (ok) { err = std_destroy(&new_cap); (void)test_good(err, "std_destroy()"); } new_cap = tmp_cap; err = std_destroy(&tmp_cap); (void)test_good(err, "std_destroy()"); TEST_ASSERT(ok = !obj_cmp(&new_cap, &commit_cap), TEST_SERIOUS, ("%s, 1a: new object expected\n", testname)); } /* Insert some (restore original contents) and commit. */ err = b_insert(&new_cap, AVG_OFFS, write_buf + (int)AVG_OFFS, AUX_SIZE, SAFE_COMMIT, &tmp_cap); if (test_good(err, "1a, b_insert()")) { /* The capability should be the same as the original one. */ TEST_ASSERT(ok1 = cap_cmp(&tmp_cap, &commit_cap), TEST_SERIOUS, ("%s, 1a: original capability expected\n", testname)); TEST_ASSERT(!obj_cmp(&tmp_cap, &new_cap), TEST_SERIOUS, ("%s, 1a: new object expected\n", testname)); err = b_size(&tmp_cap, &size); if (test_good(err, "1a, b_size()")) TEST_ASSERT(size == AVG_SIZE, TEST_SERIOUS, ("%s, 1a: size should be %ld but is %ld\n", testname, (long)AVG_SIZE, (long)size)); memset(read_buf, 0, (size_t)BIG_SIZE); err = b_read(&tmp_cap, ZERO, read_buf, BIG_SIZE, &size); if (test_good(err, "1a, b_read()")) { TEST_ASSERT(size == AVG_SIZE, TEST_SERIOUS, ("%s, 1a: bytes read should be %ld but is %ld\n", testname, (long)AVG_SIZE, (long)size)); TEST_ASSERT(memcmp(read_buf, write_buf, (size_t)size)==0,TEST_SERIOUS, ("%s, 1a: data read do not match original data\n", testname)); } if (!ok1) { err = std_destroy(&tmp_cap); (void)test_good(err, "1a, std_destroy()"); } ok = FALSE; /* Nothing left to destroy. */ } } } if (ok) { err = std_destroy(&new_cap); (void)test_good(err, "1a, std_destroy()"); } } } /* test_many() */
/* main function takes a PLATYPUS source file as * an argument at the command line. * usage: scanner source_file_name" */ int main(int argc, char ** argv){ Buffer *sc_buf; /* pointer to input (source) buffer */ FILE *fi; /* input file handle */ Token t; /* token produced by the scanner */ int loadsize = 0; /*the size of the file loaded in the buffer */ int ansi_c = !ANSI_C; /* ANSI C compliancy flag */ /* Check if the compiler option is set to compile ANSI C */ /* __DATE__, __TIME__, __LINE__, __FILE__, __STDC__ are predefined preprocessor macros*/ if(ansi_c){ err_printf("Date: %s Time: %s",__DATE__, __TIME__); err_printf("ERROR: Compiler is not ANSI C compliant!\n"); exit(1); } /*check for correct arrguments - source file name */ if (argc <= 1){ /* __DATE__, __TIME__, __LINE__, __FILE__ are predefined preprocessor macros*/ err_printf("Date: %s Time: %s",__DATE__, __TIME__); err_printf("Runtime error at line %d in file %s", __LINE__, __FILE__); err_printf("%s%s%s",argv[0],": ","Missing source file name."); err_printf("%s%s%s","Usage: ", "scanner", " source_file_name"); exit(1); } /* create a source code input buffer - multiplicative mode */ sc_buf = b_create(INIT_CAPACITY,INC_FACTOR,'m'); if (sc_buf == NULL){ err_printf("%s%s%s",argv[0],": ","Could not create source buffer"); exit(1); } /*open source file */ if ((fi = fopen(argv[1],"r")) == NULL){ err_printf("%s%s%s%s",argv[0],": ", "Cannot open file: ",argv[1]); exit (1); } /* load source file into input buffer */ printf("Reading file %s ....Please wait\n",argv[1]); loadsize = b_load (fi,sc_buf); if(loadsize == R_FAIL_1) err_printf("%s%s%s",argv[0],": ","Error in loading buffer."); /* close source file */ fclose(fi); /*find the size of the file */ if (loadsize == LOAD_FAIL){ printf("The input file %s %s\n", argv[1],"is not completely loaded."); printf("Input file size: %ld\n", get_filesize(argv[1])); } /* pack and display the source buffer */ if(b_pack(sc_buf)){ display(sc_buf); } /* create string Literal Table */ str_LTBL = b_create(INIT_CAPACITY,INC_FACTOR,'a'); if (str_LTBL == NULL){ err_printf("%s%s%s",argv[0],": ","Could not create string literals buffer"); exit(1); } /*Testbed for the scanner */ /* add SEOF to input program buffer*/ b_addc(sc_buf,'\0'); /* Initialize scanner input buffer */ if(scanner_init(sc_buf)){; err_printf("%s%s%s",argv[0],": ","Empty program buffer - scanning canceled"); exit(1); } printf("\nScanning source file...\n\n"); printf("Token\t\tAttribute\n"); printf("----------------------------------\n"); do{ t= mlwpar_next_token(sc_buf); print_token(t); }while(t.code != SEOF_T); if(b_size(str_LTBL)) b_print(str_LTBL); b_destroy(sc_buf); b_destroy(str_LTBL); sc_buf = str_LTBL = NULL; return (0); }
/******************************************************************************* * Purpose: Add a new entry into the symbol table array of STVR at the * next available element. * Author: Skye Turriff * History: Version 1, 19 November 2015 * Called functions: st_lookup(), b_setmark(), b_size(), b_addc(), b_rflag(), * st_incoffset() * Parameters: STD sym_table struct with valid st_size * char* lexeme pointer to VID name to be added to table * char type of VID * int line of first occurence of VID * Return value: int offset into STVR array where VID record is installed, * -2 on bad parameters, or -1 if symbol table is full. * Algorithm: Check parameters, return -2 if bad. If lexeme already in * symbol table, return the offset. If symbol table is full, * return -1. Set plex for new STVR to point to the next space * available in CA. Add each character in the VID lexeme to * this location and make a C-type string. Once the new lexeme * has been added, if at any time the location of the CA was * moved, re-calculate plex for each STVR in the STVR array. * Finally, initialize remaining members for new STVR, and * increment st_offset of global sym_table. Return offset of * new STVR in STVR array. *******************************************************************************/ int st_install(STD sym_table, char* lexeme, char type, int line) { int offset; /* Offset into array of STVR */ char r_flag; /* Memory reallocation flag for lexeme storage buffer */ char* tplex; /* Used to iterate through lexeme storage buffer */ int i; /* Loop counter for iteration through STVR array */ /* Check for valid symbol table */ if (sym_table.st_size == 0) return ERR_FAIL2; /* Check if lexeme already exists */ if ((offset = st_lookup(sym_table, lexeme)) != ERR_FAIL1) return offset; /* Ensure there is room for a new record */ if (sym_table.st_offset >= sym_table.st_size) return ERR_FAIL1; /* Set plex for new VID record */ sym_table.pstvr[sym_table.st_offset].plex = b_setmark(sym_table.plsBD, b_size(sym_table.plsBD)); /* Install new entry into STVR array, make C-type string */ r_flag = 0; for (; *lexeme; lexeme++) { b_addc(sym_table.plsBD, *lexeme); if (b_rflag(sym_table.plsBD)) r_flag = 1; } b_addc(sym_table.plsBD, '\0'); if (b_rflag(sym_table.plsBD)) r_flag = 1; /* If r_flag set, use tplex to interate through lexeme storage until '\0'. Then set plex of the current STVR to tplex + 1 */ if (r_flag) { sym_table.pstvr[0].plex = b_setmark(sym_table.plsBD, 0); tplex = sym_table.pstvr[0].plex; for (i = 1; i <= sym_table.st_offset; i++) { while (*tplex) { tplex++; continue; } sym_table.pstvr[i].plex = ++tplex; } } /* Record source line number */ sym_table.pstvr[sym_table.st_offset].o_line = line; /* Initialize status_field and i_value */ sym_table.pstvr[sym_table.st_offset].status_field &= DEFAULTZ; sym_table.pstvr[sym_table.st_offset].status_field |= DEFAULT; if (type == 'I') { /* integer */ sym_table.pstvr[sym_table.st_offset].status_field |= DT_INT; sym_table.pstvr[sym_table.st_offset].i_value.int_val = 0; } else if (type == 'F') { /* float */ sym_table.pstvr[sym_table.st_offset].status_field |= DT_FPL; sym_table.pstvr[sym_table.st_offset].i_value.fpl_val = 0.0F; } else { /* string */ sym_table.pstvr[sym_table.st_offset].status_field |= DT_STR; sym_table.pstvr[sym_table.st_offset].status_field |= SET_FLG; sym_table.pstvr[sym_table.st_offset].i_value.str_offset = -1; } st_incoffset(); /* Increment offset into STVR array of global sym_table */ return sym_table.st_offset; }
/******************************************************************** Function name: st_install() Purpose: Install a new entry in the symbol table Author: Warren Rainey History/Versions: 1.0 Called functions: st_lookup(), b_setmark, b_get_r_flag(), b_addc(), b_get_chloc(), st_incoffset() In parameters: STD, char *, char, int Return Value: Int Algorithm: - Check if symbol table is valid - This function installs a new entry (VID record) in the symbol table. First, It calls the st_lookup() function to search for the lexeme (variable name) in the symbol table. - Add new record to symbol table and increment st_offest. **********************************************************************/ int st_install(STD sym_table, char *lexeme, char type, int line){ int location = 0; int i = 0; int j = 0; STVR new_rec; /*intf("1lexeme: %s\n", lexeme);Test*/ if(!sym_table.st_size){ return R_FAIL_2; } location = st_lookup(sym_table,lexeme); if(location >= 0){/*lexeme found in symbol table*/ return location; } if(sym_table.st_offset == sym_table.st_size){ return TABLE_FULL; } /*printf("2lexeme:%s\n", lexeme);Test*/ /*sym_table.pstvr[sym_table.st_offset].plex = b_setmark(sym_table.plsBD,b_size(sym_table.plsBD));/*sym_table.st_offset*/ new_rec.plex = b_setmark(sym_table.plsBD,b_size(sym_table.plsBD));/*sym_table.st_offset*/ /*plex = b_setmark(sym_table.plsBD, getOffset(plsBD); b_setmark(sym_table.plsBD, 0); /*set to addc_offset*/ for(i = 0; lexeme[i] != '\0'; i++){ /*add lexeme to lexeme storage*/ b_addc(sym_table.plsBD, lexeme[i]); /*if plsBD has moves as result of realloc of buffer*/ /*if( b_rflag(sym_table.plsBD)){/*Remove as it may creat p while(j < sym_table.st_offset){ sym_table.pstvr[j].plex = temp; temp += strlen(temp) + 1; /* move temp to end of lexeme in buffer* j++; } }*/ } b_addc(sym_table.plsBD, '\0'); new_rec.status_field = DEFAULT; new_rec.o_line = line; /*Finding out if int, string or float*/ if(type == 'I'){/*Is int*/ new_rec.i_value.int_val = 0; new_rec.status_field |= SET_INTEGER; }else if(type == 'S'){/*Is string*/ new_rec.i_value.str_offset = -1; new_rec.status_field |= SET_STRING; new_rec.status_field |= CHK_LSB; }else{/*Is float*/ new_rec.i_value.fpl_val = 0.0f; new_rec.status_field |= SET_FLOAT; } /*printf("New plex: %s", new_rec.plex);Test*/ sym_table.pstvr[sym_table.st_offset] = new_rec; /*add new record to table*/ st_incoffset(); return sym_table.st_offset; }