chsrc/lib/xy.h
2025-03-06 17:03:54 +08:00

193 lines
5.9 KiB
C

/** ------------------------------------------------------------
* Copyright © 2023-2025 Aoran Zeng, Heng Guo
* SPDX-License-Identifier: MIT
* -------------------------------------------------------------
* Lib Name : xy
* Lib Authors : Aoran Zeng <ccmywish@qq.com>
* | Heng Guo <2085471348@qq.com>
* Contributors : juzeon <skyjuzheng@gmail.com>
* |
* Created On : <2023-08-28>
* Last Modified : <2025-03-06>
*
* xy: 襄阳、咸阳
* Corss-Platform C lib for CLI applications in Ruby & Perl flavor
* ------------------------------------------------------------*/
#ifndef XY_H
#define XY_H
#define _XY_Version "v0.1.5-2025/03/06"
#define _XY_Maintain_URL "https://gitee.com/RubyMetric/chsrc/blob/main/lib/xy.h"
#include <assert.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
// #define NDEBUG
/* Global */
extern bool xy_enable_color; /* Default value defined in xy.c */
#ifdef _WIN32
#define XY_On_Windows 1
#define xy_on_windows true
#define xy_on_linux false
#define xy_on_macos false
#define xy_on_bsd false
#define xy_os_devnull "nul"
#include <windows.h>
#define xy_useutf8() SetConsoleOutputCP (65001)
#elif defined(__linux__) || defined(__linux)
#define XY_On_Linux 1
#define xy_on_windows false
#define xy_on_linux true
#define xy_on_macos false
#define xy_on_bsd false
#define xy_os_devnull "/dev/null"
#define xy_useutf8()
#elif defined(__APPLE__)
#define XY_On_macOS 1
#define xy_on_windows false
#define xy_on_linux false
#define xy_on_macos true
#define xy_on_bsd false
#define xy_os_devnull "/dev/null"
#define xy_useutf8()
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
#define XY_On_BSD 1
#define xy_on_windows false
#define xy_on_linux false
#define xy_on_macos false
#define xy_on_bsd true
#define xy_os_devnull "/dev/null"
#define xy_useutf8()
#endif
/******************************************************
* Convenience
******************************************************/
void putf (double n);
void puti (long long n);
void putb (bool n);
void print (const char *s);
void println (const char *s);
void say (const char *s);
void br ();
void p (const char *s);
#define xy_arylen(x) (sizeof (x) / sizeof (x[0]))
/******************************************************
* Assertion
******************************************************/
#define assert_str(a, b) assert (xy_streql ((a), (b)))
#define xy_unsupported() assert(!"Unsuppoted")
#define xy_unimplemented() assert(!"Unimplemented temporarily")
#define xy_unreached() assert(!"This code shouldn't be reached")
/******************************************************
* Memory
******************************************************/
void * xy_malloc0 (size_t size);
/******************************************************
* String
******************************************************/
char * xy_str_gsub (const char *str, const char *pat, const char *replace);
char * xy_2strjoin (const char *str1, const char *str2);
char * xy_strjoin (unsigned int count, ...);
char * xy_strdup (const char *str);
bool xy_streql (const char *str1, const char *str2);
char * xy_str_to_quietcmd (const char *cmd);
bool xy_str_end_with (const char *str, const char *suffix);
bool xy_str_start_with (const char *str, const char *prefix);
char * xy_str_delete_prefix (const char *str, const char *prefix);
char * xy_str_delete_suffix (const char *str, const char *suffix);
char * xy_str_strip (const char *str);
char * xy_str_to_bold(const char *str);
char * xy_str_to_faint(const char *str);
char * xy_str_to_italic(const char *str);
char * xy_str_to_underline(const char *str);
char * xy_str_to_blink(const char *str);
char * xy_str_to_cross(const char *str);
char * xy_str_to_red(const char *str);
char * xy_str_to_green(const char *str);
char * xy_str_to_yellow(const char *str);
char * xy_str_to_blue(const char *str);
char * xy_str_to_magenta(const char *str);
char * xy_str_to_purple(const char *str);
char * xy_str_to_cyan(const char *str);
/******************************************************
* Logging
******************************************************/
void xy_log (const char *prompt, const char *str);
void xy_succ(const char *prompt, const char *str);
void xy_info(const char *prompt, const char *str);
void xy_warn(const char *prompt, const char *str);
void xy_error(const char *prompt, const char *str);
void xy_log_brkt (const char *prompt1, const char *prompt2, const char *content);
void xy_succ_brkt(const char *prompt1, const char *prompt2, const char *content);
void xy_info_brkt(const char *prompt1, const char *prompt2, const char *content);
void xy_warn_brkt(const char *prompt1, const char *prompt2, const char *content);
void xy_error_brkt(const char *prompt1,const char *prompt2, const char *content);
void xy_log_brkt_to (const char *prompt, const char *content, FILE *stream);
/******************************************************
* System
******************************************************/
char * xy_run_iter (const char *cmd, unsigned long n, void (*iter_func) (const char *));
char * xy_run (const char *cmd, unsigned long n);
bool xy_file_exist (const char *path);
bool xy_dir_exist (const char *path);
char * xy_normalize_path (const char *path);
char * xy_parent_dir (const char *path);
/******************************************************
* OS specific
******************************************************/
#define xy_zshrc "~/.zshrc"
#define xy_bashrc "~/.bashrc"
#define xy_fishrc "~/.config/fish/config.fish"
char * xy_os_home ();
char * xy_win_powershell_profile ();
char * xy_win_powershellv5_profile ();
#endif