column.c
This file
Path
column.c
50 lines · 1.0 KiB · C
Last changed by
Add `--accept-nth` command line option to select column to outputbbb651before adoption33c9b8e
History of this fileTake it
1#include "column.h"23#include <assert.h>4#include <limits.h>5#include <stdlib.h>6#include <string.h>7#include <uchar.h>8#include <unistd.h>910#define LOG_MODULE "column"11#define LOG_ENABLE_DBG 012#include "log.h"13#include "xmalloc.h"1415char32_t *16nth_column(const char32_t *string, unsigned int nth) {17 assert(nth > 0);1819 const char32_t* column = string;2021 for (unsigned int i = 0;22 i < nth - 1;23 i++)24 {25 column = c32chr(column, U'\t');2627 if (column == NULL) {28 LOG_WARN("%ls: only %u column(s) available",29 (const wchar_t *)string, i + 1);30 column = U"";31 break;32 }3334 column++;35 }3637 char32_t *result = NULL;38 const char32_t *next_tab = c32chr(column, U'\t');3940 if (next_tab == NULL)41 result = xc32dup(column);42 else {43 const size_t len = next_tab - column;44 result = xmalloc((len + 1) * sizeof(char32_t));45 memcpy(result, column, len * sizeof(char32_t));46 result[len] = U'\0';47 }4849 return result;50}