Blender V2.61 - r43446

idcode.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00019  * All rights reserved.
00020  *
00021  * The Original Code is: all of this file.
00022  *
00023  * Contributor(s): none yet.
00024  *
00025  * ***** END GPL LICENSE BLOCK *****
00026  * return info about ID types
00027  */
00028 
00034 #include <stdlib.h>
00035 #include <string.h>
00036 
00037 #include "DNA_ID.h"
00038 
00039 #include "BKE_idcode.h"
00040 
00041 typedef struct {
00042     unsigned short code;
00043     const char *name, *plural;
00044     
00045     int flags;
00046 #define IDTYPE_FLAGS_ISLINKABLE (1<<0)
00047 } IDType;
00048 
00049 /* plural need to match rna_main.c's MainCollectionDef */
00050 static IDType idtypes[]= {
00051     { ID_AC,        "Action",   "actions",      IDTYPE_FLAGS_ISLINKABLE}, 
00052     { ID_AR,        "Armature", "armatures",    IDTYPE_FLAGS_ISLINKABLE}, 
00053     { ID_BR,        "Brush",    "brushes",      IDTYPE_FLAGS_ISLINKABLE}, 
00054     { ID_CA,        "Camera",   "cameras",      IDTYPE_FLAGS_ISLINKABLE}, 
00055     { ID_CU,        "Curve",    "curves",       IDTYPE_FLAGS_ISLINKABLE}, 
00056     { ID_GD,        "GPencil",  "grease_pencil",IDTYPE_FLAGS_ISLINKABLE},  /* rename gpencil */
00057     { ID_GR,        "Group",    "groups",       IDTYPE_FLAGS_ISLINKABLE}, 
00058     { ID_ID,        "ID",       "ids",          0}, /* plural is fake */
00059     { ID_IM,        "Image",    "images",       IDTYPE_FLAGS_ISLINKABLE}, 
00060     { ID_IP,        "Ipo",      "ipos",         IDTYPE_FLAGS_ISLINKABLE},  /* deprecated */
00061     { ID_KE,        "Key",      "keys",         0}, 
00062     { ID_LA,        "Lamp",     "lamps",        IDTYPE_FLAGS_ISLINKABLE}, 
00063     { ID_LI,        "Library",  "libraries",    0}, 
00064     { ID_LT,        "Lattice",  "lattices",     IDTYPE_FLAGS_ISLINKABLE}, 
00065     { ID_MA,        "Material", "materials",    IDTYPE_FLAGS_ISLINKABLE}, 
00066     { ID_MB,        "Metaball", "metaballs",    IDTYPE_FLAGS_ISLINKABLE}, 
00067     { ID_ME,        "Mesh",     "meshes",       IDTYPE_FLAGS_ISLINKABLE}, 
00068     { ID_NT,        "NodeTree", "node_groups",  IDTYPE_FLAGS_ISLINKABLE}, 
00069     { ID_OB,        "Object",   "objects",      IDTYPE_FLAGS_ISLINKABLE}, 
00070     { ID_PA,        "ParticleSettings", "particles", 0},
00071     { ID_SCE,       "Scene",    "scenes",       IDTYPE_FLAGS_ISLINKABLE}, 
00072     { ID_SCR,       "Screen",   "screens",      0}, 
00073     { ID_SEQ,       "Sequence", "sequences",    0}, /* not actually ID data */
00074     { ID_SPK,       "Speaker",  "speakers",     IDTYPE_FLAGS_ISLINKABLE},
00075     { ID_SO,        "Sound",    "sounds",       IDTYPE_FLAGS_ISLINKABLE},
00076     { ID_TE,        "Texture",  "textures",     IDTYPE_FLAGS_ISLINKABLE}, 
00077     { ID_TXT,       "Text",     "texts",        IDTYPE_FLAGS_ISLINKABLE}, 
00078     { ID_VF,        "VFont",    "fonts",        IDTYPE_FLAGS_ISLINKABLE}, 
00079     { ID_WO,        "World",    "worlds",       IDTYPE_FLAGS_ISLINKABLE}, 
00080     { ID_WM,        "WindowManager", "window_managers", 0}, 
00081     { ID_MC,        "MovieClip", "movieclips",  IDTYPE_FLAGS_ISLINKABLE},
00082 };
00083 static int nidtypes= sizeof(idtypes)/sizeof(idtypes[0]);
00084 
00085 static IDType *idtype_from_name(const char *str) 
00086 {
00087     int i= nidtypes;
00088     
00089     while (i--)
00090         if (strcmp(str, idtypes[i].name)==0)
00091             return &idtypes[i];
00092 
00093     return NULL;
00094 }
00095 static IDType *idtype_from_code(int code) 
00096 {
00097     int i= nidtypes;
00098     
00099     while (i--)
00100         if (code==idtypes[i].code)
00101             return &idtypes[i];
00102     
00103     return NULL;
00104 }
00105 
00106 int BKE_idcode_is_valid(int code) 
00107 {
00108     return idtype_from_code(code)?1:0;
00109 }
00110 
00111 int BKE_idcode_is_linkable(int code)
00112 {
00113     IDType *idt= idtype_from_code(code);
00114     return idt?(idt->flags&IDTYPE_FLAGS_ISLINKABLE):0;
00115 }
00116 
00117 const char *BKE_idcode_to_name(int code) 
00118 {
00119     IDType *idt= idtype_from_code(code);
00120     
00121     return idt?idt->name:NULL;
00122 }
00123 
00124 int BKE_idcode_from_name(const char *name) 
00125 {
00126     IDType *idt= idtype_from_name(name);
00127     
00128     return idt?idt->code:0;
00129 }
00130 
00131 const char *BKE_idcode_to_name_plural(int code) 
00132 {
00133     IDType *idt= idtype_from_code(code);
00134     
00135     return idt?idt->plural:NULL;
00136 }
00137 
00138 int BKE_idcode_iter_step(int *index)
00139 {
00140     return (*index < nidtypes) ? idtypes[(*index)++].code : 0;
00141 }