Skip to content
Snippets Groups Projects
Commit 139d5b73 authored by Christophe Geuzaine's avatar Christophe Geuzaine
Browse files

Replace fwrite with safe_fwrite
parent ef27886d
Branches
Tags
No related merge requests found
// $Id: List.cpp,v 1.23 2003-01-23 20:19:17 geuzaine Exp $ // $Id: List.cpp,v 1.24 2003-02-10 18:57:02 geuzaine Exp $
// //
// Copyright (C) 1997 - 2003 C. Geuzaine, J.-F. Remacle // Copyright (C) 1997 - 2003 C. Geuzaine, J.-F. Remacle
// //
...@@ -27,6 +27,7 @@ ...@@ -27,6 +27,7 @@
#include "Malloc.h" #include "Malloc.h"
#include "List.h" #include "List.h"
#include "Message.h" #include "Message.h"
#include "SafeIO.h"
static char *startptr; static char *startptr;
...@@ -450,7 +451,7 @@ void List_WriteToFile(List_T *liste, FILE *file, int format){ ...@@ -450,7 +451,7 @@ void List_WriteToFile(List_T *liste, FILE *file, int format){
Msg(GERROR, "Bad type of data to write list to file (size = %d)", liste->size); Msg(GERROR, "Bad type of data to write list to file (size = %d)", liste->size);
break; break;
case LIST_FORMAT_BINARY : case LIST_FORMAT_BINARY :
fwrite(liste->array, liste->size, n, file); safe_fwrite(liste->array, liste->size, n, file);
break; break;
default : default :
Msg(GERROR, "Unknown list format"); Msg(GERROR, "Unknown list format");
......
# $Id: Makefile,v 1.16 2002-05-18 16:31:16 geuzaine Exp $ # $Id: Makefile,v 1.17 2003-02-10 18:57:02 geuzaine Exp $
# #
# Makefile for "libGmshDataStr.a" # Makefile for "libGmshDataStr.a"
# #
...@@ -20,6 +20,7 @@ CFLAGS = $(OPT_FLAGS) $(OS_FLAGS) $(VERSION_FLAGS) $(INCLUDE) ...@@ -20,6 +20,7 @@ CFLAGS = $(OPT_FLAGS) $(OS_FLAGS) $(VERSION_FLAGS) $(INCLUDE)
SRC = List.cpp \ SRC = List.cpp \
Malloc.cpp \ Malloc.cpp \
SafeIO.cpp \
Tree.cpp \ Tree.cpp \
avl.cpp \ avl.cpp \
Tools.cpp Tools.cpp
...@@ -47,8 +48,9 @@ depend: ...@@ -47,8 +48,9 @@ depend:
$(RM) Makefile.new $(RM) Makefile.new
# DO NOT DELETE THIS LINE # DO NOT DELETE THIS LINE
List.o: List.cpp Malloc.h List.h ../Common/Message.h List.o: List.cpp Malloc.h List.h SafeIO.h ../Common/Message.h
Malloc.o: Malloc.cpp Malloc.h ../Common/Message.h Malloc.o: Malloc.cpp Malloc.h ../Common/Message.h
SafeIO.o: SafeIO.cpp SafeIO.h ../Common/Message.h
Tree.o: Tree.cpp Malloc.h Tree.h avl.h ../Common/Message.h Tree.o: Tree.cpp Malloc.h Tree.h avl.h ../Common/Message.h
avl.o: avl.cpp avl.h Malloc.h avl.o: avl.cpp avl.h Malloc.h
Tools.o: Tools.cpp Tools.h List.h Tree.h avl.h Tools.o: Tools.cpp Tools.h List.h Tree.h avl.h
// $Id: SafeIO.cpp,v 1.1 2003-02-10 18:57:02 geuzaine Exp $
//
// Copyright (C) 1997 - 2003 C. Geuzaine, J.-F. Remacle
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// Please report all bugs and problems to "gmsh@geuz.org".
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "SafeIO.h"
#include "Message.h"
/*
Safe fprintf routine
In a perfect world, one should use this routine. Unfortunately, it
is very, very slow.
We should use a macro, i.e.
#define gprintf(...) { fprintf(__VA_ARGS__); ... }
but but var args don't work with all C preprocessors.
Bottom line: don't use safe_fprintf.
*/
int safe_fprintf(FILE *stream, char *fmt, ...){
va_list args;
va_start (args, fmt);
vfprintf(stream, fmt, args);
va_end (args);
if(ferror(stream)){
Msg(GERROR, strerror(errno));
clearerr(stream);
return 1;
}
return 0;
}
/* Safe fwrite routine */
int safe_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream){
size_t result = fwrite(ptr, size, nmemb, stream);
if(result < nmemb){
if(result >= 0) /* Partial write */
Msg(GERROR, "Disk full");
else
Msg(GERROR, strerror(errno));
Msg(GERROR, "Truncating output file");
if(fflush(stream) < 0)
Msg(GERROR, "EOF reached");
if(ftruncate(fileno(stream), 0) < 0)
Msg(GERROR, strerror(errno));
if(fclose(stream) < 0)
Msg(GERROR, strerror(errno));
return 1;
}
return 0;
}
#ifndef _SAFE_IO_H_
// Copyright (C) 1997 - 2003 C. Geuzaine, J.-F. Remacle
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA.
//
// Please report all bugs and problems to "gmsh@geuz.org".
#include <stdarg.h>
#include <stdio.h>
int safe_fprintf(FILE *stream, char *fmt, ...);
int safe_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment