首页 > 代码库 > c 实现IO流

c 实现IO流

//
//  fs_stream.h
//  fsnet
//
//  Created by Vincent on 14-5-22.
//  Copyright (c) 2014年 Vincent. All rights reserved.
//

#ifndef fsnet_fs_stream_h
#define fsnet_fs_stream_h

#include "fs_define.h"

#define OUTPUT_STRAEM_DEFAULT_MAX 128

struct fs_stream;
struct fs_input_stream;
struct fs_output_stream;


struct fs_output_stream* fs_create_output_stream(size_t len);
#define fs_create_output_stream_ext fs_create_output_stream(OUTPUT_STRAEM_DEFAULT_MAX)
void fs_stream_free_output( struct fs_output_stream* );
void fs_stream_write_byte( struct fs_output_stream*,  BYTE);
void fs_stream_write_uint32( struct fs_output_stream*,  uint32_t);
void fs_stream_write_uint16( struct fs_output_stream*,  uint16_t);
void fs_stream_write_int32( struct fs_output_stream*,  int32_t);
void fs_stream_write_int16( struct fs_output_stream*,  int16_t);
void fs_stream_write_float( struct fs_output_stream*,  float );
void fs_stream_write_double( struct fs_output_stream*,  double );
void fs_stream_write_data( struct fs_output_stream*,  BYTE*, size_t len );
const BYTE* fs_stream_get_dataptr( struct fs_output_stream* );
size_t fs_stream_get_len( struct fs_output_stream* );
size_t fs_stream_output_sub( struct fs_output_stream*, size_t start, size_t len );



struct fs_input_stream* fs_create_input_stream(const BYTE* data, size_t len);
struct fs_input_stream* fs_create_input_stream_with_copy(const BYTE* data, size_t len);

void fs_input_stream_set_data( struct fs_input_stream*, const BYTE* data, size_t len);
void fs_input_stream_set_data_copy( struct fs_input_stream*, const BYTE* data, size_t len);
size_t fs_input_stream_get_len( struct fs_input_stream* stream);
void fs_input_stream_set_order( struct fs_input_stream*,  BYTE order);
void fs_stream_free_input( struct fs_input_stream* );
BYTE fs_stream_read_byte( struct fs_input_stream* );
uint32_t fs_stream_read_uint32( struct fs_input_stream* );
uint16_t fs_stream_read_uint16( struct fs_input_stream* );
int32_t fs_stream_read_int32( struct fs_input_stream* );
int16_t fs_stream_read_int16( struct fs_input_stream* );
float fs_stream_read_float( struct fs_input_stream* );
double fs_stream_read_double( struct fs_input_stream* );
size_t fs_stream_read_string( struct fs_input_stream* , char**);


void         fs_output_stream_set_script_id( struct fs_output_stream* , fs_script_id _id );
fs_script_id fs_output_stream_get_script_id( struct fs_output_stream* );
void         fs_input_stream_set_script_id( struct fs_input_stream* , fs_script_id _id );
fs_script_id fs_input_stream_get_script_id( struct fs_input_stream* );

#endif
//
//  fs_stream.c
//  fsnet
//
//  Created by Vincent on 14-5-22.
//  Copyright (c) 2014年 Vincent. All rights reserved.
//

#include <stdio.h>
#include <string.h>
#include "fs_define.h"
#include "fs_stream.h"
#include "fs_malloc.h"

#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 fs_output_stream*
fs_create_output_stream(size_t len){
    
    struct fs_output_stream* ret = (struct fs_output_stream*)fs_malloc(sizeof(*ret));
    fs_zero(ret, sizeof(*ret));
    ret->data = http://www.mamicode.com/(BYTE*)fs_malloc(len);>