C implements IO streams

Source: Internet
Author: User

/// Fs_stream.h // fsnet /// Created by Vincent on 14-5-22. // Copyright (c) 2014 Vincent. all rights reserved. // # ifndef define # include "fs_define.h" # define 128 struct fs_stream; struct fs_input_stream; struct fs_output_stream * struct (size_t len ); # define struct (struct) void fs_stream_free_output (struct fs_output_stream *); void Merge (struct fs_output_stream *, BYTE); void Merge (struct fs_output_stream *, uint32_t ); void Merge (struct merge *, uint16_t); void Merge (struct fs_output_stream *, int32_t); void Merge (struct fs_output_stream *, int16_t); void Merge (struct merge *, float ); void fs_stream_write_double (struct fs_output_stream *, double); void evaluate (struct fs_output_stream *, BYTE *, size_t len); const BYTE * terminate (struct fs_output_stream *); size_t evaluate (struct fs_output_stream *); size_t evaluate (struct fs_output_stream *, size_t start, size_t len); struct evaluate * terminate (const BYTE * data, size_t len ); struct fs_input_stream * struct (const BYTE * data, size_t len); void Merge (struct fs_input_stream *, const BYTE * data, size_t len); void Merge (struct fs_input_stream *, const BYTE * data, size_t len); size_t evaluate (struct fs_input_stream * stream); void Merge (struct fs_input_stream *, BYTE order); void Merge (struct fs_input_stream *); BYTE values (struct fs_input_stream *); uint32_t values (struct fs_input_stream *); uint16_t values (struct values *); int32_t values (struct values *); int16_t values (struct values *); float struct (struct fs_input_stream *); double struct (struct fs_input_stream *); size_t struct (struct *, char **); void struct (struct fs_output_stream *, fs_script_id _ id ); fs_script_id forward (struct fs_output_stream *); void forward (struct fs_input_stream *, fs_script_id _ id); fs_script_id forward (struct fs_input_stream *); # endif
/// Fs_stream.c // fsnet /// Created by Vincent on 14-5-22. // Copyright (c) Aug Vincent. All rights reserved. // # include
 
  
# Include
  
   
# Include "fs_define.h" # include "fs_stream.h" # include "attributes" # define BASE_STREAM \ BYTE byte_order; \ BYTE * data; \ struct fs_input_stream {BASE_STREAM size_t pos; size_t len; fs_bool copy; fs_script_id script_id;}; struct fs_output_stream {BASE_STREAM size_t pos; size_t buff_len; fs_script_id script_id;}; struct comment * Comment (size_t len) {struct fs_output_stream * ret = (struct fs_output_stream *) fs_malloc (sizeof (* ret); fs_zero (ret, sizeof (* ret); ret-> data = (BYTE *) fs_malloc (len); ret-> pos = 0; ret-> buff_len = len; return ret;} voidfs_stream_free_output (struct fs_output_stream * stream) {fs_free (stream-> data ); fs_free (stream);} voidfs_stream_write_byte (struct fs_output_stream * stream, BYTE v) {fs_stream_write_data (stream, (BYTE *) & v, sizeof (BYTE ));} struct (struct fs_output_stream * stream, uint32_t v) {struct (stream, (BYTE *) & v, sizeof (uint32_t);} struct (struct fs_output_stream * stream, uint16_t v) {fs_stream_write_data (stream, (BYTE *) & v, sizeof (uint16_t);} evaluate (struct fs_output_stream * stream, int32_t v) {fs_stream_write_data (stream, (BYTE *) & v, sizeof (int32_t);} evaluate (struct fs_output_stream * stream, int16_t v) {fs_stream_write_data (stream, (BYTE *) & v, sizeof (int16_t ));} struct (struct fs_output_stream * stream, float v) {fs_stream_write_data (stream, (BYTE *) & v, sizeof (float);} voidfs_stream_write_double (struct fs_output_stream * stream, double) {fs_stream_write_data (stream, (BYTE *) & v, sizeof (double);} voidfs_stream_write_data (struct fs_output_stream * stream, BYTE * data, size_t len) {while (stream-> pos + len> stream-> buff_len) {void * data = fs_realloc (stream-> data, stream-> buff_len <1 ); stream-> data = data; stream-> buff_len = stream-> buff_len <1;} memcpy (stream-> data + stream-> pos, data, len ); stream-> pos + = len;} const BYTE * fs_stream_get_dataptr (struct fs_output_stream * stream) {return stream-> data;} size_t fs_stream_get_len (struct fs_output_stream * stream) {return stream-> pos;} size_tfs_stream_output_sub (struct fs_output_stream * stream, size_t start, size_t len) {memcpy (stream-> data, stream-> data + start, len ); stream-> pos = len; return len;} struct fs_input_stream * fs_create_input_stream (const BYTE * data, size_t len) {struct fs_input_stream * ret = (struct fs_input_stream *) fs_malloc (sizeof (* ret); fs_zero (ret, sizeof (* ret); fs_input_stream_set_data (ret, data, len); return ret ;} struct fs_input_stream * substring (const BYTE * data, size_t len) {struct fs_input_stream * ret = (struct fs_input_stream *) fs_malloc (sizeof (* ret); fs_zero (ret, sizeof (* ret); fs_input_stream_set_data_copy (ret, data, len); return ret;} voidfs_input_stream_set_data (struct fs_input_stream * stream, const BYTE * data, size_t len) {if (stream-> data) {if (stream-> copy) {fs_free (stream-> data) ;}} stream-> data = (BYTE *) data; stream-> pos = 0; stream-> len = len; stream-> copy = fs_false;} voidfs_input_stream_set_data_copy (struct fs_input_stream * stream, const BYTE * data, size_t len) {if (stream-> data) {if (stream-> copy) {fs_free (stream-> data) ;}} stream-> data = (BYTE *) fs_malloc (len); stream-> pos = 0; stream-> len = len; stream-> copy = fs_true; memcpy (stream-> data, data, len );} struct (struct fs_input_stream * stream) {return stream-> len;} voidfs_input_stream_set_order (struct fs_input_stream * stream, BYTE order) {stream-> byte_order = order ;} voidfs_stream_free_input (struct fs_input_stream * stream) {if (stream-> copy) {fs_free (stream-> data);} fs_free (stream);} # define _ stream_read (TYPE) \ TYPE v = 0; \ memcpy (& v, stream-> data + stream-> pos, sizeof (v); \ stream-> pos + = sizeof (v ); \ return v; BYTE iterator (struct fs_input_stream * stream) {_ stream_read (BYTE);} uint32_t fs_stream_read_uint32 (struct fs_input_stream * stream) {_ stream_read (uint32_t );} uint16_t Merge (struct fs_input_stream * stream) {_ stream_read (uint16_t);} int32_t Merge (struct into * stream) {_ stream_read (int32_t);} int16_t Merge (struct into * stream) {_ stream_read (int16_t);} float iterator (struct fs_input_stream * stream) {_ stream_read (float);} double fs_stream_read_double (struct fs_input_stream * stream) {_ stream_read (double );} size_t fs_stream_read_string (struct fs_input_stream * stream, char ** out) {return 0;} struct (struct fs_output_stream * stream, fs_script_id _ id) {stream-> script_id = _ id ;} struct (struct fs_output_stream * stream) {return stream-> script_id;} voidfs_input_stream_set_script_id (struct fs_input_stream * stream, fs_script_id _ id) {stream-> script_id = _ id ;} fs_script_idfs_input_stream_get_script_id (struct fs_input_stream * stream) {return stream-> script_id ;}
  
 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.