mirror of
https://github.com/ZoiteChat/zoitechat.git
synced 2026-03-10 07:50:19 +00:00
43
Makefile
Normal file
43
Makefile
Normal file
@@ -0,0 +1,43 @@
|
||||
PREFIX ?= /usr/local
|
||||
BUILD_DIR ?= build
|
||||
MESON ?= meson
|
||||
NINJA ?= ninja
|
||||
MESON_SETUP_ARGS ?=
|
||||
MESON_COMPILE_ARGS ?=
|
||||
MESON_INSTALL_ARGS ?=
|
||||
|
||||
CONFIG_STAMP := $(BUILD_DIR)/build.ninja
|
||||
|
||||
.PHONY: all configure build reconfigure install uninstall clean distclean
|
||||
|
||||
all: build
|
||||
|
||||
# Only run initial meson setup if we don't have a configured build dir yet.
|
||||
$(CONFIG_STAMP):
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
@env NINJA=$(NINJA) $(MESON) setup $(BUILD_DIR) --prefix=$(PREFIX) $(MESON_SETUP_ARGS)
|
||||
|
||||
configure: $(CONFIG_STAMP)
|
||||
|
||||
build: configure
|
||||
@env NINJA=$(NINJA) $(MESON) compile -C $(BUILD_DIR) $(MESON_COMPILE_ARGS)
|
||||
|
||||
reconfigure:
|
||||
@mkdir -p $(BUILD_DIR)
|
||||
@env NINJA=$(NINJA) $(MESON) setup --reconfigure $(BUILD_DIR) --prefix=$(PREFIX) $(MESON_SETUP_ARGS)
|
||||
|
||||
install: build
|
||||
@env NINJA=$(NINJA) $(MESON) install -C $(BUILD_DIR) $(MESON_INSTALL_ARGS)
|
||||
|
||||
uninstall: configure
|
||||
@$(NINJA) -C $(BUILD_DIR) uninstall
|
||||
|
||||
clean:
|
||||
@if [ -f "$(CONFIG_STAMP)" ]; then \
|
||||
env NINJA=$(NINJA) $(MESON) compile -C $(BUILD_DIR) --clean; \
|
||||
else \
|
||||
echo "Nothing to clean (no $(CONFIG_STAMP))."; \
|
||||
fi
|
||||
|
||||
distclean:
|
||||
rm -rf $(BUILD_DIR)
|
||||
@@ -10,7 +10,7 @@
|
||||
|
||||
[![Version][github-version-img]][github-version-uri] [![Downloads][github-downloads-img]][github-downloads-uri] [![Size][github-size-img]][github-size-img] [![Last Commit][github-commit-img]][github-commit-img] [![Contributors][contribs-all-img]](#contributors-)
|
||||
|
||||
[](https://zoitechat.zoite.net/docs/)
|
||||
[](https://docs.zoitechat.zoite.net)
|
||||
|
||||
</div>
|
||||
|
||||
@@ -35,7 +35,7 @@
|
||||
|
||||
ZoiteChat is an HexChat based IRC client for Windows and UNIX-like operating systems.
|
||||
See [IRCHelp.org](http://irchelp.org) for information about IRC in general.
|
||||
For more information on ZoiteChat please read our [documentation](https://zoitechat.zoite.net/docs/):
|
||||
For more information on ZoiteChat please read our [documentation](https://docs.zoitechat.zoite.net/):
|
||||
- [Downloads](https://zoitechat.zoite.net/download)
|
||||
|
||||
---
|
||||
|
||||
@@ -18,14 +18,13 @@
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <glib.h>
|
||||
#include "history.h"
|
||||
|
||||
void
|
||||
history_add (struct history *his, char *text)
|
||||
{
|
||||
g_free (his->lines[his->realpos]);
|
||||
his->lines[his->realpos] = g_strdup (text);
|
||||
free (his->lines[his->realpos]);
|
||||
his->lines[his->realpos] = strdup (text);
|
||||
his->realpos++;
|
||||
if (his->realpos == HISTORY_SIZE)
|
||||
his->realpos = 0;
|
||||
@@ -40,7 +39,7 @@ history_free (struct history *his)
|
||||
{
|
||||
if (his->lines[i])
|
||||
{
|
||||
g_free (his->lines[i]);
|
||||
free (his->lines[i]);
|
||||
his->lines[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <glib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#ifndef WIN32
|
||||
#include <unistd.h>
|
||||
@@ -54,7 +54,7 @@ net_set_socket_options (int sok)
|
||||
}
|
||||
|
||||
char *
|
||||
net_ip (guint32 addr)
|
||||
net_ip (uint32_t addr)
|
||||
{
|
||||
struct in_addr ia;
|
||||
|
||||
@@ -67,13 +67,13 @@ net_store_destroy (netstore * ns)
|
||||
{
|
||||
if (ns->ip6_hostent)
|
||||
freeaddrinfo (ns->ip6_hostent);
|
||||
g_free (ns);
|
||||
free (ns);
|
||||
}
|
||||
|
||||
netstore *
|
||||
net_store_new (void)
|
||||
{
|
||||
return g_new0 (netstore, 1);
|
||||
return calloc (1, sizeof (netstore));
|
||||
}
|
||||
|
||||
/* =================== IPV6 ================== */
|
||||
@@ -121,11 +121,11 @@ net_resolve (netstore * ns, char *hostname, int port, char **real_host)
|
||||
ipstring, sizeof (ipstring), NULL, 0, NI_NUMERICHOST);
|
||||
|
||||
if (ns->ip6_hostent->ai_canonname)
|
||||
*real_host = g_strdup (ns->ip6_hostent->ai_canonname);
|
||||
*real_host = strdup (ns->ip6_hostent->ai_canonname);
|
||||
else
|
||||
*real_host = g_strdup (hostname);
|
||||
*real_host = strdup (hostname);
|
||||
|
||||
return g_strdup (ipstring);
|
||||
return strdup (ipstring);
|
||||
}
|
||||
|
||||
/* the only thing making this interface unclean, this shitty sok4, sok6 business */
|
||||
|
||||
@@ -20,6 +20,8 @@
|
||||
#ifndef ZOITECHAT_NETWORK_H
|
||||
#define ZOITECHAT_NETWORK_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
typedef struct netstore_
|
||||
{
|
||||
#ifdef NETWORK_PRIVATE
|
||||
@@ -36,7 +38,7 @@ void net_store_destroy (netstore *ns);
|
||||
int net_connect (netstore *ns, int sok4, int sok6, int *sok_return);
|
||||
char *net_resolve (netstore *ns, char *hostname, int port, char **real_host);
|
||||
void net_bind (netstore *tobindto, int sok4, int sok6);
|
||||
char *net_ip (guint32 addr);
|
||||
char *net_ip (uint32_t addr);
|
||||
void net_sockets (int *sok4, int *sok6);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
#ifndef ZOITECHAT_SYSINFO_H
|
||||
#define ZOITECHAT_SYSINFO_H
|
||||
|
||||
#include <glib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
int sysinfo_get_cpu_arch (void);
|
||||
int sysinfo_get_build_arch (void);
|
||||
char *sysinfo_get_cpu (void);
|
||||
char *sysinfo_get_os (void);
|
||||
void sysinfo_get_hdd_info (guint64 *hdd_capacity_out, guint64 *hdd_free_space_out);
|
||||
void sysinfo_get_hdd_info (uint64_t *hdd_capacity_out, uint64_t *hdd_free_space_out);
|
||||
char *sysinfo_get_gpu (void);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -20,7 +20,14 @@
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <ctype.h>
|
||||
#include <inttypes.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <wchar.h>
|
||||
#include <windows.h>
|
||||
#include <wbemidl.h>
|
||||
@@ -47,12 +54,26 @@ static char *read_os_name (IWbemClassObject *object);
|
||||
static char *read_cpu_info (IWbemClassObject *object);
|
||||
static char *read_vga_name (IWbemClassObject *object);
|
||||
|
||||
static guint64 hdd_capacity;
|
||||
static guint64 hdd_free_space;
|
||||
static uint64_t hdd_capacity;
|
||||
static uint64_t hdd_free_space;
|
||||
static char *read_hdd_info (IWbemClassObject *object);
|
||||
|
||||
static char *bstr_to_utf8 (BSTR bstr);
|
||||
static guint64 variant_to_uint64 (VARIANT *variant);
|
||||
static uint64_t variant_to_uint64 (VARIANT *variant);
|
||||
static char *zoitechat_strdup (const char *value);
|
||||
static char *zoitechat_strdup_printf (const char *format, ...);
|
||||
static char *zoitechat_strchomp (char *value);
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char *data;
|
||||
size_t len;
|
||||
size_t cap;
|
||||
} StringBuilder;
|
||||
|
||||
static bool string_builder_init (StringBuilder *builder);
|
||||
static void string_builder_free (StringBuilder *builder);
|
||||
static bool string_builder_append (StringBuilder *builder, const char *text);
|
||||
|
||||
char *
|
||||
sysinfo_get_cpu (void)
|
||||
@@ -60,7 +81,7 @@ sysinfo_get_cpu (void)
|
||||
if (cpu_info == NULL)
|
||||
cpu_info = query_wmi (QUERY_WMI_CPU);
|
||||
|
||||
return g_strdup (cpu_info);
|
||||
return zoitechat_strdup (cpu_info);
|
||||
}
|
||||
|
||||
char *
|
||||
@@ -69,7 +90,12 @@ sysinfo_get_os (void)
|
||||
if (os_name == NULL)
|
||||
os_name = query_wmi (QUERY_WMI_OS);
|
||||
|
||||
return g_strdup_printf ("%s (x%d)", os_name, sysinfo_get_cpu_arch ());
|
||||
if (os_name == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return zoitechat_strdup_printf ("%s (x%d)", os_name, sysinfo_get_cpu_arch ());
|
||||
}
|
||||
|
||||
int
|
||||
@@ -124,11 +150,11 @@ sysinfo_get_gpu (void)
|
||||
if (vga_name == NULL)
|
||||
vga_name = query_wmi (QUERY_WMI_VGA);
|
||||
|
||||
return g_strdup (vga_name);
|
||||
return zoitechat_strdup (vga_name);
|
||||
}
|
||||
|
||||
void
|
||||
sysinfo_get_hdd_info (guint64 *hdd_capacity_out, guint64 *hdd_free_space_out)
|
||||
sysinfo_get_hdd_info (uint64_t *hdd_capacity_out, uint64_t *hdd_free_space_out)
|
||||
{
|
||||
char *hdd_info;
|
||||
|
||||
@@ -149,7 +175,8 @@ sysinfo_get_hdd_info (guint64 *hdd_capacity_out, guint64 *hdd_free_space_out)
|
||||
/* https://msdn.microsoft.com/en-us/library/aa390423 */
|
||||
static char *query_wmi (QueryWmiType type)
|
||||
{
|
||||
GString *result = NULL;
|
||||
StringBuilder result;
|
||||
bool result_initialized = false;
|
||||
HRESULT hr;
|
||||
|
||||
IWbemLocator *locator = NULL;
|
||||
@@ -160,7 +187,7 @@ static char *query_wmi (QueryWmiType type)
|
||||
IUnknown *namespaceUnknown = NULL;
|
||||
IEnumWbemClassObject *enumerator = NULL;
|
||||
int i;
|
||||
gboolean atleast_one_appended = FALSE;
|
||||
bool atleast_one_appended = false;
|
||||
|
||||
hr = CoCreateInstance (&CLSID_WbemLocator, 0, CLSCTX_INPROC_SERVER, &IID_IWbemLocator, (LPVOID *) &locator);
|
||||
if (FAILED (hr))
|
||||
@@ -214,7 +241,11 @@ static char *query_wmi (QueryWmiType type)
|
||||
goto release_query;
|
||||
}
|
||||
|
||||
result = g_string_new ("");
|
||||
if (!string_builder_init (&result))
|
||||
{
|
||||
goto release_query;
|
||||
}
|
||||
result_initialized = true;
|
||||
|
||||
for (i = 0;; i++)
|
||||
{
|
||||
@@ -256,14 +287,22 @@ static char *query_wmi (QueryWmiType type)
|
||||
{
|
||||
if (atleast_one_appended)
|
||||
{
|
||||
g_string_append (result, ", ");
|
||||
if (!string_builder_append (&result, ", "))
|
||||
{
|
||||
free (line);
|
||||
goto release_query;
|
||||
}
|
||||
}
|
||||
|
||||
g_string_append (result, line);
|
||||
if (!string_builder_append (&result, line))
|
||||
{
|
||||
free (line);
|
||||
goto release_query;
|
||||
}
|
||||
|
||||
g_free (line);
|
||||
free (line);
|
||||
|
||||
atleast_one_appended = TRUE;
|
||||
atleast_one_appended = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -286,12 +325,12 @@ release_locator:
|
||||
SysFreeString (namespaceName);
|
||||
|
||||
exit:
|
||||
if (result == NULL)
|
||||
if (!result_initialized || result.data == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return g_string_free (result, FALSE);
|
||||
return result.data;
|
||||
}
|
||||
|
||||
static char *read_os_name (IWbemClassObject *object)
|
||||
@@ -315,7 +354,7 @@ static char *read_os_name (IWbemClassObject *object)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
g_strchomp (caption_utf8);
|
||||
zoitechat_strchomp (caption_utf8);
|
||||
|
||||
return caption_utf8;
|
||||
}
|
||||
@@ -326,7 +365,7 @@ static char *read_cpu_info (IWbemClassObject *object)
|
||||
VARIANT name_variant;
|
||||
char *name_utf8;
|
||||
VARIANT max_clock_speed_variant;
|
||||
guint cpu_freq_mhz;
|
||||
uint32_t cpu_freq_mhz;
|
||||
char *result;
|
||||
|
||||
hr = object->lpVtbl->Get (object, L"Name", 0, &name_variant, NULL, NULL);
|
||||
@@ -347,7 +386,7 @@ static char *read_cpu_info (IWbemClassObject *object)
|
||||
hr = object->lpVtbl->Get (object, L"MaxClockSpeed", 0, &max_clock_speed_variant, NULL, NULL);
|
||||
if (FAILED (hr))
|
||||
{
|
||||
g_free (name_utf8);
|
||||
free (name_utf8);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
@@ -356,18 +395,18 @@ static char *read_cpu_info (IWbemClassObject *object)
|
||||
|
||||
VariantClear (&max_clock_speed_variant);
|
||||
|
||||
g_strchomp (name_utf8);
|
||||
zoitechat_strchomp (name_utf8);
|
||||
|
||||
if (cpu_freq_mhz > 1000)
|
||||
{
|
||||
result = g_strdup_printf ("%s (%.2fGHz)", name_utf8, cpu_freq_mhz / 1000.f);
|
||||
result = zoitechat_strdup_printf ("%s (%.2fGHz)", name_utf8, cpu_freq_mhz / 1000.f);
|
||||
}
|
||||
else
|
||||
{
|
||||
result = g_strdup_printf ("%s (%" G_GUINT32_FORMAT "MHz)", name_utf8, cpu_freq_mhz);
|
||||
result = zoitechat_strdup_printf ("%s (%" PRIu32 "MHz)", name_utf8, cpu_freq_mhz);
|
||||
}
|
||||
|
||||
g_free (name_utf8);
|
||||
free (name_utf8);
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -393,7 +432,7 @@ static char *read_vga_name (IWbemClassObject *object)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return g_strchomp (name_utf8);
|
||||
return zoitechat_strchomp (name_utf8);
|
||||
}
|
||||
|
||||
static char *read_hdd_info (IWbemClassObject *object)
|
||||
@@ -401,11 +440,11 @@ static char *read_hdd_info (IWbemClassObject *object)
|
||||
HRESULT hr;
|
||||
VARIANT name_variant;
|
||||
BSTR name_bstr;
|
||||
gsize name_len;
|
||||
size_t name_len;
|
||||
VARIANT capacity_variant;
|
||||
guint64 capacity;
|
||||
uint64_t capacity;
|
||||
VARIANT free_space_variant;
|
||||
guint64 free_space;
|
||||
uint64_t free_space;
|
||||
|
||||
hr = object->lpVtbl->Get (object, L"Name", 0, &name_variant, NULL, NULL);
|
||||
if (FAILED (hr))
|
||||
@@ -464,10 +503,38 @@ static char *read_hdd_info (IWbemClassObject *object)
|
||||
|
||||
static char *bstr_to_utf8 (BSTR bstr)
|
||||
{
|
||||
return g_utf16_to_utf8 (bstr, SysStringLen (bstr), NULL, NULL, NULL);
|
||||
int utf8_len;
|
||||
char *utf8;
|
||||
|
||||
if (bstr == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
utf8_len = WideCharToMultiByte (CP_UTF8, 0, bstr, SysStringLen (bstr), NULL, 0, NULL, NULL);
|
||||
if (utf8_len <= 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
utf8 = malloc ((size_t) utf8_len + 1);
|
||||
if (utf8 == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (WideCharToMultiByte (CP_UTF8, 0, bstr, SysStringLen (bstr), utf8, utf8_len, NULL, NULL) <= 0)
|
||||
{
|
||||
free (utf8);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
utf8[utf8_len] = '\0';
|
||||
|
||||
return utf8;
|
||||
}
|
||||
|
||||
static guint64 variant_to_uint64 (VARIANT *variant)
|
||||
static uint64_t variant_to_uint64 (VARIANT *variant)
|
||||
{
|
||||
switch (V_VT (variant))
|
||||
{
|
||||
@@ -481,3 +548,139 @@ static guint64 variant_to_uint64 (VARIANT *variant)
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
static char *zoitechat_strdup (const char *value)
|
||||
{
|
||||
size_t len;
|
||||
char *copy;
|
||||
|
||||
if (value == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
len = strlen (value);
|
||||
copy = malloc (len + 1);
|
||||
if (copy == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
memcpy (copy, value, len + 1);
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
static char *zoitechat_strdup_printf (const char *format, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_list args_copy;
|
||||
int length;
|
||||
char *buffer;
|
||||
|
||||
va_start (args, format);
|
||||
va_copy (args_copy, args);
|
||||
length = vsnprintf (NULL, 0, format, args_copy);
|
||||
va_end (args_copy);
|
||||
|
||||
if (length < 0)
|
||||
{
|
||||
va_end (args);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
buffer = malloc ((size_t) length + 1);
|
||||
if (buffer == NULL)
|
||||
{
|
||||
va_end (args);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
vsnprintf (buffer, (size_t) length + 1, format, args);
|
||||
va_end (args);
|
||||
|
||||
return buffer;
|
||||
}
|
||||
|
||||
static char *zoitechat_strchomp (char *value)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
if (value == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
len = strlen (value);
|
||||
while (len > 0 && isspace ((unsigned char) value[len - 1]) != 0)
|
||||
{
|
||||
value[len - 1] = '\0';
|
||||
len--;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
static bool string_builder_init (StringBuilder *builder)
|
||||
{
|
||||
builder->cap = 64;
|
||||
builder->len = 0;
|
||||
builder->data = malloc (builder->cap);
|
||||
if (builder->data == NULL)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
builder->data[0] = '\0';
|
||||
return true;
|
||||
}
|
||||
|
||||
static void string_builder_free (StringBuilder *builder)
|
||||
{
|
||||
if (builder->data != NULL)
|
||||
{
|
||||
free (builder->data);
|
||||
builder->data = NULL;
|
||||
}
|
||||
builder->len = 0;
|
||||
builder->cap = 0;
|
||||
}
|
||||
|
||||
static bool string_builder_append (StringBuilder *builder, const char *text)
|
||||
{
|
||||
size_t add_len;
|
||||
size_t needed;
|
||||
size_t new_cap;
|
||||
char *new_data;
|
||||
|
||||
if (text == NULL)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
add_len = strlen (text);
|
||||
needed = builder->len + add_len + 1;
|
||||
if (needed > builder->cap)
|
||||
{
|
||||
new_cap = builder->cap;
|
||||
while (new_cap < needed)
|
||||
{
|
||||
new_cap *= 2;
|
||||
}
|
||||
|
||||
new_data = realloc (builder->data, new_cap);
|
||||
if (new_data == NULL)
|
||||
{
|
||||
string_builder_free (builder);
|
||||
return false;
|
||||
}
|
||||
|
||||
builder->data = new_data;
|
||||
builder->cap = new_cap;
|
||||
}
|
||||
|
||||
memcpy (builder->data + builder->len, text, add_len);
|
||||
builder->len += add_len;
|
||||
builder->data[builder->len] = '\0';
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -214,11 +214,13 @@ waitline2 (GIOChannel *source, char *buf, int bufsize)
|
||||
gsize len;
|
||||
GError *error = NULL;
|
||||
|
||||
if (g_io_channel_get_buffered (source))
|
||||
{
|
||||
g_io_channel_set_encoding (source, NULL, &error);
|
||||
g_io_channel_set_buffered (source, FALSE);
|
||||
}
|
||||
while (1)
|
||||
{
|
||||
g_io_channel_set_buffered (source, FALSE);
|
||||
g_io_channel_set_encoding (source, NULL, &error);
|
||||
|
||||
if (g_io_channel_read_chars (source, &buf[i], 1, &len, &error) != G_IO_STATUS_NORMAL)
|
||||
{
|
||||
return -1;
|
||||
|
||||
@@ -23,7 +23,9 @@
|
||||
#include "config.h"
|
||||
|
||||
#include <Spellcheck.h>
|
||||
#include <glib.h>
|
||||
#include <windows.h>
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include "typedef.h" // for ssize_t
|
||||
#include <enchant-provider.h>
|
||||
@@ -33,9 +35,24 @@ ENCHANT_PLUGIN_DECLARE ("win8")
|
||||
/* --------- Utils ----------*/
|
||||
|
||||
static char *
|
||||
utf16_to_utf8 (const wchar_t * const str, gboolean from_bcp47)
|
||||
utf16_to_utf8 (const wchar_t * const str, bool from_bcp47)
|
||||
{
|
||||
char *utf8 = g_utf16_to_utf8 ((gunichar2*)str, -1, nullptr, nullptr, nullptr);
|
||||
if (!str)
|
||||
return nullptr;
|
||||
|
||||
int needed = WideCharToMultiByte (CP_UTF8, 0, str, -1, nullptr, 0, nullptr, nullptr);
|
||||
if (needed <= 0)
|
||||
return nullptr;
|
||||
|
||||
char *utf8 = static_cast<char*>(std::malloc (static_cast<size_t>(needed)));
|
||||
if (!utf8)
|
||||
return nullptr;
|
||||
|
||||
if (WideCharToMultiByte (CP_UTF8, 0, str, -1, utf8, needed, nullptr, nullptr) != needed)
|
||||
{
|
||||
std::free (utf8);
|
||||
return nullptr;
|
||||
}
|
||||
if (utf8 && from_bcp47)
|
||||
{
|
||||
char *p = utf8;
|
||||
@@ -51,9 +68,27 @@ utf16_to_utf8 (const wchar_t * const str, gboolean from_bcp47)
|
||||
}
|
||||
|
||||
static wchar_t *
|
||||
utf8_to_utf16 (const char * const str, size_t len, gboolean to_bcp47)
|
||||
utf8_to_utf16 (const char * const str, int len, bool to_bcp47)
|
||||
{
|
||||
wchar_t *utf16 = (wchar_t*)g_utf8_to_utf16 (str, len, nullptr, nullptr, nullptr);
|
||||
if (!str)
|
||||
return nullptr;
|
||||
|
||||
int needed = MultiByteToWideChar (CP_UTF8, 0, str, len, nullptr, 0);
|
||||
if (needed <= 0)
|
||||
return nullptr;
|
||||
|
||||
int alloc_len = (len == -1) ? needed : needed + 1;
|
||||
wchar_t *utf16 = static_cast<wchar_t*>(std::malloc (sizeof (wchar_t) * static_cast<size_t>(alloc_len)));
|
||||
if (!utf16)
|
||||
return nullptr;
|
||||
|
||||
if (MultiByteToWideChar (CP_UTF8, 0, str, len, utf16, needed) != needed)
|
||||
{
|
||||
std::free (utf16);
|
||||
return nullptr;
|
||||
}
|
||||
if (len != -1)
|
||||
utf16[needed] = L'\0';
|
||||
if (utf16 && to_bcp47)
|
||||
{
|
||||
wchar_t *p = utf16;
|
||||
@@ -69,12 +104,19 @@ utf8_to_utf16 (const char * const str, size_t len, gboolean to_bcp47)
|
||||
}
|
||||
|
||||
static char **
|
||||
enumstring_to_chararray (IEnumString *strings, size_t *out_len, gboolean from_bcp47)
|
||||
enumstring_to_chararray (IEnumString *strings, size_t *out_len, bool from_bcp47)
|
||||
{
|
||||
char **chars = g_new (char*, 256); /* Hopefully large enough */
|
||||
char **chars = static_cast<char**>(std::calloc (256, sizeof (char*))); /* Hopefully large enough */
|
||||
LPOLESTR wstr = nullptr;
|
||||
size_t i = 0;
|
||||
|
||||
if (!chars)
|
||||
{
|
||||
*out_len = 0;
|
||||
strings->Release ();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
while (SUCCEEDED (strings->Next (1, &wstr, nullptr)) && i < 256 && wstr)
|
||||
{
|
||||
char *str = utf16_to_utf8 (wstr, from_bcp47);
|
||||
@@ -98,33 +140,33 @@ static void
|
||||
win8_dict_add_to_personal (EnchantDict *dict, const char *const word, size_t len)
|
||||
{
|
||||
auto checker = static_cast<ISpellChecker*>(dict->user_data);
|
||||
wchar_t *wword = utf8_to_utf16 (word, len, FALSE);
|
||||
wchar_t *wword = utf8_to_utf16 (word, static_cast<int>(len), false);
|
||||
|
||||
checker->Add (wword);
|
||||
g_free (wword);
|
||||
std::free (wword);
|
||||
}
|
||||
|
||||
static void
|
||||
win8_dict_add_to_session (EnchantDict *dict, const char *const word, size_t len)
|
||||
{
|
||||
auto checker = static_cast<ISpellChecker*>(dict->user_data);
|
||||
wchar_t *wword = utf8_to_utf16 (word, len, FALSE);
|
||||
wchar_t *wword = utf8_to_utf16 (word, static_cast<int>(len), false);
|
||||
|
||||
checker->Ignore (wword);
|
||||
g_free (wword);
|
||||
std::free (wword);
|
||||
}
|
||||
|
||||
static int
|
||||
win8_dict_check (EnchantDict *dict, const char *const word, size_t len)
|
||||
{
|
||||
auto checker = static_cast<ISpellChecker*>(dict->user_data);
|
||||
wchar_t *wword = utf8_to_utf16 (word, len, FALSE);
|
||||
wchar_t *wword = utf8_to_utf16 (word, static_cast<int>(len), false);
|
||||
IEnumSpellingError *errors;
|
||||
ISpellingError *error = nullptr;
|
||||
HRESULT hr;
|
||||
|
||||
hr = checker->Check (wword, &errors);
|
||||
g_free (wword);
|
||||
std::free (wword);
|
||||
|
||||
if (FAILED (hr))
|
||||
return -1; /* Error */
|
||||
@@ -146,12 +188,12 @@ static char **
|
||||
win8_dict_suggest (EnchantDict *dict, const char *const word, size_t len, size_t *out_n_suggs)
|
||||
{
|
||||
auto checker = static_cast<ISpellChecker*>(dict->user_data);
|
||||
wchar_t *wword = utf8_to_utf16 (word, len, FALSE);
|
||||
wchar_t *wword = utf8_to_utf16 (word, static_cast<int>(len), false);
|
||||
IEnumString *suggestions;
|
||||
HRESULT hr;
|
||||
|
||||
hr = checker->Suggest (wword, &suggestions);
|
||||
g_free (wword);
|
||||
std::free (wword);
|
||||
|
||||
if (FAILED (hr))
|
||||
{
|
||||
@@ -159,7 +201,7 @@ win8_dict_suggest (EnchantDict *dict, const char *const word, size_t len, size_t
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return enumstring_to_chararray (suggestions, out_n_suggs, FALSE);
|
||||
return enumstring_to_chararray (suggestions, out_n_suggs, false);
|
||||
}
|
||||
|
||||
/* ---------- Provider ------------ */
|
||||
@@ -170,16 +212,16 @@ win8_provider_request_dict (EnchantProvider *provider, const char *const tag)
|
||||
auto factory = static_cast<ISpellCheckerFactory*>(provider->user_data);
|
||||
ISpellChecker *checker;
|
||||
EnchantDict *dict;
|
||||
wchar_t *wtag = utf8_to_utf16 (tag, -1, TRUE);
|
||||
wchar_t *wtag = utf8_to_utf16 (tag, -1, true);
|
||||
HRESULT hr;
|
||||
|
||||
hr = factory->CreateSpellChecker (wtag, &checker);
|
||||
g_free (wtag);
|
||||
std::free (wtag);
|
||||
|
||||
if (FAILED (hr))
|
||||
return nullptr;
|
||||
|
||||
dict = g_new0 (EnchantDict, 1);
|
||||
dict = static_cast<EnchantDict*>(std::calloc (1, sizeof (EnchantDict)));
|
||||
dict->suggest = win8_dict_suggest;
|
||||
dict->check = win8_dict_check;
|
||||
dict->add_to_personal = win8_dict_add_to_personal;
|
||||
@@ -199,7 +241,7 @@ win8_provider_dispose_dict (EnchantProvider *provider, EnchantDict *dict)
|
||||
auto checker = static_cast<ISpellChecker*>(dict->user_data);
|
||||
|
||||
checker->Release ();
|
||||
g_free (dict);
|
||||
std::free (dict);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,12 +249,12 @@ static int
|
||||
win8_provider_dictionary_exists (EnchantProvider *provider, const char *const tag)
|
||||
{
|
||||
auto factory = static_cast<ISpellCheckerFactory*>(provider->user_data);
|
||||
wchar_t *wtag = utf8_to_utf16 (tag, -1, TRUE);
|
||||
wchar_t *wtag = utf8_to_utf16 (tag, -1, true);
|
||||
|
||||
BOOL is_supported = FALSE;
|
||||
factory->IsSupported (wtag, &is_supported);
|
||||
|
||||
g_free (wtag);
|
||||
std::free (wtag);
|
||||
return is_supported;
|
||||
}
|
||||
|
||||
@@ -229,13 +271,17 @@ win8_provider_list_dicts (EnchantProvider *provider, size_t *out_n_dicts)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return enumstring_to_chararray (dicts, out_n_dicts, TRUE);
|
||||
return enumstring_to_chararray (dicts, out_n_dicts, true);
|
||||
}
|
||||
|
||||
static void
|
||||
win8_provider_free_string_list (EnchantProvider *provider, char **str_list)
|
||||
{
|
||||
g_strfreev (str_list);
|
||||
if (!str_list)
|
||||
return;
|
||||
for (size_t i = 0; str_list[i]; ++i)
|
||||
std::free (str_list[i]);
|
||||
std::free (str_list);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -246,7 +292,7 @@ win8_provider_dispose (EnchantProvider *provider)
|
||||
auto factory = static_cast<ISpellCheckerFactory*>(provider->user_data);
|
||||
|
||||
factory->Release();
|
||||
g_free (provider);
|
||||
std::free (provider);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,7 +321,7 @@ init_enchant_provider (void)
|
||||
CLSCTX_INPROC_SERVER, IID_PPV_ARGS (&factory))))
|
||||
return nullptr;
|
||||
|
||||
provider = g_new0 (EnchantProvider, 1);
|
||||
provider = static_cast<EnchantProvider*>(std::calloc (1, sizeof (EnchantProvider)));
|
||||
provider->dispose = win8_provider_dispose;
|
||||
provider->request_dict = win8_provider_request_dict;
|
||||
provider->dispose_dict = win8_provider_dispose_dict;
|
||||
|
||||
Reference in New Issue
Block a user