Add IDs and names

master
C. McEnroe 2020-02-01 02:33:17 -05:00
parent 2b3a8bfb9c
commit 03cb0d7c04
2 changed files with 29 additions and 0 deletions

7
chat.c
View File

@ -23,6 +23,13 @@
#include "chat.h" #include "chat.h"
char *idNames[IDCap] = {
[None] = "<none>",
[Debug] = "<debug>",
[Network] = "<network>",
};
size_t idNext = Network + 1;
struct Self self; struct Self self;
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {

22
chat.h
View File

@ -14,13 +14,35 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <err.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h>
#include <sysexits.h>
#include <time.h>
#define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0])) #define ARRAY_LEN(a) (sizeof(a) / sizeof(a[0]))
#define BIT(x) x##Bit, x = 1 << x##Bit, x##Bit_ = x##Bit #define BIT(x) x##Bit, x = 1 << x##Bit, x##Bit_ = x##Bit
typedef unsigned char byte; typedef unsigned char byte;
enum { None, Debug, Network, IDCap = 256 };
extern char *idNames[IDCap];
extern size_t idNext;
static inline size_t idFind(const char *name) {
for (size_t id = 0; id < idNext; ++id) {
if (!strcmp(idNames[id], name)) return id;
}
return None;
}
static inline size_t idFor(const char *name) {
size_t id = idFind(name);
if (id) return id;
idNames[idNext] = strdup(name);
if (!idNames[idNext]) err(EX_OSERR, "strdup");
return idNext++;
}
#define ENUM_CAP \ #define ENUM_CAP \
X("sasl", CapSASL) \ X("sasl", CapSASL) \
X("server-time", CapServerTime) \ X("server-time", CapServerTime) \