resres.c
来自「基于4个mips核的noc设计」· C语言 代码 · 共 660 行 · 第 1/2 页
C
660 行
/* resres.c: read_res_file and write_res_file implementation for windres. Copyright 1998, 1999 Free Software Foundation, Inc. Written by Anders Norlander <anorland@hem2.passagen.se>. This file is part of GNU Binutils. 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. *//* FIXME: This file does not work correctly in a cross configuration. It assumes that it can use fread and fwrite to read and write integers. It does no swapping. */#include "bfd.h"#include "bucomm.h"#include "libiberty.h"#include "windres.h"#include <assert.h>#include <time.h>struct res_hdr { unsigned long data_size; unsigned long header_size; };static void write_res_directory PARAMS ((const struct res_directory *, const struct res_id *, const struct res_id *, int *, int));static void write_res_resource PARAMS ((const struct res_id *, const struct res_id *, const struct res_resource *, int *));static void write_res_bin PARAMS ((const struct res_resource *, const struct res_id *, const struct res_id *, const struct res_res_info *));static void write_res_id PARAMS ((const struct res_id *));static void write_res_info PARAMS ((const struct res_res_info *));static void write_res_data PARAMS ((const void *, size_t, int));static void write_res_header PARAMS ((unsigned long, const struct res_id *, const struct res_id *, const struct res_res_info *));static int read_resource_entry PARAMS ((void));static void read_res_data PARAMS ((void *, size_t, int));static void read_res_id PARAMS ((struct res_id *));static unichar *read_unistring PARAMS ((int *));static void skip_null_resource PARAMS ((void));static unsigned long get_id_size PARAMS ((const struct res_id *));static void res_align_file PARAMS ((void));static void res_add_resource PARAMS ((struct res_resource *, const struct res_id *, const struct res_id *, int, int));void res_append_resource PARAMS ((struct res_directory **, struct res_resource *, int, const struct res_id *, int));static struct res_directory *resources = NULL;static FILE *fres;static const char *filename;extern char *program_name;/* Read resource file */struct res_directory *read_res_file (fn) const char *fn;{ filename = fn; fres = fopen (filename, "rb"); if (fres == NULL) fatal ("can't open `%s' for output: %s", filename, strerror (errno)); skip_null_resource (); while (read_resource_entry ()) ; fclose (fres); return resources;}/* Write resource file */voidwrite_res_file (fn, resdir) const char *fn; const struct res_directory *resdir;{ int language; static const unsigned char sign[] = {0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; long fpos; filename = fn; fres = fopen (filename, "wb"); if (fres == NULL) fatal ("can't open `%s' for output: %s", filename, strerror (errno)); /* Write 32 bit resource signature */ write_res_data (sign, sizeof (sign), 1); /* write resources */ language = -1; write_res_directory (resdir, (const struct res_id *) NULL, (const struct res_id *) NULL, &language, 1); /* end file on DWORD boundary */ fpos = ftell (fres); if (fpos % 4) write_res_data (sign, fpos % 4, 1); fclose (fres);}/* Read a resource entry, returns 0 when all resources are read */static intread_resource_entry (void){ struct res_id type; struct res_id name; struct res_res_info resinfo; struct res_hdr reshdr; long version; void *buff; struct res_resource *r; res_align_file (); /* Read header */ if (fread (&reshdr, sizeof (reshdr), 1, fres) != 1) return 0; /* read resource type */ read_res_id (&type); /* read resource id */ read_res_id (&name); res_align_file (); /* Read additional resource header */ read_res_data (&resinfo.version, sizeof (resinfo.version), 1); read_res_data (&resinfo.memflags, sizeof (resinfo.memflags), 1); read_res_data (&resinfo.language, sizeof (resinfo.language), 1); read_res_data (&version, sizeof (version), 1); read_res_data (&resinfo.characteristics, sizeof (resinfo.characteristics), 1); res_align_file (); /* Allocate buffer for data */ buff = res_alloc (reshdr.data_size); /* Read data */ read_res_data (buff, reshdr.data_size, 1); /* Convert binary data to resource */ r = bin_to_res (type, buff, reshdr.data_size, 0); r->res_info = resinfo; /* Add resource to resource directory */ res_add_resource (r, &type, &name, resinfo.language, 0); return 1;}/* write resource directory to binary resource file */static voidwrite_res_directory (rd, type, name, language, level) const struct res_directory *rd; const struct res_id *type; const struct res_id *name; int *language; int level;{ const struct res_entry *re; for (re = rd->entries; re != NULL; re = re->next) { switch (level) { case 1: /* If we're at level 1, the key of this resource is the type. This normally duplicates the information we have stored with the resource itself, but we need to remember the type if this is a user define resource type. */ type = &re->id; break; case 2: /* If we're at level 2, the key of this resource is the name we are going to use in the rc printout. */ name = &re->id; break; case 3: /* If we're at level 3, then this key represents a language. Use it to update the current language. */ if (!re->id.named && re->id.u.id != (unsigned long) *language && (re->id.u.id & 0xffff) == re->id.u.id) { *language = re->id.u.id; } break; default: break; } if (re->subdir) write_res_directory (re->u.dir, type, name, language, level + 1); else { if (level == 3) { /* This is the normal case: the three levels are TYPE/NAME/LANGUAGE. NAME will have been set at level 2, and represents the name to use. We probably just set LANGUAGE, and it will probably match what the resource itself records if anything. */ write_res_resource (type, name, re->u.res, language); } else { fprintf (stderr, "// Resource at unexpected level %d\n", level); write_res_resource (type, (struct res_id *) NULL, re->u.res, language); } } }}static voidwrite_res_resource (type, name, res, language) const struct res_id *type; const struct res_id *name; const struct res_resource *res; int *language ATTRIBUTE_UNUSED;{ int rt; switch (res->type) { default: abort (); case RES_TYPE_ACCELERATOR: rt = RT_ACCELERATOR; break; case RES_TYPE_BITMAP: rt = RT_BITMAP; break; case RES_TYPE_CURSOR: rt = RT_CURSOR; break; case RES_TYPE_GROUP_CURSOR: rt = RT_GROUP_CURSOR; break; case RES_TYPE_DIALOG: rt = RT_DIALOG; break; case RES_TYPE_FONT: rt = RT_FONT; break; case RES_TYPE_FONTDIR: rt = RT_FONTDIR; break; case RES_TYPE_ICON: rt = RT_ICON; break; case RES_TYPE_GROUP_ICON: rt = RT_GROUP_ICON; break; case RES_TYPE_MENU: rt = RT_MENU; break; case RES_TYPE_MESSAGETABLE: rt = RT_MESSAGETABLE; break; case RES_TYPE_RCDATA: rt = RT_RCDATA; break; case RES_TYPE_STRINGTABLE: rt = RT_STRING; break; case RES_TYPE_USERDATA: rt = 0; break; case RES_TYPE_VERSIONINFO: rt = RT_VERSION; break; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?