int Stream::put( unsigned int i) { unsigned int tmp; char pad; getcount =0; putcount +=4; NETWORK_TRACE("put int " << i << " c(" << putcount << ") "); switch(_code){ case internal: if (put_bytes(&i, sizeof(int)) != sizeof(int)) return FALSE; break; case external: { tmp = htonl(i); pad = 0; for (int s=0; s < INT_SIZE-(int)sizeof(int); s++) { if (put_bytes(&pad, 1) != 1) return FALSE; } if (put_bytes(&tmp, sizeof(int)) != sizeof(int)) return FALSE; break; } case ascii: return FALSE; } return TRUE; }
int Stream::put( char const *s, int l) { NETWORK_TRACE("put string \"" << s << "\" and int " << l); switch(_code){ case internal: case external: if (!s){ if (get_encryption()) { int len = 1; if (put(len) == FALSE) { return FALSE; } } if (put_bytes(BIN_NULL_CHAR, 1) != 1) return FALSE; } else{ if (get_encryption()) { if (put(l) == FALSE) { return FALSE; } } if (put_bytes(s, l) != l) return FALSE; } break; case ascii: return FALSE; } return TRUE; }
int Stream::put( uint64_t l) { char pad; NETWORK_TRACE("put uint64_t" << l); switch(_code){ case internal: if (put_bytes(&l, sizeof(uint64_t)) != sizeof(uint64_t)) return FALSE; break; case external: if ((sizeof(int) == sizeof(uint64_t)) || (sizeof(uint64_t) > INT_SIZE)) { return put((unsigned long)l); } else { if (!hton_is_noop()) { // need to convert to network order l = htonLL(l); } if (sizeof(uint64_t) < INT_SIZE) { pad = 0; for (int s=0; s < INT_SIZE-(int)sizeof(uint64_t); s++) { if (put_bytes(&pad, 1) != 1) return FALSE; } } if (put_bytes(&l, sizeof(uint64_t)) != sizeof(uint64_t)) return FALSE; } break; case ascii: return FALSE; } return TRUE; }
int Stream::put( int i) { int tmp; char pad; getcount =0; putcount +=4; NETWORK_TRACE("put int " << i << " c(" << putcount << ") "); //dprintf(D_ALWAYS, "put(int) required\n"); switch(_code){ case internal: if (put_bytes(&i, sizeof(int)) != sizeof(int)) return FALSE; break; case external: { tmp = htonl(i); pad = (i >= 0) ? 0 : 0xff; // sign extend value for (int s=0; s < INT_SIZE-(int)sizeof(int); s++) { if (put_bytes(&pad, 1) != 1) return FALSE; } if (put_bytes(&tmp, sizeof(int)) != sizeof(int)) return FALSE; break; } case ascii: return FALSE; } return TRUE; }
int Stream::put( long l) { char pad; NETWORK_TRACE("put long " << l); switch(_code){ case internal: if (put_bytes(&l, sizeof(long)) != sizeof(long)) return FALSE; break; case external: if ((sizeof(int) == sizeof(long)) || (sizeof(long) > INT_SIZE)) { return put((int)l); } else { if (!hton_is_noop()) { // need to convert to network order l = htonL(l); } if (sizeof(long) < INT_SIZE) { pad = (l >= 0) ? 0 : 0xff; // sign extend value for (int s=0; s < INT_SIZE-(int)sizeof(long); s++) { if (put_bytes(&pad, 1) != 1) return FALSE; } } if (put_bytes(&l, sizeof(long)) != sizeof(long)) return FALSE; } break; case ascii: return FALSE; } return TRUE; }
/* Check that an in-use record is valid. */ static bool tdb_check_used_record(struct tdb_context *tdb, tdb_off_t off, const struct tdb_record *rec, unsigned char **hashes, int (*check)(TDB_DATA, TDB_DATA, void *), void *private_data) { TDB_DATA key, data; if (!tdb_check_record(tdb, off, rec)) return false; /* key + data + tailer must fit in record */ if (rec->key_len + rec->data_len + sizeof(tdb_off_t) > rec->rec_len) { TDB_LOG((tdb, TDB_DEBUG_ERROR, "Record offset %d too short for contents\n", off)); return false; } key = get_bytes(tdb, off + sizeof(*rec), rec->key_len); if (!key.dptr) return false; if (tdb->hash_fn(&key) != rec->full_hash) { TDB_LOG((tdb, TDB_DEBUG_ERROR, "Record offset %d has incorrect hash\n", off)); goto fail_put_key; } /* Mark this offset as a known value for this hash bucket. */ record_offset(hashes[BUCKET(rec->full_hash)+1], off); /* And similarly if the next pointer is valid. */ if (rec->next) record_offset(hashes[BUCKET(rec->full_hash)+1], rec->next); /* If they supply a check function and this record isn't dead, get data and feed it. */ if (check && rec->magic != TDB_DEAD_MAGIC) { data = get_bytes(tdb, off + sizeof(*rec) + rec->key_len, rec->data_len); if (!data.dptr) goto fail_put_key; if (check(key, data, private_data) == -1) goto fail_put_data; put_bytes(tdb, data); } put_bytes(tdb, key); return true; fail_put_data: put_bytes(tdb, data); fail_put_key: put_bytes(tdb, key); return false; }
static void start_message(void *impl, char *name, uint8_t msg_id __attribute__((unused)), uint8_t payload_len __attribute__((unused))) { uint8_t ac_id = AC_ID; struct onboard_transport *onboard = (struct onboard_transport *) impl; onboard->buffer_idx = 0; onboard->array_length = 0; put_bytes(onboard, DL_TYPE_TIMESTAMP, 4, (uint8_t *) onboard->timestamp); put_bytes(onboard, DL_TYPE_UINT8, 1, (uint8_t *) &ac_id); onboard->buffer_idx += snprintf(onboard->buffer + onboard->buffer_idx, ONBOARD_BUFFER_LEN - onboard->buffer_idx, " %s", name); }
// Handshake with client and attach to PSL static struct client *_client_connect(int *fd, char *ip) { struct client *client; uint8_t buffer[MAX_LINE_CHARS]; uint8_t ack[3]; uint16_t map; int rc; // Parse client handshake data ack[0] = PSLSE_DETACH; memset(buffer, '\0', MAX_LINE_CHARS); rc = get_bytes(*fd, 5, buffer, timeout, 0, fp, -1, -1); if ((rc < 0) || (strcmp((char *)buffer, "PSLSE"))) { info_msg("Connecting application is not PSLSE client\n"); info_msg("Expected: \"PSLSE\" Got: \"%s\"", buffer); put_bytes(*fd, 1, ack, fp, -1, -1); close_socket(fd); return NULL; } rc = get_bytes_silent(*fd, 2, buffer, timeout, 0); if ((rc < 0) || ((uint8_t) buffer[0] != PSLSE_VERSION_MAJOR) || ((uint8_t) buffer[1] != PSLSE_VERSION_MINOR)) { info_msg("Client is wrong version\n"); put_bytes(*fd, 1, ack, fp, -1, -1); close_socket(fd); return NULL; } // Initialize client struct client = (struct client *)calloc(1, sizeof(struct client)); client->fd = *fd; client->ip = ip; client->pending = 1; client->timeout = timeout; client->flushing = FLUSH_NONE; client->state = CLIENT_INIT; // Return acknowledge to client ack[0] = PSLSE_CONNECT; map = htons(afu_map); memcpy(&(ack[1]), &map, sizeof(map)); if (put_bytes(client->fd, 3, ack, fp, -1, -1) < 0) { _free_client(client); return NULL; } info_msg("%s connected", client->ip); return client; }
// Attach to AFU static void _attach(struct psl *psl, struct client *client) { uint64_t wed; uint8_t ack; uint8_t buffer[MAX_LINE_CHARS]; size_t size; // FIXME: This only works for dedicate mode // Get wed value from application ack = PSLSE_DETACH; size = sizeof(uint64_t); if (get_bytes_silent(client->fd, size, buffer, psl->timeout, &(client->abort)) < 0) { warn_msg("Failed to get WED value from client"); client_drop(client, PSL_IDLE_CYCLES, CLIENT_NONE); goto attach_done; } memcpy((char *)&wed, (char *)buffer, sizeof(uint64_t)); wed = ntohll(wed); // Send start to AFU if (add_job(psl->job, PSL_JOB_START, wed) != NULL) { psl->idle_cycles = PSL_IDLE_CYCLES; ack = PSLSE_ATTACH; } attach_done: if (put_bytes(client->fd, 1, &ack, psl->dbg_fp, psl->dbg_id, client->context) < 0) { client_drop(client, PSL_IDLE_CYCLES, CLIENT_NONE); } }
static void create_trip_name(dive_trip_t *trip, struct membuffer *name, struct tm *tm) { put_format(name, "%02u-", tm->tm_mday); if (trip->location) { char ascii_loc[MAXTRIPNAME+1], *p = trip->location; int i; for (i = 0; i < MAXTRIPNAME; ) { char c = *p++; switch (c) { case 0: case ',': case '.': break; case 'a' ... 'z': case 'A' ... 'Z': ascii_loc[i++] = c; continue; default: continue; } break; } if (i > 1) { put_bytes(name, ascii_loc, i); return; } }
// Increase the maximum number of interrupts static void _max_irqs(struct client *client, uint8_t id) { struct psl *psl; uint8_t buffer[MAX_LINE_CHARS]; uint8_t major, minor; uint16_t value; // Retrieve requested new maximum interrupts psl = _find_psl(id, &major, &minor); if (get_bytes(client->fd, 2, buffer, psl->timeout, &(client->abort), psl->dbg_fp, psl->dbg_id, client->context) < 0) { client_drop(client, PSL_IDLE_CYCLES, CLIENT_NONE); return; } memcpy((char *)&client->max_irqs, (char *)buffer, sizeof(uint16_t)); client->max_irqs = ntohs(client->max_irqs); // Limit to legal value if (client->max_irqs < psl->mmio->desc.num_ints_per_process) client->max_irqs = psl->mmio->desc.num_ints_per_process; if (client->max_irqs > 2037 / psl->mmio->desc.num_of_processes) client->max_irqs = 2037 / psl->mmio->desc.num_of_processes; // Return set value buffer[0] = PSLSE_MAX_INT; value = htons(client->max_irqs); memcpy(&(buffer[1]), (char *)&value, 2); if (put_bytes(client->fd, 3, buffer, psl->dbg_fp, psl->dbg_id, client->context) < 0) { client_drop(client, PSL_IDLE_CYCLES, CLIENT_NONE); } }
void put(FTYPE map, int *ptr[], int idim[], void *idat) { int i, nbytes; void (*swap)(); dptr = idat; nbytes = map.dtype->bits/8; len = 0; poff = -999999; ocumprod[0] = nbytes*map.dtype->channels; icumprod[0] = nbytes*1; for(i=0; i<map.ndim; i++) { icumprod[i+1] = icumprod[i]*idim[i]; ocumprod[i+1] = ocumprod[i]*map.dim[i]; } if (map.swap) swap = map.dtype->swap; else swap = copy; put_bytes(map.ndim-1, map.fp, ptr, idim, (unsigned char *)idat, map.off, 0,swap); swap(len,dptr,wbuf); if (fwrite(wbuf,1,len,map.fp) != len) { /* Problem */ (void)fclose(map.fp); (void)mexErrMsgTxt("Problem writing data (3)."); } }
// ----------------------------------------------------------------------------- // Note that this function can only write to an already "allocated" block (in // the range of <num_blocks>). // If you allocate a new block, please use "append_block" instead. // ----------------------------------------------------------------------------- bool BlockFile::write_block( // write a <block> into <index> Block block, // a <block> int index) // position of the blocks { index++; // extrnl block to intrnl block // move to the position if (index <= num_blocks_ && index > 0) { seek_block(index); } else { printf("BlockFile::write_block request the block %d " "which is illegal.", index - 1); error("\n", true); } put_bytes(block, block_length_);// write this block if (index + 1 > num_blocks_) { // update <act_block_> fseek(fp_, 0, SEEK_SET); act_block_ = 0; } else { act_block_ = index + 1; } return true; }
static inline AVP_dword DATA_PARAM put_avp_property( Serialize* sz, AVP_Property* val ) { AVP_Property converted; memcpy (&converted, val, sizeof(AVP_Property)); converted.id = ReadWordPtr (&converted.id); return put_bytes (sz, &converted, sizeof(converted)); }
void put_vformat(struct membuffer *b, const char *fmt, va_list args) { /* Handle the common case on the stack */ char buffer[128], *p; int len; len = vsnprintf(buffer, sizeof(buffer), fmt, args); if (len <= sizeof(buffer)) { put_bytes(b, buffer, len); return; } p = malloc(len); len = vsnprintf(p, len, fmt, args); put_bytes(b, p, len); free(p); }
// Query AFU descriptor data static void _query(struct client *client, uint8_t id) { struct psl *psl; uint8_t *buffer; uint8_t major, minor; int size, offset; psl = _find_psl(id, &major, &minor); size = 1 + sizeof(psl->mmio->desc.num_ints_per_process) + sizeof(client->max_irqs) + sizeof(psl->mmio->desc.req_prog_model) + sizeof(psl->mmio->desc.PerProcessPSA) + sizeof(psl->mmio->desc.PerProcessPSA_offset) + sizeof(psl->mmio->desc.AFU_EB_len) + sizeof(psl->mmio->desc.crptr->cr_device) + sizeof(psl->mmio->desc.crptr->cr_vendor) + sizeof(psl->mmio->desc.crptr->cr_class); buffer = (uint8_t *) malloc(size); buffer[0] = PSLSE_QUERY; offset = 1; memcpy(&(buffer[offset]), (char *)&(psl->mmio->desc.num_ints_per_process), sizeof(psl->mmio->desc.num_ints_per_process)); offset += sizeof(psl->mmio->desc.num_ints_per_process); if (client->max_irqs == 0) client->max_irqs = 2037 / psl->mmio->desc.num_of_processes; memcpy(&(buffer[offset]), (char *)&(client->max_irqs), sizeof(client->max_irqs)); offset += sizeof(client->max_irqs); memcpy(&(buffer[offset]), (char *)&(psl->mmio->desc.req_prog_model), sizeof(psl->mmio->desc.req_prog_model)); offset += sizeof(psl->mmio->desc.req_prog_model); memcpy(&(buffer[offset]), (char *)&(psl->mmio->desc.PerProcessPSA), sizeof(psl->mmio->desc.PerProcessPSA)); offset += sizeof(psl->mmio->desc.PerProcessPSA); memcpy(&(buffer[offset]), (char *)&(psl->mmio->desc.PerProcessPSA_offset), sizeof(psl->mmio->desc.PerProcessPSA_offset)); offset += sizeof(psl->mmio->desc.PerProcessPSA_offset); memcpy(&(buffer[offset]), (char *)&(psl->mmio->desc.AFU_EB_len), sizeof(psl->mmio->desc.AFU_EB_len)); offset += sizeof(psl->mmio->desc.AFU_EB_len); memcpy(&(buffer[offset]), (char *)&(psl->mmio->desc.crptr->cr_device), sizeof(psl->mmio->desc.crptr->cr_device)); offset += sizeof(psl->mmio->desc.crptr->cr_device); memcpy(&(buffer[offset]), (char *)&(psl->mmio->desc.crptr->cr_vendor), sizeof(psl->mmio->desc.crptr->cr_vendor)); offset += sizeof(psl->mmio->desc.crptr->cr_vendor); memcpy(&(buffer[offset]), (char *)&(psl->mmio->desc.crptr->cr_class), sizeof(psl->mmio->desc.crptr->cr_class)); if (put_bytes(client->fd, size, buffer, psl->dbg_fp, psl->dbg_id, client->context) < 0) { client_drop(client, PSL_IDLE_CYCLES, CLIENT_NONE); } free(buffer); }
static inline AVP_dword DATA_PARAM put_date( Serialize* sz, AVP_date* val ) { AVP_struct_date * orig = (AVP_struct_date *)val; AVP_struct_date converted; converted.wYear = ReadWordPtr( &orig->wYear ); converted.wMonth = ReadWordPtr( &orig->wMonth ); converted.wDayOfWeek = ReadWordPtr( &orig->wDayOfWeek ); converted.wDay = ReadWordPtr( &orig->wDay ); return put_bytes( sz, &converted, sizeof(converted) ); }
static inline AVP_dword DATA_PARAM put_time( Serialize* sz, AVP_time* val ) { AVP_struct_time * orig = (AVP_struct_time *) val; AVP_struct_time converted; converted.wHour = ReadWordPtr (&orig->wHour); converted.wMinute = ReadWordPtr (&orig->wMinute); converted.wSecond = ReadWordPtr (&orig->wSecond); converted.wMilliseconds = ReadWordPtr (&orig->wMilliseconds); return put_bytes (sz, &converted, sizeof(converted)); }
int report_error(const char *fmt, ...) { struct membuffer *buf = &error_string_buffer; /* Previous unprinted errors? Add a newline in between */ if (buf->len) put_bytes(buf, "\n", 1); VA_BUF(buf, fmt); mb_cstring(buf); return -1; }
/* * We have a very simple line-based interface, with the small * complication that lines can have strings in the middle, and * a string can be multiple lines. * * The UTF-8 string escaping is *very* simple, though: * * - a string starts and ends with double quotes (") * * - inside the string we escape: * (a) double quotes with '\"' * (b) backslash (\) with '\\' * * - additionally, for human readability, we escape * newlines with '\n\t', with the exception that * consecutive newlines are left unescaped (so an * empty line doesn't become a line with just a tab * on it). * * Also, while the UTF-8 string can have arbitrarily * long lines, the non-string parts of the lines are * never long, so we can use a small temporary buffer * on stack for that part. * * Also, note that if a line has one or more strings * in it: * * - each string will be represented as a single '"' * character in the output. * * - all string will exist in the same 'membuffer', * separated by NUL characters (that cannot exist * in a string, not even quoted). */ static const char *parse_one_string(const char *buf, const char *end, struct membuffer *b) { const char *p = buf; /* * We turn multiple strings one one line (think dive tags) into one * membuffer that has NUL characters in between strings. */ if (b->len) put_bytes(b, "", 1); while (p < end) { char replace; switch (*p++) { default: continue; case '\n': if (p < end && *p == '\t') { replace = '\n'; break; } continue; case '\\': if (p < end) { replace = *p; break; } continue; case '"': replace = 0; break; } put_bytes(b, buf, p - buf - 1); if (!replace) break; put_bytes(b, &replace, 1); buf = ++p; } return p; }
void handle_mem_write(struct cmd *cmd) { struct cmd_event **head; struct cmd_event *event; struct client *client; uint64_t *addr; uint8_t *buffer; uint64_t offset; // Make sure cmd structure is valid if (cmd == NULL) return; // Send any ready write data to client immediately head = &cmd->list; while (*head != NULL) { if (((*head)->type == CMD_WRITE) && ((*head)->state == MEM_RECEIVED)) break; head = &((*head)->_next); } event = *head; // Test for client disconnect if ((event == NULL) || ((client = _get_client(cmd, event)) == NULL)) return; // Check that memory request can be driven to client if (client->mem_access != NULL) return; // Send data to client and clear event to allow // the next buffer read to occur. The request will now await // confirmation from the client that the memory write was // successful before generating a response. The client // response will cause a call to either handle_aerror() or // handle_mem_return(). buffer = (uint8_t *) malloc(event->size + 10); offset = event->addr & ~CACHELINE_MASK; buffer[0] = (uint8_t) PSLSE_MEMORY_WRITE; buffer[1] = (uint8_t) event->size; addr = (uint64_t *) & (buffer[2]); *addr = htonll(event->addr); memcpy(&(buffer[10]), &(event->data[offset]), event->size); event->abort = &(client->abort); if (put_bytes(client->fd, event->size + 10, buffer, cmd->dbg_fp, cmd->dbg_id, client->context) < 0) { client_drop(client, PSL_IDLE_CYCLES, CLIENT_NONE); } debug_cmd_client(cmd->dbg_fp, cmd->dbg_id, event->tag, event->context); event->state = MEM_REQUEST; client->mem_access = (void *)event; }
int Stream::put( char const *s) { int len; NETWORK_TRACE("put string " << s); switch(_code){ case internal: case external: if (!s){ if (get_encryption()) { len = 1; if (put(len) == FALSE) { return FALSE; } } if (put_bytes(BIN_NULL_CHAR, 1) != 1) return FALSE; } else{ len = strlen(s)+1; if (get_encryption()) { if (put(len) == FALSE) { return FALSE; } } if (put_bytes(s, len) != len) return FALSE; } break; case ascii: return FALSE; } return TRUE; }
int Stream::put( unsigned char c) { getcount =0; NETWORK_TRACE("put char " << c << " c(" << ++putcount << ") "); switch(_code){ case internal: case external: case ascii: if (put_bytes(&c, 1) != 1) return FALSE; break; } return TRUE; }
/* * We're outputting utf8 in xml. * We need to quote the characters <, >, &. * * Technically I don't think we'd necessarily need to quote the control * characters, but at least libxml2 doesn't like them. It doesn't even * allow them quoted. So we just skip them and replace them with '?'. * * If we do this for attributes, we need to quote the quotes we use too. */ static void quote(struct membuffer *b, const char *text, int is_attribute) { const char *p = text; for (;;) { const char *escape; switch (*p++) { default: continue; case 0: escape = NULL; break; case 1 ... 8: case 11: case 12: case 14 ... 31: escape = "?"; break; case '<': escape = "<"; break; case '>': escape = ">"; break; case '&': escape = "&"; break; case '\'': if (!is_attribute) continue; escape = "'"; break; case '\"': if (!is_attribute) continue; escape = """; break; } put_bytes(b, text, (p - text - 1)); if (!escape) break; put_string(b, escape); text = p; } }
gssize g_obex_header_encode(GObexHeader *header, void *buf, gsize buf_len) { guint8 *ptr = buf; guint16 u16; guint32 u32; gunichar2 *utf16; glong utf16_len; g_obex_debug(G_OBEX_DEBUG_HEADER, "header 0x%02x", G_OBEX_HDR_ENC(header->id)); if (buf_len < header->hlen) return -1; ptr = put_bytes(ptr, &header->id, sizeof(header->id)); switch (G_OBEX_HDR_ENC(header->id)) { case G_OBEX_HDR_ENC_UNICODE: utf16_len = utf8_to_utf16(&utf16, header->v.string); if (utf16_len < 0 || (guint16) utf16_len > buf_len) return -1; g_assert_cmpuint(utf16_len + 3, ==, header->hlen); u16 = g_htons(utf16_len + 3); ptr = put_bytes(ptr, &u16, sizeof(u16)); ptr = put_bytes(ptr, utf16, utf16_len); g_free(utf16); break; case G_OBEX_HDR_ENC_BYTES: u16 = g_htons(header->hlen); ptr = put_bytes(ptr, &u16, sizeof(u16)); if (header->extdata) ptr = put_bytes(ptr, header->v.extdata, header->vlen); else ptr = put_bytes(ptr, header->v.data, header->vlen); break; case G_OBEX_HDR_ENC_UINT8: *ptr = header->v.u8; break; case G_OBEX_HDR_ENC_UINT32: u32 = g_htonl(header->v.u32); ptr = put_bytes(ptr, &u32, sizeof(u32)); break; default: g_assert_not_reached(); } return header->hlen; }
// Handle randomly selected memory touch void handle_touch(struct cmd *cmd) { struct cmd_event *event; struct client *client; uint8_t buffer[10]; uint64_t *addr; // Make sure cmd structure is valid if (cmd == NULL) return; // Randomly select a pending touch (or none) event = cmd->list; while (event != NULL) { if (((event->type == CMD_TOUCH) || (event->type == CMD_WRITE)) && (event->state == MEM_IDLE) && ((event->client_state != CLIENT_VALID) || !allow_reorder(cmd->parms))) { break; } event = event->_next; } // Test for client disconnect if ((event == NULL) || ((client = _get_client(cmd, event)) == NULL)) return; // Check that memory request can be driven to client if (client->mem_access != NULL) return; // Send memory touch request to client buffer[0] = (uint8_t) PSLSE_MEMORY_TOUCH; buffer[1] = (uint8_t) event->size; addr = (uint64_t *) & (buffer[2]); *addr = htonll(event->addr & CACHELINE_MASK); event->abort = &(client->abort); if (put_bytes(client->fd, 10, buffer, cmd->dbg_fp, cmd->dbg_id, event->context) < 0) { client_drop(client, PSL_IDLE_CYCLES, CLIENT_NONE); } event->state = MEM_TOUCH; client->mem_access = (void *)event; debug_cmd_client(cmd->dbg_fp, cmd->dbg_id, event->tag, event->context); }
int Stream::code_bytes(void *p, int l) { switch(_coding) { case stream_encode: return put_bytes((const void *)p, l); case stream_decode: return get_bytes(p, l); case stream_unknown: EXCEPT("ERROR: Stream::code(void *p, int l) has unknown direction!"); break; default: EXCEPT("ERROR: Stream::code(void *p, int l)'s _coding is illegal!"); break; } return FALSE; /* will never get here */ }
int Stream::put( unsigned short s) { NETWORK_TRACE("put short " << s); switch(_code){ case internal: if (put_bytes(&s, sizeof(short)) != sizeof(short)) return FALSE; break; case external: return put((unsigned int)s); case ascii: return FALSE; } return TRUE; }
int Stream::put( float f) { NETWORK_TRACE("put float " << f); switch(_code){ case internal: if (put_bytes(&f, sizeof(float)) != sizeof(float)) return FALSE; break; case external: return put((double)f); case ascii: return FALSE; } return TRUE; }
// ----------------------------------------------------------------------------- // Append a new block at the end of file (out of the range of <num_blocks_>). // The file pointer is pointed to the new appended block and return its pos. // ----------------------------------------------------------------------------- int BlockFile::append_block( // append new block at the end of file Block block) // the new block { fseek(fp_, 0, SEEK_END); // <fp_> point to the end of file put_bytes(block, block_length_);// write a <block> num_blocks_++; // add 1 to <num_blocks_> fseek(fp_, SIZEINT, SEEK_SET); // <fp_> point to pos of header fwrite_number(num_blocks_); // update <num_blocks_> // ------------------------------------------------------------------------- // <fp_> point to the pos of new added block. // The equation <act_block_> = <num_blocks_> indicates the file pointer // point to new added block. // Return index of new added block // ------------------------------------------------------------------------- fseek(fp_, -block_length_, SEEK_END); return (act_block_ = num_blocks_) - 1; }