⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 protect.c

📁 这是TSVQ的经典实现
💻 C
字号:
/* protect.c:   Each function in protect.c generally encapsulates a function from the   standard library.   If the protect.c function is used instead of its standard library   equivalent, then when errors occur the program will gracefully exit and   print error messages to stderr with no extra effort on the part of the   programmer.   The motivation for writing these is that it's a little bit of a pain to   write code to check for errors each time that something like malloc is   called. Since almost always my programs (which are written for research and   not production use) take only one kind of action when errors are   encountered (they exit), it's feasible to have just one routine of each   type to check for errors.  */#include "protect.h"char *prog_name = "Error: ";/* The first line in main() of a program that calls any of these routines   should be: prog_name = av[0];   "Error: " is just the default value in case this isn't done for some   reason. */double verbosity_level = 0.0;/* pr_error(): This calls perror() (see stdio.h) if errno (see errno.h) is   nonzero. Then it prints the string in fmt with the rest of the arguments   that were passed.   According to the C FAQ (ques. 12.24): "Note that it is only meaningful for   a program to inspect the contents of errno after an error has been   reported. [by one of the standard library functions]". So if pr_error() was   reached without such an error being reported, perror() could be putting out   spurious information. Take what it says with a grain of salt.  */void pr_error(char *fmt, ...){  va_list args;  if (errno != 0)    perror("\n\n\tperror says");  else fprintf(stderr,"\n\n");  va_start(args, fmt);  fprintf(stderr, "%s: ", prog_name);  vfprintf(stderr, fmt, args);  fprintf(stderr, "\n\n");  va_end(args);  exit(-1);}/* If the V is low enough this is equivalent to fprintf( stederr, fmt, ....);   followed by a newline. */void print_debug(double V, char *fmt, ...){  va_list args;  if (verbosity_level < V)    return;  va_start(args, fmt);  vfprintf(stderr, fmt, args);  fprintf(stderr, "\n");  va_end(args);}/* pr_alloc: This returns a pointer to sz bytes of memory. If fill_val is NOF,   the memory is uninitialized, otherwise each byte is filled with fill_val. */void *pr_alloc(const size_t sz, const int fill_val){  void *p;  p = malloc(sz);  if (p == NULL) pr_error("OUT OF MEMORY ERROR.\n");  if (fill_val != NOF)    memset(p, fill_val, sz);  return p;}/* pr_free(): This simulates the ANSI free() function. */void pr_free(void *p){  if (p == 0) return;  else free(p);}/* pr_realloc: This uses realloc (3) to return a pointer to sz bytes of   memory. Some of the routines that call this assume that realloc(0, sz) is   the same as malloc(sz), which is what the ANSI standard says. */void *pr_realloc(void *p, const size_t sz){  if (p == 0) return pr_alloc(sz, NOF); /* this isn't necessary in ANSI */  p = realloc(p, sz);  if (p == NULL) pr_error("out of memory error in pr_realloc().\n");  return p;}/* This writes "nobj"'s of size "size" starting from "ptr" to "stream" and   checks for errors. */void pr_fwrite(const void *ptr, size_t size, size_t nobj, FILE *stream){  int t;   if ((t = fwrite(ptr, size, nobj, stream)) != nobj)    pr_error("fwrite was asked to save %d bytes, but was only able "	     "to save %d bytes", nobj*size, t*size);   if (ferror(stream) != 0)    pr_error("ferror says there's a problem in pr_fwrite()");}/* This reads "nobj"'s of size "size" starting from "ptr" to "stream" and   checks for errors. */void pr_fread(void *ptr, size_t size, size_t nobj, FILE *stream){  int t;   if ((t = fread(ptr, size, nobj, stream)) != nobj)    pr_error("fread was asked to read %d bytes, but was only able "	     "to read %d bytes", nobj*size, t*size);   if (ferror(stream) != 0)    pr_error("ferror says there's a problem after using fread()");}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -