程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C >> 關於C >> c 實現IO流

c 實現IO流

編輯:關於C
//
//  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 
#include 
#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 = (BYTE*)fs_malloc(len);
    ret->pos = 0;
    ret->buff_len = len;
    
    return ret;
}


void
fs_stream_free_output( struct fs_output_stream* stream){
    
    fs_free(stream->data);
    fs_free(stream);
    
}

void
fs_stream_write_byte( struct fs_output_stream* stream,  BYTE v){
    fs_stream_write_data(stream, (BYTE*)&v, sizeof(BYTE));
}

void
fs_stream_write_uint32( struct fs_output_stream* stream,  uint32_t v){
    fs_stream_write_data(stream, (BYTE*)&v, sizeof(uint32_t));
}

void
fs_stream_write_uint16( struct fs_output_stream* stream,  uint16_t v){
    fs_stream_write_data(stream, (BYTE*)&v, sizeof(uint16_t));
}

void
fs_stream_write_int32( struct fs_output_stream* stream,  int32_t v){
    fs_stream_write_data(stream, (BYTE*)&v, sizeof(int32_t));
}

void
fs_stream_write_int16( struct fs_output_stream* stream,  int16_t v){
    fs_stream_write_data(stream, (BYTE*)&v, sizeof(int16_t));
}

void
fs_stream_write_float( struct fs_output_stream* stream,  float v){
    fs_stream_write_data(stream, (BYTE*)&v, sizeof(float));
}

void
fs_stream_write_double( struct fs_output_stream* stream,  double v){
    fs_stream_write_data(stream, (BYTE*)&v, sizeof(double));
}

void
fs_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_t
fs_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*
fs_create_input_stream_with_copy(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;
}


void
fs_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;
}

void
fs_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);
}

size_t
fs_input_stream_get_len( struct fs_input_stream* stream){
    return stream->len;
}

void
fs_input_stream_set_order( struct fs_input_stream* stream,  BYTE order){
    stream->byte_order = order;
}

void
fs_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 fs_stream_read_byte( 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 fs_stream_read_uint16( struct fs_input_stream* stream){
    _stream_read(uint16_t);
}
int32_t fs_stream_read_int32( struct fs_input_stream* stream){
    _stream_read(int32_t);
}
int16_t fs_stream_read_int16( struct fs_input_stream* stream){
    _stream_read(int16_t);
}
float fs_stream_read_float( 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;
}




void
fs_output_stream_set_script_id( struct fs_output_stream* stream, fs_script_id _id ){
    stream->script_id = _id;
}

fs_script_id
fs_output_stream_get_script_id( struct fs_output_stream* stream){
    return stream->script_id;
}

void
fs_input_stream_set_script_id( struct fs_input_stream* stream, fs_script_id _id ){
    stream->script_id = _id;
}


fs_script_id
fs_input_stream_get_script_id( struct fs_input_stream* stream){
    return stream->script_id;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved