xref: /haiku/src/kits/textencoding/CharacterSetRoster.cpp (revision 3cb015b1ee509d69c643506e8ff573808c86dcfc)
1 #include <string.h>
2 #include <CharacterSet.h>
3 #include <CharacterSetRoster.h>
4 #include "character_sets.h"
5 
6 namespace BPrivate {
7 
8 BCharacterSetRoster::BCharacterSetRoster()
9 {
10 	index = 0;
11 }
12 
13 BCharacterSetRoster::~BCharacterSetRoster()
14 {
15 	// nothing to do
16 }
17 
18 status_t
19 BCharacterSetRoster::GetNextCharacterSet(BCharacterSet * charset)
20 {
21 	if (charset == 0) {
22 		return B_BAD_VALUE;
23 	}
24 	if (index >= character_sets_by_id_count) {
25 		return B_BAD_VALUE;
26 	}
27 	*charset = *character_sets_by_id[index++];
28 	return B_NO_ERROR;
29 }
30 
31 status_t
32 BCharacterSetRoster::RewindCharacterSets()
33 {
34 	index = 0;
35 	if (index >= character_sets_by_id_count) {
36 		return B_BAD_VALUE;
37 	}
38 	return B_NO_ERROR;
39 }
40 
41 status_t
42 BCharacterSetRoster::StartWatching(BMessenger target)
43 {
44 	// TODO: implement it
45 	return B_ERROR;
46 }
47 
48 status_t
49 BCharacterSetRoster::StopWatching(BMessenger target)
50 {
51 	// TODO: implement it
52 	return B_ERROR;
53 }
54 
55 const BCharacterSet *
56 BCharacterSetRoster::GetCharacterSetByFontID(uint32 id)
57 {
58 	if ((id < 0) || (id >= character_sets_by_id_count)) {
59 		return NULL;
60 	}
61 	return character_sets_by_id[id];
62 }
63 
64 const BCharacterSet *
65 BCharacterSetRoster::GetCharacterSetByConversionID(uint32 id)
66 {
67 	if ((id+1 < 0) || (id+1 >= character_sets_by_id_count)) {
68 		return NULL;
69 	}
70 	return character_sets_by_id[id+1];
71 }
72 
73 const BCharacterSet *
74 BCharacterSetRoster::GetCharacterSetByMIBenum(uint32 MIBenum)
75 {
76 	if ((MIBenum < 0) || (MIBenum > maximum_valid_MIBenum)) {
77 		return NULL;
78 	}
79 	return character_sets_by_MIBenum[MIBenum];
80 }
81 
82 const BCharacterSet *
83 BCharacterSetRoster::FindCharacterSetByPrintName(const char * name)
84 {
85 	for (uint id = 0 ; (id < character_sets_by_id_count) ; id++) {
86 		if (strcmp(character_sets_by_id[id]->GetPrintName(),name) == 0) {
87 			return character_sets_by_id[id];
88 		}
89 	}
90 	return 0;
91 }
92 
93 const BCharacterSet *
94 BCharacterSetRoster::FindCharacterSetByName(const char * name)
95 {
96 	// first search standard names and mime names
97 	for (uint id = 0 ; (id < character_sets_by_id_count) ; id++) {
98 		if (strcasecmp(character_sets_by_id[id]->GetName(),name) == 0) {
99 			return character_sets_by_id[id];
100 		}
101 		const char * mime = character_sets_by_id[id]->GetMIMEName();
102 		if ((mime != NULL) && (strcasecmp(mime,name) == 0)) {
103 			return character_sets_by_id[id];
104 		}
105 	}
106 	// only after searching all the above names do we look at aliases
107 	for (uint id = 0 ; (id < character_sets_by_id_count) ; id++) {
108 		for (int alias = 0 ; (alias < character_sets_by_id[id]->CountAliases()) ; alias++) {
109 			if (strcasecmp(character_sets_by_id[id]->AliasAt(alias),name) == 0) {
110 				return character_sets_by_id[id];
111 			}
112 		}
113 	}
114 	return 0;
115 }
116 
117 }
118