Beispiel #1
0
/*
 * Open the library of the given name and fill in the given library
 * structure.  Return TRUE if successful, FALSE otherwise.
 */
boolean
open_library(const char *lib_name, library * lp)
{
    boolean status = FALSE;

    lp->fdata = fopen_datafile(lib_name, RDBMODE, DATAPREFIX);
    if (lp->fdata) {
        if (readlibdir(lp)) {
            status = TRUE;
        } else {
            fclose(lp->fdata);
            lp->fdata = NULL;
        }
    }
    return status;
}
Beispiel #2
0
void sdlgl_load_face_dirs(const char *filename, char *face_dirs)
{
  FILE *fp;

  int mon = 0;
  int ch;

#ifdef FILE_AREAS
  fp = fopen_datafile_area(FILE_AREA_SHARE, filename, RDTMODE, FALSE);
#else
  fp = fopen_datafile(filename, RDTMODE, FALSE);
#endif
  if (!fp)
  {
    sdlgl_error("Failed to open file: %s\n", filename);
    return;  /* NOT REACHED */
  }

  while ((ch = fgetc(fp)) != EOF)
  {
    ch = highc(ch);

    if (isspace(ch) || !isprint(ch))
      continue;

    if (ch != '.' && ch != 'L' && ch != 'R')
    {
      sdlgl_warning("Ignoring bad character `%c' in face file: %s\n",
          ch, filename);
      continue;
    }

    face_dirs[mon++] = ch;

    if (mon >= SIZE(tile_16_face_dirs))
    {
      sdlgl_warning("Too many monsters in face file: %s\n", filename);
      break;
    }
  }

  fclose(fp);
}
Beispiel #3
0
/*ARGSUSED*/
void paniclog(const char *type,   /* panic, impossible, trickery */
	      const char *reason) /* explanation */
{
#ifdef PANICLOG
	FILE *lfile;
	char buf[BUFSZ];

	if (!program_state.in_paniclog) {
		program_state.in_paniclog = 1;
		lfile = fopen_datafile(PANICLOG, "a", TROUBLEPREFIX);
		if (lfile) {
		    fprintf(lfile, "%s %08ld: %s %s\n",
				   version_string(buf), yyyymmdd((time_t)0L),
				   type, reason);
		    fclose(lfile);
		}
		program_state.in_paniclog = 0;
	}
#endif /* PANICLOG */
	return;
}
Beispiel #4
0
dlb *
dlb_fopen(const char *name, const char *mode)
{
    FILE *fp;
    dlb *dp;

    if (!dlb_initialized)
        return NULL;

    dp = malloc(sizeof (dlb));
    if (do_dlb_fopen(dp, name, mode))
        dp->fp = NULL;
    else if ((fp = fopen_datafile(name, mode, DATAPREFIX)) != 0)
        dp->fp = fp;
    else {
        /* can't find anything */
        free(dp);
        dp = NULL;
    }

    return dp;
}
Beispiel #5
0
const char *
begin_dump(int how)
{
    const char *timestamp, *dumpname, *status, *rolename;
    time_t t;
    struct tm *tmp;

    /* back up the window procs */
    winprocs_original = windowprocs;

    /* Make a timestamp like "2011-11-30 18:45:00".  This now uses UTC time, in
       accordance with the timebase rules (in particular, we never look at the
       system timezone). This also avoids clashes when there are two games an
       hour apart and DST changed in between. (It doesn't help when there are
       two games in the same second, but that only happens as a result of
       extreme startscumming.) */
    t = (time_t)(utc_time() / 1000000LL);
    tmp = gmtime(&t);
    if (tmp)
        timestamp = msgstrftime(TIMESTAMP_FORMAT, tmp);
    else
        timestamp = "unknown time"; /* previously "???" but that's illegal
                                       on many filesystems */

    switch (how) {
    case ASCENDED:
        status = "ascended";
        break;
    case QUIT:
        status = "quit";
        break;
    case ESCAPED:
        status = "escaped";
        break;
    default:
        status = "died";
        break;
    }

    dumpname = msgprintf("%s, %s-%s-%s-%s-%s, %s.txt",
                         timestamp, u.uplname, urole.filecode, urace.filecode,
                         genders[u.ufemale].filecode,
                         aligns[1 - u.ualign.type].filecode, status);
    dumpfp = fopen_datafile(dumpname, "w+", DUMPPREFIX);
    if (!dumpfp)
        return NULL;

#ifdef UNIX
    fchmod(fileno(dumpfp), 0644);
#endif

    rolename = (u.ufemale && urole.name.f) ? urole.name.f : urole.name.m;
    fprintf(dumpfp, "%s, %s %s %s %s\n", u.uplname,
            aligns[1 - u.ualign.type].adj, genders[u.ufemale].adj,
            urace.adj, rolename);

    dump_screen(dumpfp);
    dump_status();

    return dumpname;
}
Beispiel #6
0
unsigned char *sdlgl_load_png_file(const char *filename,
    int *image_w, int *image_h)
{
  /* -AJA- all these volatiles here may seem strange.  They are needed
   * because the ANSI C standard (which GCC adheres to) says that when
   * setjmp/longjmp is being used, only volatile local variables are
   * guaranteed to keep their state if longjmp() gets called.
   */
  FILE * volatile fp = NULL;
  unsigned char * volatile image_dat = NULL;
  png_bytep * volatile row_pointers = NULL;

  /* we take the address of these two, so we shouldn't need the
   * volatile.  (GCC complains about discarding qualifiers if the
   * volatile is there).
   */
  png_structp /*volatile*/ png_ptr = NULL;
  png_infop   /*volatile*/ info_ptr = NULL;

  char sig_buf[CHECK_PNG_BYTES];
  png_uint_32 width, height;
  int bit_depth, color_type, interlace_type;
  int row, stride;

  /* open the prospective PNG file */
#ifdef FILE_AREAS
  fp = fopen_datafile_area(FILE_AREA_SHARE, filename, RDBMODE, FALSE);
#else
  fp = fopen_datafile(filename, RDBMODE, FALSE);
#endif
  if (!fp)
  {
    sdlgl_warning("Failed to open file: %s\n", filename);
    return NULL;
  }

  /* read in some of the signature bytes */
  if (fread(sig_buf, 1, CHECK_PNG_BYTES, fp) != CHECK_PNG_BYTES)
  {
    sdlgl_warning("Failed to read from file: %s\n", filename);
    goto failed;
  }

  /* compare the first CHECK_PNG_BYTES bytes of the signature */
  if (png_sig_cmp(sig_buf, (png_size_t)0, CHECK_PNG_BYTES) != 0)
  {
    sdlgl_warning("File is not a PNG file: %s\n", filename);
    goto failed;
  }

  /* pass NULLs for the error functions -- thus use the setjump stuff
   */
  png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

  if (png_ptr == NULL)
  {
    sdlgl_warning("Problem within LibPNG (no memory ?)\n");
    goto failed;
  }

  /* allocate/initialize the memory for image information */
  info_ptr = png_create_info_struct(png_ptr);
  if (info_ptr == NULL)
  {
    sdlgl_warning("Out of memory with LibPNG\n");
    goto failed;
  }

  /* set error handling since we are using the setjmp/longjmp method
   * (this is the normal method of doing things with libpng).
   */
  if (setjmp(png_ptr->jmpbuf))
  {
    sdlgl_warning("Problem within LibPNG (unknown)\n");
    goto failed;
  }

  /* set up the input control since we're using standard C streams */
  png_init_io(png_ptr, fp);
  png_set_sig_bytes(png_ptr, CHECK_PNG_BYTES);

  /* the call to png_read_info() gives us all of the information from the
   * PNG file before the first IDAT (image data chunk)
   */
  png_read_info(png_ptr, info_ptr);

  png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth,
      &color_type, &interlace_type, NULL, NULL);

  *image_w = (int)width;
  *image_h = (int)height;

  /* tell libpng to strip 16 bit/color down to 8 bits/color */
  png_set_strip_16(png_ptr);

  /* expand paletted colors into true RGB triplets */
  if (color_type == PNG_COLOR_TYPE_PALETTE ||
      color_type == PNG_COLOR_TYPE_GRAY ||
      png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
  {
     png_set_expand(png_ptr);
  }

  /* set alpha position and filler value */
  png_set_filler(png_ptr, 0xFF, PNG_FILLER_AFTER);
 
  /* let all the above calls take effect */
  png_read_update_info(png_ptr, info_ptr);
  
  /* allocate the memory for the image */
  stride = png_get_rowbytes(png_ptr, info_ptr);

  image_dat = (unsigned char *) alloc(height * stride);
  row_pointers = (png_bytep *) alloc(height * sizeof(png_bytep));
   
  for (row=0; row < height; row++)
  {
     row_pointers[row] = image_dat + row * stride;
  }

  /* now read in the image.  Yeah baby ! */
  png_read_image(png_ptr, row_pointers);
  png_read_end(png_ptr, info_ptr);

  /* free stuff & close the file */
  png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
  free(row_pointers);
  fclose(fp);

  return image_dat;

  /* -AJA- Normally I don't like gotos.  In this situation where there
   * are lots of points of possible failure and a growing set of
   * things to be undone, it makes for nicer code.
   */
failed:

  if (image_dat)
    free(image_dat);
  
  if (png_ptr)
  {
    /* assume NULLs not allowed (png docs don't say, bad bad) */
    if (info_ptr)
      png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
    else
      png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
  }

  if (row_pointers)
    free(row_pointers);

  if (fp)
    fclose(fp);
  
  return NULL;
}