(Not A Real) Warzone

Pascal-ish strings

The Pascal programming language has length-prefixed string type. Let's try it in C.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define CSTR(lps) lps + sizeof(size_t)
#define CLEN(lps) *((size_t *)lps)

char * new_length_prefixed_string(const char * s) {
  if (s == NULL) return NULL;
  size_t len = strlen(s);
  char * lps = calloc(sizeof(size_t) + len + 1, 1);
  if (lps == NULL) return NULL;
  memcpy(CSTR(lps), s, len);
  CLEN(lps) = len;
  return lps;
}

int main(void) {
  char * lps = new_length_prefixed_string("Hello!");
  if (lps == NULL) return -1;
  printf("String: %s\nLength: %zu\n", CSTR(lps), CLEN(lps));	
  free(lps);
  return 0;
}

Generics in C

Just a simple struct and macro trick.

#include <stdint.h>
#include <stdio.h>

#define GENERIC_VALUE(generic, type) *((type*)generic.value)

typedef struct {
  uint8_t value[8]; // 8 bytes for 64 bit values
} Generic;


int main(void) {
  Generic T = {0};

  GENERIC_VALUE(T, int) = 10;
  printf("int: %d\n", GENERIC_VALUE(T, int));
  GENERIC_VALUE(T, float) = 9.9999F;
  printf("float: %f\n", GENERIC_VALUE(T, float));
  GENERIC_VALUE(T, char*) = "Hello!"; 
  printf("string: %s\n", GENERIC_VALUE(T, char*));
  
  return 0;
}

Win10 UTF-8 locale

"Starting in Windows 10 build 17134 (April 2018 Update), the Universal C Runtime supports using a UTF-8 code page. This means that char strings passed to C runtime functions will expect strings in the UTF-8 encoding. To enable UTF-8 mode, use "UTF-8" as the code page when using setlocale. For example, setlocale(LC_ALL, ".utf8") will use the current default Windows ANSI code page (ACP) for the locale and UTF-8 for the code page. After calling setlocale(LC_ALL, ".UTF8"), you may pass "😊" to mbtowcs and it will be properly translated to a wchar_t string, whereas previously there was not a locale setting available to do this." – https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale

PinePhone

My PinePhone Manjaro Community Edition phone has just arrived. It's a massive dissapointment. The hardware is way below expectations, the available operating systems and software are pre-alpha quality. $200 + shipping cost are down the drain...

wchar_t

Using wide characters and the unicode enabled functions is Win32, you can display proper texts in the cmd shell regardless of codepage settings.

wchar_t buffer[16];
StringCchCopyW(buffer, 16, L"Őrß");
HANDLE std = GetStdHandle(STD_OUTPUT_HANDLE);
WriteConsoleW(std, buffer, wcslen(buffer), NULL, NULL);
CloseHandle(std);