static void b_free(o_string *o) { b_reset(o); free(o->data); o->data = NULL; o->maxlen = 0; }
/********************************************************************************************************** Purpose: Find the column in the transition table that corresponds to the current character Author: Thom Palmer History/Versions: 10.18.13 Called functions: chk_sym_tbl(), qsort(), malloc(), st_compare_A(), b_getsize(), strlen(), b_reset(), b_addc, free(), b_get_chmemloc() Parameters: STD sym_table, char s_order Return value: 1 on success, 0 on failure Algorithm: Check that the symbol table is valid, if s_order is A or D call qsort with the corresponding function call, allocated memory for the temporary char *, iterate over each STVR and store the contents of their plexs in the char *, use b_reset to prepare the buffer for its new contents, store the contents of the char * into the buffer, free the char *, correct the plex pointers and return. **********************************************************************************************************/ int st_sort(STD sym_table, char s_order) { unsigned int i; /* Used to iterate over STVR array */ unsigned int j; /* Used to iterate over characters in each plex */ short initOffset = 0; /* Stores the offset from the beginning of the buffer */ int charOffset = 0; /* Counts the offset from the beginning of the char* */ char * orderedBufferCont = NULL; /* Used to store the ordered lexemes from the buffer temporarily */ unsigned short buffSize = b_getsize(sym_table.plsBD); /* Needed to store the size of the buffer */ chk_sym_tbl(sym_table); switch(s_order) { /* Call qsort to sort the STVR array in ascending order */ case 'A': qsort((void*)sym_table.pstvr, sym_table.st_offset,sizeof(STVR), st_compare_A); break; /* Call qsort to sort the STVR array in descending order */ case 'D': qsort((void*)sym_table.pstvr, sym_table.st_offset,sizeof(STVR), st_compare_D); break; /* Invalid s_order return -1 */ default: return INVLD_TYPE; } /* Allocate memory for a char* the same size as the buffsize */ orderedBufferCont = (char*)malloc(sizeof(char)*buffSize); /* Iterate over the array of STVRs */ /* Cast to unsigned because the offset is and index value which cannot be negative*/ for(i = 0; i<(unsigned)sym_table.st_offset;i++) { /* For each STVR.plex store its contents character by character into the temporary char* */ for (j = 0;j<=strlen(sym_table.pstvr[i].plex);j++) { orderedBufferCont[charOffset] = sym_table.pstvr[i].plex[j]; /* Increment charOffset each time */ ++charOffset; } } /* Reset the buffer in preparation for adding the contents of the temporary char* to it */ b_reset(sym_table.plsBD); /* Iterate over the buffer and add the ordered contents in */ for( i = 0; i<buffSize;++i) { b_addc(sym_table.plsBD, orderedBufferCont[i]); } /* Free the temprary char *. It is no longer neeeded */ free((char*)orderedBufferCont); /* Cast to unsigned because the offset is and index value which cannot be negative*/ /* Iterate over the STVR array and ensure that each plex is point to the correct memory location */ for(i=0; i<(unsigned)sym_table.st_offset; i++) { sym_table.pstvr[i].plex = b_get_chmemloc(sym_table.plsBD, initOffset); /* Casting to a short because the plex is stored in the buffer which could never be larget that SHRT_MAX*/ initOffset += (short)strlen(sym_table.pstvr[i].plex)+1; } return SRT_SUCCESS; }
int scanner_init(Buffer * sc_buf) { if(b_isempty(sc_buf)) return EXIT_FAILURE;/*1*/ b_set_getc_offset(sc_buf,0);/* in case the buffer has been read previously */ b_reset(str_LTBL); line = 1; return EXIT_SUCCESS;/*0*/ /* scerrnum = 0; *//*no need - global ANSI C */ }
//------------------------------------------------------------------------------ void b_shrink(struct buffer *buf) { if (buf->pos == buf->len) { b_reset(buf); } else { buf->len -= buf->pos; memmove(buf->bytes, buf->bytes + buf->pos, buf->len); buf->pos = 0; if (buf->len < buf->size / 2) { buf->size /= 2; buf->bytes = s_realloc(buf->bytes, buf->size); } } }
/* Return the size of consumed data or -1. The output buffer is unused at this * point, we only keep a reference to the input data or a copy of them if the * reference is already used. */ static int rfc195x_add_data(struct comp_ctx *comp_ctx, const char *in_data, int in_len, struct buffer *out) { static THREAD_LOCAL struct buffer *tmpbuf = &buf_empty; if (in_len <= 0) return 0; if (comp_ctx->direct_ptr && !comp_ctx->queued) { /* data already being pointed to, we're in front of fragmented * data and need a buffer now. We reuse the same buffer, as it's * not used out of the scope of a series of add_data()*, end(). */ if (unlikely(!tmpbuf->size)) { /* this is the first time we need the compression buffer */ if (b_alloc(&tmpbuf) == NULL) return -1; /* no memory */ } b_reset(tmpbuf); memcpy(bi_end(tmpbuf), comp_ctx->direct_ptr, comp_ctx->direct_len); tmpbuf->i += comp_ctx->direct_len; comp_ctx->direct_ptr = NULL; comp_ctx->direct_len = 0; comp_ctx->queued = tmpbuf; /* fall through buffer copy */ } if (comp_ctx->queued) { /* data already pending */ memcpy(bi_end(comp_ctx->queued), in_data, in_len); comp_ctx->queued->i += in_len; return in_len; } comp_ctx->direct_ptr = in_data; comp_ctx->direct_len = in_len; return in_len; }
void scanner_init(Buffer *buf) { ca_addc(buf, '\0'); /* in case EOF is not in the buffer */ b_reset(str_LTBL); /* reset the string literal table */ line = 1; /*set the source code line number to 1*/ }