| 1 | // +build !linux |
| 2 | // This file is systemdependent because not all versions |
| 3 | // of iconv have the iconvlist function. |
| 4 | |
| 5 | package iconv |
| 6 | |
| 7 | //#cgo darwin LDFLAGS: -liconv |
| 8 | //#cgo freebsd LDFLAGS: -liconv |
| 9 | //#cgo windows LDFLAGS: -liconv |
| 10 | //#include <stdlib.h> |
| 11 | //#include <string.h> |
| 12 | //#include <iconv.h> |
| 13 | //#include <errno.h> |
| 14 | // |
| 15 | //typedef struct nameList nameList; |
| 16 | //struct nameList { |
| 17 | // int n; |
| 18 | // char **names; |
| 19 | // nameList *next; |
| 20 | //}; |
| 21 | // |
| 22 | //int |
| 23 | //addNames(unsigned int n, const char *const *names, void *data) { |
| 24 | // // we can't call back to Go because of the stack size issue, |
| 25 | // // so copy all the names. |
| 26 | // nameList *hd, *e; |
| 27 | // int i; |
| 28 | // |
| 29 | // hd = data; |
| 30 | // e = malloc(sizeof(nameList)); |
| 31 | // e->n = n; |
| 32 | // e->names = malloc(sizeof(char*) * n); |
| 33 | // for(i = 0; i < n; i++){ |
| 34 | // e->names[i] = strdup(names[i]); |
| 35 | // } |
| 36 | // e->next = hd->next; |
| 37 | // hd->next = e; |
| 38 | // return 0; |
| 39 | //} |
| 40 | // |
| 41 | //nameList * |
| 42 | //listNames(void) { |
| 43 | // nameList hd; |
| 44 | // hd.next = 0; |
| 45 | // iconvlist(addNames, &hd); |
| 46 | // return hd.next; |
| 47 | //} |
| 48 | import "C" |
| 49 | |
| 50 | import ( |
| 51 | "strings" |
| 52 | "sync" |
| 53 | "unsafe" |
| 54 | ) |
| 55 | |
| 56 | var getAliasesOnce sync.Once |
| 57 | var allAliases = map[string][]string{} |
| 58 | |
| 59 | func aliases() map[string][]string { |
| 60 | getAliasesOnce.Do(getAliases) |
| 61 | return allAliases |
| 62 | } |
| 63 | |
| 64 | func getAliases() { |
| 65 | var next *C.nameList |
| 66 | for p := C.listNames(); p != nil; p = next { |
| 67 | next = p.next |
| 68 | aliases := make([]string, p.n) |
| 69 | pnames := (*[1e9]*C.char)(unsafe.Pointer(p.names)) |
| 70 | for i := range aliases { |
| 71 | aliases[i] = strings.ToLower(C.GoString(pnames[i])) |
| 72 | C.free(unsafe.Pointer(pnames[i])) |
| 73 | } |
| 74 | C.free(unsafe.Pointer(p.names)) |
| 75 | C.free(unsafe.Pointer(p)) |
| 76 | for _, alias := range aliases { |
| 77 | allAliases[alias] = aliases |
| 78 | } |
| 79 | } |
| 80 | } |