📄 write_bin.c
字号:
/* Copyright (C) 2004,2005,2006 Bull S.A. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #if HAVE_CONFIG_H#include <config.h>#endif#include <string.h>#include <stdio.h>#include <stdlib.h>#include <assert.h>#include "types.h"#include "debug.h"#define PTT_SIZE (ptt_buf->size_buff)#define PTT_SZ_WORD (sizeof(PTT_WORD))extern struct ptt_buffer * ptt_buf;static size_t file_size; /* size of file = file_size / 4 */static FILE *stream = NULL;unsigned int max_size = 0;unsigned int max_file = 0; /* 0 : no limit, >0 : the number of file to keep */void close_bin () { if (!stream) return; if (fclose (stream) != 0) { perror("fclose failed"); abort(); }}static void new_file (char *binary, ptt_header_t *header) { char arch_size = sizeof (void*); /* add 10 for writing an int, 4 for .bin, 1 for '-', 1 for \0 */ char file [strlen (binary)+16]; close_bin(); if (max_file && header->file_number > max_file) { sprintf (file, "%s-%04i.bin", binary, header->file_number - max_file); remove (file); } if (max_size) { header->file_number++; sprintf (file, "%s-%04i.bin", binary, header->file_number); } else sprintf (file, "%s.bin", binary); /* create and open the binary file */ if ((stream = fopen (file, "wb")) == NULL) { perror("fopen failed"); abort(); } /* write arch size * can't write it in the struct, because the sizeof the struct depend of * the architecture */ if(fwrite (&arch_size, 1, 1, stream) != 1) { perror("fwrite failed"); assert(0); } /* write the header in the text file */ if (fwrite (header, sizeof (ptt_header_t), 1, stream) != 1) { perror("fwrite failed"); assert(0); } file_size = 0;}static inline void write_trace (size_t start, size_t nmemb) { size_t write_size = fwrite ((const void*)(ptt_buf->array + start), PTT_SZ_WORD, nmemb, stream); if (write_size != nmemb) { perror("fwrite failed"); assert(0); } PTT_TRACE_INTERNALS (PTT_DBG_WRITE, "we wrote %u data\n", write_size); file_size += write_size;}/* copy data from the shared buffer to the binary file * - stream: to choose the binary file * - beg: index where the copy begins * - end: index where the copy finishes */unsigned int write_bin (char *binary, ptt_header_t *header, unsigned int beg, unsigned int end) { size_t start = beg % PTT_SIZE; size_t stop = end % PTT_SIZE;#if __WORDSIZE == 64 PTT_TRACE_INTERNALS (PTT_DBG_WRITE, "writting between beg=%u and end=%u, " "start=%lu and stop=%lu\n", beg, end, start, stop);#else PTT_TRACE_INTERNALS (PTT_DBG_WRITE, "writting between beg=%u and end=%u, " "start=%u and stop=%u\n", beg, end, start, stop);#endif if (((max_size) && (file_size*PTT_SZ_WORD > max_size)) || (!stream)) new_file (binary, header); if (stop < start) { write_trace (start, PTT_SIZE - start); if (stop != 0) write_trace (0, stop); return (PTT_SIZE - start + stop); } else { write_trace (start, stop - start); return stop - start; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -