6c8b5329711949fadc9e891ca8306809866ceae8
[blender.git] / source / blender / blenkernel / intern / idcode.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * return info about ID types
29  */
30  
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "DNA_ID.h"
35
36 #include "BKE_idcode.h"
37
38 typedef struct {
39         unsigned short code;
40         const char *name, *plural;
41         
42         int flags;
43 #define IDTYPE_FLAGS_ISLINKABLE (1<<0)
44 } IDType;
45
46 /* plural need to match rna_main.c's MainCollectionDef */
47 static IDType idtypes[]= {
48         { ID_AC,                "Action",       "actions",              IDTYPE_FLAGS_ISLINKABLE}, 
49         { ID_AR,                "Armature", "armatures",        IDTYPE_FLAGS_ISLINKABLE}, 
50         { ID_BR,                "Brush",        "brushes",              IDTYPE_FLAGS_ISLINKABLE}, 
51         { ID_CA,                "Camera",       "cameras",              IDTYPE_FLAGS_ISLINKABLE}, 
52         { ID_CU,                "Curve",        "curves",               IDTYPE_FLAGS_ISLINKABLE}, 
53         { ID_GD,                "GPencil",      "grease_pencil",IDTYPE_FLAGS_ISLINKABLE},  /* rename gpencil */
54         { ID_GR,                "Group",        "groups",               IDTYPE_FLAGS_ISLINKABLE}, 
55         { ID_ID,                "ID",           "ids",                  0}, /* plural is fake */
56         { ID_IM,                "Image",        "images",               IDTYPE_FLAGS_ISLINKABLE}, 
57         { ID_IP,                "Ipo",          "ipos",                 IDTYPE_FLAGS_ISLINKABLE},  /* deprecated */
58         { ID_KE,                "Key",          "keys",                 0}, 
59         { ID_LA,                "Lamp",         "lamps",                IDTYPE_FLAGS_ISLINKABLE}, 
60         { ID_LI,                "Library",      "libraries",    0}, 
61         { ID_LT,                "Lattice",      "lattices",             IDTYPE_FLAGS_ISLINKABLE}, 
62         { ID_MA,                "Material", "materials",        IDTYPE_FLAGS_ISLINKABLE}, 
63         { ID_MB,                "Metaball", "metaballs",        IDTYPE_FLAGS_ISLINKABLE}, 
64         { ID_ME,                "Mesh",         "meshes",               IDTYPE_FLAGS_ISLINKABLE}, 
65         { ID_NT,                "NodeTree",     "node_groups",  IDTYPE_FLAGS_ISLINKABLE}, 
66         { ID_OB,                "Object",       "objects",              IDTYPE_FLAGS_ISLINKABLE}, 
67         { ID_PA,                "ParticleSettings",     "particles", 0},
68         { ID_SCE,               "Scene",        "scenes",               IDTYPE_FLAGS_ISLINKABLE}, 
69         { ID_SCR,               "Screen",       "screens",              0}, 
70         { ID_SEQ,               "Sequence",     "sequences",    0}, /* not actually ID data */
71         { ID_SO,                "Sound",        "sounds",               IDTYPE_FLAGS_ISLINKABLE}, 
72         { ID_TE,                "Texture",      "textures",             IDTYPE_FLAGS_ISLINKABLE}, 
73         { ID_TXT,               "Text",         "texts",                IDTYPE_FLAGS_ISLINKABLE}, 
74         { ID_VF,                "VFont",        "fonts",                IDTYPE_FLAGS_ISLINKABLE}, 
75         { ID_WO,                "World",        "worlds",               IDTYPE_FLAGS_ISLINKABLE}, 
76         { ID_WM,                "WindowManager", "window_managers",     0}, 
77 };
78 static int nidtypes= sizeof(idtypes)/sizeof(idtypes[0]);
79
80 static IDType *idtype_from_name(const char *str) 
81 {
82         int i= nidtypes;
83         
84         while (i--)
85                 if (strcmp(str, idtypes[i].name)==0)
86                         return &idtypes[i];
87
88         return NULL;
89 }
90 static IDType *idtype_from_code(int code) 
91 {
92         int i= nidtypes;
93         
94         while (i--)
95                 if (code==idtypes[i].code)
96                         return &idtypes[i];
97         
98         return NULL;
99 }
100
101 int BKE_idcode_is_valid(int code) 
102 {
103         return idtype_from_code(code)?1:0;
104 }
105
106 int BKE_idcode_is_linkable(int code) {
107         IDType *idt= idtype_from_code(code);
108         return idt?(idt->flags&IDTYPE_FLAGS_ISLINKABLE):0;
109 }
110
111 const char *BKE_idcode_to_name(int code) 
112 {
113         IDType *idt= idtype_from_code(code);
114         
115         return idt?idt->name:NULL;
116 }
117
118 int BKE_idcode_from_name(const char *name) 
119 {
120         IDType *idt= idtype_from_name(name);
121         
122         return idt?idt->code:0;
123 }
124
125 const char *BKE_idcode_to_name_plural(int code) 
126 {
127         IDType *idt= idtype_from_code(code);
128         
129         return idt?idt->plural:NULL;
130 }