From 5678ab5182b27d684cf7a4e7c6c48f3657389d31 Mon Sep 17 00:00:00 2001 From: matz Date: Sat, 25 Aug 2007 04:07:20 +0000 Subject: encoding.c: added. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13263 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- encoding.c | 229 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 229 insertions(+) create mode 100644 encoding.c (limited to 'encoding.c') diff --git a/encoding.c b/encoding.c new file mode 100644 index 0000000000..eed384fb0d --- /dev/null +++ b/encoding.c @@ -0,0 +1,229 @@ +/********************************************************************** + + encoding.c - + + $Author: matz $ + $Date: 2007-05-24 17:22:33 +0900 (Thu, 24 May 2007) $ + created at: Thu May 24 17:23:27 JST 2007 + + Copyright (C) 2007 Yukihiro Matsumoto + +**********************************************************************/ + +#include "ruby/ruby.h" +#include "ruby/encoding.h" +#include "regenc.h" + +static ID id_encoding; + +struct rb_encoding_entry { + const char *name; + rb_encoding *enc; +}; + +static struct rb_encoding_entry *enc_table; +static int enc_table_size; + +void +rb_enc_register(const char *name, rb_encoding *encoding) +{ + struct rb_encoding_entry *ent; + + if (!enc_table) { + enc_table = malloc(sizeof(struct rb_encoding_entry)); + enc_table_size = 1; + } + else { + enc_table_size++; + enc_table = realloc(enc_table, sizeof(struct rb_encoding_entry)*enc_table_size); + } + ent = &enc_table[enc_table_size-1]; + ent->name = name; + ent->enc = encoding; +} + +void +rb_enc_init(void) +{ + rb_enc_register("ascii", ONIG_ENCODING_ASCII); + rb_enc_register("sjis", ONIG_ENCODING_SJIS); + rb_enc_register("euc-jp", ONIG_ENCODING_EUC_JP); + rb_enc_register("utf-8", ONIG_ENCODING_UTF8); +} + +rb_encoding * +rb_enc_from_index(int index) +{ + if (!enc_table) { + rb_enc_init(); + } + if (index < 0 || enc_table_size <= index) { + return 0; + } + return enc_table[index].enc; +} + +rb_encoding * +rb_enc_find(const char *name) +{ + int i; + + if (!enc_table) { + rb_enc_init(); + } + for (i=0; i