Factor out allocating conversion between wcs and mbs
This commit is contained in:
		
							parent
							
								
									2fe8b4e614
								
							
						
					
					
						commit
						a6ede6f91f
					
				
							
								
								
									
										2
									
								
								chat.h
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								chat.h
									
									
									
									
									
								
							@ -64,4 +64,6 @@ void tabTouch(const char *word);
 | 
				
			|||||||
void tabRemove(const char *word);
 | 
					void tabRemove(const char *word);
 | 
				
			||||||
void tabReplace(const char *prev, const char *next);
 | 
					void tabReplace(const char *prev, const char *next);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					wchar_t *ambstowcs(const char *src);
 | 
				
			||||||
 | 
					char *awcstombs(const wchar_t *src);
 | 
				
			||||||
int vaswprintf(wchar_t **ret, const wchar_t *format, va_list ap);
 | 
					int vaswprintf(wchar_t **ret, const wchar_t *format, va_list ap);
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										32
									
								
								pls.c
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								pls.c
									
									
									
									
									
								
							@ -20,6 +20,38 @@
 | 
				
			|||||||
#include <stdlib.h>
 | 
					#include <stdlib.h>
 | 
				
			||||||
#include <wchar.h>
 | 
					#include <wchar.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					wchar_t *ambstowcs(const char *src) {
 | 
				
			||||||
 | 
						size_t len = mbsrtowcs(NULL, &src, 0, NULL);
 | 
				
			||||||
 | 
						if (len == (size_t)-1) return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						wchar_t *dst = malloc(sizeof(*dst) * (1 + len));
 | 
				
			||||||
 | 
						if (!dst) return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						len = mbsrtowcs(dst, &src, 1 + len, NULL);
 | 
				
			||||||
 | 
						if (len == (size_t)-1) {
 | 
				
			||||||
 | 
							free(dst);
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return dst;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					char *awcstombs(const wchar_t *src) {
 | 
				
			||||||
 | 
						size_t len = wcsrtombs(NULL, &src, 0, NULL);
 | 
				
			||||||
 | 
						if (len == (size_t)-1) return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						char *dst = malloc(sizeof(*dst) * (1 + len));
 | 
				
			||||||
 | 
						if (!dst) return NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						len = wcsrtombs(dst, &src, 1 + len, NULL);
 | 
				
			||||||
 | 
						if (len == (size_t)-1) {
 | 
				
			||||||
 | 
							free(dst);
 | 
				
			||||||
 | 
							return NULL;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						return dst;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// From <https://en.cppreference.com/w/c/io/fwprintf#Notes>:
 | 
					// From <https://en.cppreference.com/w/c/io/fwprintf#Notes>:
 | 
				
			||||||
//
 | 
					//
 | 
				
			||||||
// While narrow strings provide snprintf, which makes it possible to determine
 | 
					// While narrow strings provide snprintf, which makes it possible to determine
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										21
									
								
								ui.c
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								ui.c
									
									
									
									
									
								
							@ -267,11 +267,10 @@ void uiTopic(const wchar_t *topic) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void uiTopicStr(const char *topic) {
 | 
					void uiTopicStr(const char *topic) {
 | 
				
			||||||
	size_t len = strlen(topic);
 | 
						wchar_t *wcs = ambstowcs(topic);
 | 
				
			||||||
	wchar_t wcs[1 + len];
 | 
						if (!wcs) err(EX_DATAERR, "ambstowcs");
 | 
				
			||||||
	len = mbstowcs(wcs, topic, 1 + len);
 | 
					 | 
				
			||||||
	if (len == (size_t)-1) err(EX_DATAERR, "mbstowcs");
 | 
					 | 
				
			||||||
	uiTopic(wcs);
 | 
						uiTopic(wcs);
 | 
				
			||||||
 | 
						free(wcs);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void uiLog(const wchar_t *line) {
 | 
					void uiLog(const wchar_t *line) {
 | 
				
			||||||
@ -350,16 +349,10 @@ static void delete(void) {
 | 
				
			|||||||
static void enter(void) {
 | 
					static void enter(void) {
 | 
				
			||||||
	if (line.end == line.buf) return;
 | 
						if (line.end == line.buf) return;
 | 
				
			||||||
	*line.end = L'\0';
 | 
						*line.end = L'\0';
 | 
				
			||||||
 | 
						char *str = awcstombs(line.buf);
 | 
				
			||||||
	const wchar_t *src = line.buf;
 | 
						if (!str) err(EX_DATAERR, "awcstombs");
 | 
				
			||||||
	size_t len = wcsrtombs(NULL, &src, 0, NULL);
 | 
						input(str);
 | 
				
			||||||
	if (len == (size_t)-1) err(EX_DATAERR, "wcsrtombs");
 | 
						free(str);
 | 
				
			||||||
 | 
					 | 
				
			||||||
	char buf[1 + len];
 | 
					 | 
				
			||||||
	len = wcsrtombs(buf, &src, sizeof(buf), NULL);
 | 
					 | 
				
			||||||
	if (len == (size_t)-1) err(EX_DATAERR, "wcsrtombs");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	input(buf);
 | 
					 | 
				
			||||||
	line.ptr = line.buf;
 | 
						line.ptr = line.buf;
 | 
				
			||||||
	line.end = line.buf;
 | 
						line.end = line.buf;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user