/* librock exec/struct/vsbrf.c librock_CHISEL _summary Read an entire file into a librock_VSB_s */ /* Copyright (c) 2001-2002, Forrest J. Cavalier III, doing business as Mib Software, Saylorsburg Pennsylvania USA Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifdef librock_NOTLIBROCK /**************************************************************/ /* ABOUT THIS FILE: GUIDE TO QUICK REUSE WITHOUT REWORK This file uses many preprocessor conditional blocks and features to publish http://www.mibsoftware.com/librock/ You can disable those features with little or no editing, and reuse this file: 1. At compile time, enable this conditional block by defining the preprocessor symbol librock_NOTLIBROCK, either with a compiler command line parameter, or as a #define in a source file which then #includes this source file. 2. Define any preprocessor symbols in this block (if any) appropriately for your machine and compilation environment. 3. Copy and use the declarations from this block in your source files as appropriate. This file is originally from the librock library, which is Free (libre), free (no cost), rock-stable API, and works on gcc/MSVC/Windows/Linux/BSD/more. Get originals, updates, license certificates, more, at http://www.mibsoftware.com/librock/ (Change log appears at end of this file.) */ #define librock_ISOLATED #define librock_PTR #define librock_CONST const #ifdef _MSC_VER #include #define librock_O_BINARY O_BINARY #else #define librock_O_BINARY 0 #endif #include #include #include /**************************************************************/ #endif #ifndef librock_ISOLATED /**************************************************************/ #define librock_IMPLEMENT_vsbrf #include #include #include /**************************************************************/ #endif #ifdef librock_IMPL_LIDESC #ifndef librock_NOIMPL_LIDESC_vsbrf /**************************************************************/ #include /* librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ void *librock_LIDESC_vsbrf[] = { "\n" __FILE__ librock_LIDESC_librock "\n", 0 }; /**************************************************************/ #endif #endif #ifndef librock_WRAP /**************************************************************/ #define librock_body_VSBreadfile librock_VSBreadfile /**************************************************************/ #endif #ifndef librock_NOIMPL_VSBreadfile /**************************************************************/ const char *librock_body_VSBreadfile(struct librock_VSB_s *v,char *filename) {/* Copyright 1998-2001, Forrest J. Cavalier III d-b-a Mib Software Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License, originals, details: http://www.mibsoftware.com/librock/ */ #ifdef librock_MANUAL_VSBreadfile /* librock_VSBreadfile - Read the contents of a file into a librock_VSB. */ /**/ #include const char * librock_VSBreadfile(struct librock_VSB_s *v, /* Will be VSBrealloc'ed */ char *filename); /* file path */ /* Reallocate and load in a file. Returns 0 on success, and v->buf will contain the contents of the file, v->inbuf is set to the size of the file. For text processing convenience, a '\0' is in the buffer just beyond the last character from the file. The file is opened with open() using (O_RDONLY|librock_O_BINARY) flags. On failure, one the following error explanation strings is returned; "E-1-Could not open" "E-2-Could not fstat" "E-3-Could not realloc" "E-4-Read Error. Incomplete." Typical use is to initialize a memory block with the contents of a file. */ #ifdef librock_TYPICAL_USE_VSBreadfile #include #include struct librock_VSB_s *v; const char *err; v = librock_VSBalloc(); err = librock_VSBreadfile(v,"exec/struct/vsbrf.c"); if (err) { printf("%s\n",err); switch(atoi(err+1)) { case 1: /* "E-1-Could not open" */ break; case 2: /* "E-2-Could not fstat" */ break; case 3: /* "E-3-Could not realloc" */ break; case 4: /* "E-4-Read Error" */ break; } } else { printf("Read size: %d\n",v->inbuf); } librock_VSBfree(v); #endif /* fstat open close read librock_CHISEL _usesrc "exec/struct/vsb.c" librock_VSBrealloc */ /* Copyright 1998-2002 Forrest J. Cavalier III, http://www.mibsoftware.com Licensed under BSD-ish license, NO WARRANTY. Copies must retain this block. License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */ #endif /* MANUAL */ /*-------------------------------------------*/ struct stat Sb; int fd; /* Open the file, get its size. */ if ((fd = open(filename, O_RDONLY|librock_O_BINARY)) < 0) { return "E-1-Could not open"; } if (fstat(fd, &Sb) < 0) { close(fd); return "E-2-Could not fstat"; } if (!librock_VSBrealloc(v,Sb.st_size+1)) { close(fd); return "E-3-Could not realloc"; } v->inbuf = Sb.st_size; if (read(fd, v->buf, Sb.st_size) != Sb.st_size) { close(fd); return "E-4-Read error. Incomplete."; } (v->buf)[Sb.st_size] = '\0'; close(fd); return 0; } /**************************************************************/ #endif /* NOIMP section */ /* $Log: vsbrf.c,v $ Revision 1.5 2002/08/02 03:48:22 forrest@mibsoftware.com rights=#1 Updated TYPICAL_USE section. Added NOTLIBROCK section. Moved CVS log to end. Changed LIDESC MD5 to HC. Revision 1.4 2002/04/09 03:33:28 forrest@mibsoftware.com rights=#1 Updated LICENSE in manual. Revision 1.3 2002/02/06 14:09:36 forrest@mibsoftware.com rights=#1 Standardized chg log Revision 1.2 2002/01/30 16:07:56 forrest@mibsoftware.com rights=#1 Renamed some .h files Revision 1.1 2002/01/29 14:24:38 forrest@mibsoftware.com rights=#1 initial rights#1 Copyright (c) Forrest J Cavalier III d-b-a Mib Software rights#1 License text in librock_LIDESC_HC=12440211096131f5976d36be0cddca4cd9152e45 */