/usr/include/fcitx-utils/utils.h is in fcitx-libs-dev 1:4.2.9.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 | /***************************************************************************
* Copyright (C) 2002~2005 by Yuking *
* yuking_net@sohu.com *
* Copyright (C) 2011~2012 by CSSlayer *
* wengxt@gmail.com *
* Copyright (C) 2012~2013 by Yichao Yu *
* yyc1992@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/
/**
* @defgroup FcitxUtils FcitxUtils
*
* Fcitx Utils contains a bunch of functions, with extend common c library.
* It contains uthash/utarray as macro based hash table and dynamic array.
*
* Some Fcitx path related function, includes fcitx_utils_get_fcitx_path()
* and fcitx_utils_get_fcitx_path_with_filename()
*
* Some string related function, providing split, join string list.
*
* A simple memory pool, if you need to allocate many small memory block, and
* only need to free them at once.
*
* Some log function, which prints the current file number, with printf format.
*
* Why Fcitx does not use GLib?
*
* The answer is glib contains a lot of function that fcitx doesn't need, and
* in order to keep fcitx-core only depends on libc,
* we provide some functions that are equivalent to glib ones in fcitx-utils.
*/
/**
* @addtogroup FcitxUtils
* @{
*/
/**
* @file utils.h
* @author CS Slayer wengxt@gmail.com
*
* Misc function for Fcitx.
*
*/
#ifndef _FCITX_UTILS_H_
#define _FCITX_UTILS_H_
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <fcitx-utils/utarray.h>
#include <fcitx-utils/uthash.h>
#include <sys/stat.h>
/**
* fcitx boolean
**/
typedef int32_t boolean;
#if !defined(__cplusplus) && !defined(FCITX_DONOT_DEFINE_TRUE_FALSE)
/**
* fcitx true
*/
#define true (1)
/**
* fcitx false
*/
#define false (0)
#endif
#define FCITX_INT_LEN ((int)(sizeof(int) * 2.5) + 2)
#define FCITX_LONG_LEN ((int)(sizeof(long) * 2.5) + 2)
#define FCITX_INT32_LEN (22)
#define FCITX_INT64_LEN (42)
#define fcitx_container_of(ptr, type, member) \
((type*)(((void*)(ptr)) - offsetof(type, member)))
#if (defined(__GNUC__) && (__GNUC__ > 2))
# define fcitx_expect(exp, var) __builtin_expect(exp, var)
#else
# define fcitx_expect(exp, var) (exp)
#endif
#define FCITX_ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define fcitx_likely(x) fcitx_expect(!!(x), 1)
#define fcitx_unlikely(x) fcitx_expect(!!(x), 0)
#ifdef __cplusplus
extern "C" {
#endif
typedef enum _FcitxTriState {
Tri_False = false,
Tri_True = true,
Tri_Unknown
} FcitxTriState;
extern const UT_icd *const fcitx_ptr_icd;
extern const UT_icd *const fcitx_str_icd;
extern const UT_icd *const fcitx_int_icd;
extern const UT_icd *const fcitx_int8_icd;
extern const UT_icd *const fcitx_int16_icd;
extern const UT_icd *const fcitx_int32_icd;
extern const UT_icd *const fcitx_int64_icd;
/**
* Function used to free the pointer
**/
typedef void (*FcitxDestroyNotify)(void *p);
/**
* Function used to free the content of a structure,
* DO NOT free the pointer itself
**/
typedef void (*FcitxCallBack)();
/**
* A hash set for string
**/
typedef struct _FcitxStringHashSet {
/**
* String in Hash Set
**/
char *name;
/**
* UT Hash handle
**/
UT_hash_handle hh;
} FcitxStringHashSet;
/**
* Custom bsearch, it can search the most near value.
*
* @param key
* @param base
* @param nmemb
* @param size
* @param accurate
* @param compar
*
* @return
*/
void *fcitx_utils_custom_bsearch(const void *key, const void *base,
size_t nmemb, size_t size, int accurate,
int (*compar)(const void *, const void *));
/**
* Fork twice to run as daemon
*
* @return void
**/
void fcitx_utils_init_as_daemon(void);
/**
* Count the file line count
*
* @param fpDict file pointer
* @return int line count
**/
int fcitx_utils_calculate_record_number(FILE* fpDict);
/**
* create empty string list
*
* @return UT_array*
**/
UT_array* fcitx_utils_new_string_list(void);
/**
* Split a string by delm
*
* @param str input string
* @param delm character as delimiter
* @return UT_array* a new utarray for store the split string
**/
UT_array* fcitx_utils_split_string(const char *str, char delm);
/**
* append a string with printf format
*
* @param list string list
* @param fmt printf fmt
* @return void
**/
void fcitx_utils_string_list_printf_append(UT_array* list, const char* fmt,...);
/**
* Join string list with delm
*
* @param list string list
* @param delm delm
* @return char* return string, need to be free'd
**/
char* fcitx_utils_join_string_list(UT_array* list, char delm);
/**
* check if a string list contains a specific string
*
* @param list string list
* @param scmp string to compare
*
* @return 1 for found, 0 for not found.
*
* @since 4.2.5
*/
int fcitx_utils_string_list_contains(UT_array* list, const char* scmp);
/**
* Helper function for free the SplitString Output
*
* @param list the SplitString Output
* @return void
* @see fcitx_utils_split_string
**/
void fcitx_utils_free_string_list(UT_array *list);
/**
* Free String Hash Set
*
* @param sset String Hash Set
* @return void
*
* @since 4.2.0
**/
void fcitx_utils_free_string_hash_set(FcitxStringHashSet* sset);
/**
* compare two string with strcmp
*
* @param sseta left
* @param ssetb right
* @return same as strcmp
*
* @since 4.2.8
**/
int fcitx_utils_string_hash_set_compare(FcitxStringHashSet* sseta, FcitxStringHashSet* ssetb);
/**
* insert to a string hash set
*
* @param sset string hash set
* @param str string
* @return FcitxStringHashSet*
*
* @since 4.2.7
**/
FcitxStringHashSet* fcitx_utils_string_hash_set_insert(FcitxStringHashSet* sset, const char* str);
/**
* insert string with specified length
*
* @param sset string hash set
* @param str string
* @param len length
* @return FcitxStringHashSet*
*
* @since 4.2.7
**/
FcitxStringHashSet* fcitx_utils_string_hash_set_insert_len(FcitxStringHashSet* sset, const char* str, size_t len);
/**
* check a string contains in string hash set or not
*
* @param sset string hash set
* @param str string
* @return boolean
*
* @since 4.2.7
**/
boolean fcitx_utils_string_hash_set_contains(FcitxStringHashSet* sset, const char* str);
/**
* remove a string from string hash set
*
* @param sset string hash set
* @param str string
* @return FcitxStringHashSet*
*
* @since 4.2.7
**/
FcitxStringHashSet* fcitx_util_string_hash_set_remove(FcitxStringHashSet* sset, const char* str);
/**
* join a string hash set with delimiter
*
* @param sset string hash set
* @param delim delimeter
* @return char*
*
* @since 4.2.7
**/
char* fcitx_utils_string_hash_set_join(FcitxStringHashSet* sset, char delim);
/**
* parse a string with delimiter
*
* @param str string
* @param delim delimiter
* @return FcitxStringHashSet*
*
* @since 4.2.7
**/
FcitxStringHashSet* fcitx_utils_string_hash_set_parse(const char* str, char delim);
/**
* Trim the input string's white space
*
* @param s input string
* @return char* new malloced string, need to free'd by caller
**/
char* fcitx_utils_trim(const char *s);
/**
* Malloc and memset all memory to zero
*
* @param bytes malloc size
* @return void* malloced pointer
**/
void* fcitx_utils_malloc0(size_t bytes);
#define fcitx_utils_new(TYPE) ((TYPE*) fcitx_utils_malloc0(sizeof(TYPE)))
static inline void*
_fcitx_utils_new_with_data(size_t base_size, const void *extra_data,
size_t extra_size)
{
void *res = fcitx_utils_malloc0(base_size + extra_size);
memcpy(((char*)res) + base_size, extra_data, extra_size);
return res;
}
static inline void*
_fcitx_utils_new_with_str(size_t base_size, const char *extra_str)
{
return _fcitx_utils_new_with_data(base_size, (const void*)extra_str,
strlen(extra_str) + 1);
}
#define fcitx_utils_new_with_data(TYPE, data, size) \
((TYPE*)_fcitx_utils_new_with_data(sizeof(TYPE), data, size))
#define fcitx_utils_new_with_str(TYPE, str) \
((TYPE*)_fcitx_utils_new_with_str(sizeof(TYPE), str))
#define fcitx_utils_new_with_data_member(TYPE, member, data, size) \
((TYPE*)_fcitx_utils_new_with_data(offsetof(TYPE, member), data, size))
#define fcitx_utils_new_with_str_member(TYPE, member, str) \
((TYPE*)_fcitx_utils_new_with_str(offsetof(TYPE, member), str))
/**
* Get Display number, Fcitx DBus and Socket are identified by display number.
*
* @return int
**/
int fcitx_utils_get_display_number(void);
/**
* Get current language code, result need to be free'd
* It will check LC_CTYPE, LC_ALL, LANG, for current language code.
*
* @return char*
**/
char* fcitx_utils_get_current_langcode(void);
/**
* check the current locale is utf8 or not
*
* @return int
**/
int fcitx_utils_current_locale_is_utf8(void);
/**
* Get Current Process Name, implementation depends on OS,
* Always return a string need to be free'd, if cannot get
* current process name, it will return "".
*
* @return char*
**/
char* fcitx_utils_get_process_name(void);
/**
* @brief check a process is running or not
*
* @param pid pid
* @return 1 for exists or error, 0 for non exists
**/
int fcitx_utils_pid_exists(pid_t pid);
/**
* Get Fcitx install path, need be free'd
* All possible type includes:
* datadir
* pkgdatadir
* bindir
* libdir
* localedir
*
* It's determined at compile time, and can be changed via environment variable: FCITXDIR
*
* It will only return NULL while the type is invalid.
*
* @param type path type
*
* @return char*
*
* @since 4.2.1
*/
char* fcitx_utils_get_fcitx_path(const char* type);
/**
* Get fcitx install path with file name, need to be free'd
*
* It's just simply return the path/filename string.
*
* It will only return NULL while the type is invalid.
*
* @param type path type
* @param filename filename
*
* @return char*
*
* @see fcitx_utils_get_fcitx_path
*
* @since 4.2.1
*/
char* fcitx_utils_get_fcitx_path_with_filename(const char* type, const char* filename);
/**
* lanunch fcitx's tool
*
* @param name tool's name
* @param arg single arg
*
* @return void
*
* @since 4.2.6
*/
void fcitx_utils_launch_tool(const char* name, const char* arg);
/**
* launch fcitx-configtool
*
* @return void
**/
void fcitx_utils_launch_configure_tool(void);
/**
* launch fcitx-configtool for an addon
*
* @return void
**/
void fcitx_utils_launch_configure_tool_for_addon(const char* addon);
/**
* helper function to execute fcitx -r
*
* @return void
**/
void fcitx_utils_launch_restart(void);
/**
* helper function to execute in place
*
* @return void
**/
void fcitx_utils_restart_in_place(void);
/**
* @brief launch a process
*
* @param args argument and command
* @return void
*
* @since 4.2.5
**/
void fcitx_utils_start_process(char** args);
/**
* output backtrace to stderr, need to enable backtrace, this function
* will be signal safe since 4.2.7, if you want to use it in debug,
* you'd better call fflush(stderr) before call to it.
*
* @return void
*
* @since 4.2.5
**/
void fcitx_utils_backtrace();
/**
* @brief get bool environment var for convenience.
*
* @param name var name
* @param defval default value
*
* @return value of var
*
* @since 4.2.6
*/
int fcitx_utils_get_boolean_env(const char *name, int defval);
/**
* if obj is null, free it, after that, if str is NULL set it with NULL,
* if str is not NULL, set it with strdup(str)
*
* @param obj object string
* @param str source string
* @return void
**/
void fcitx_utils_string_swap(char** obj, const char* str);
void fcitx_utils_string_swap_with_len(char** obj,
const char* str, size_t len);
/**
* similar with strcmp, but can handle the case that a or b is null.
* NULL < not NULL and NULL == NULL
*
* @param a string a
* @param b string b
* @return same as rule of strcmp
*/
int fcitx_utils_strcmp0(const char* a, const char* b);
/**
* similar with fcitx_utils_strcmp0, but empty string will be considered
* equals to NULL in this case.
* NULL == empty, and empty < not empty
*
* @param a string a
* @param b string b
* @return same as rule of strcmp
*/
int fcitx_utils_strcmp_empty(const char* a, const char* b);
/** free a pointer if it's not NULL */
static inline void
fcitx_utils_free(void *ptr)
{
if (ptr)
free(ptr);
}
#define fcitx_utils_read(fp, p, type) \
fcitx_utils_read_ ## type(fp, p)
#define fcitx_utils_write(fp, p, type) \
fcitx_utils_write_ ## type(fp, p)
/**
* read a little endian 32bit unsigned int from a file
*
* @param fp FILE* to read from
* @param p return the integer read
* @return 1 on success, 0 on error
* @since 4.2.6
**/
size_t fcitx_utils_read_uint32(FILE *fp, uint32_t *p);
/**
* read a little endian 32bit int from a file
*
* @param fp FILE* to read from
* @param p return the integer read
* @return 1 on success, 0 on error
* @since 4.2.6
**/
static inline size_t
fcitx_utils_read_int32(FILE *fp, int32_t *p)
{
return fcitx_utils_read_uint32(fp, (uint32_t*)p);
}
/**
* write a little endian 32bit int to a file
*
* @param fp FILE* to write to
* @param i int to write in host endian
* @return 1 on success, 0 on error
* @since 4.2.6
**/
size_t fcitx_utils_write_uint32(FILE *fp, uint32_t i);
/**
* write a little endian 32bit unsigned int to a file
*
* @param fp FILE* to write to
* @param i int to write in host endian
* @return 1 on success, 0 on error
* @since 4.2.6
**/
static inline size_t
fcitx_utils_write_int32(FILE *fp, int32_t i)
{
return fcitx_utils_write_uint32(fp, (uint32_t)i);
}
/**
* read a little endian 64bit unsigned int from a file
*
* @param fp FILE* to read from
* @param p return the integer read
* @return 1 on success, 0 on error
* @since 4.2.6
**/
size_t fcitx_utils_read_uint64(FILE *fp, uint64_t *p);
/**
* read a little endian 64bit int from a file
*
* @param fp FILE* to read from
* @param p return the integer read
* @return 1 on success, 0 on error
* @since 4.2.6
**/
static inline size_t
fcitx_utils_read_int64(FILE *fp, int64_t *p)
{
return fcitx_utils_read_uint64(fp, (uint64_t*)p);
}
/**
* write a little endian 64bit int to a file
*
* @param fp FILE* to write
* @param i int to write in host endian
* @return 1 on success, 0 on error
* @since 4.2.6
**/
size_t fcitx_utils_write_uint64(FILE *fp, uint64_t i);
/**
* write a little endian 64bit unsigned int to a file
*
* @param fp FILE* to write to
* @param i int to write in host endian
* @return 1 on success, 0 on error
* @since 4.2.6
**/
static inline size_t
fcitx_utils_write_int64(FILE *fp, int64_t i)
{
return fcitx_utils_write_uint64(fp, (uint64_t)i);
}
/**
* read a little endian 16bit unsigned int from a file
*
* @param fp FILE* to read from
* @param p return the integer read
* @return 1 on success, 0 on error
* @since 4.2.6
**/
size_t fcitx_utils_read_uint16(FILE *fp, uint16_t *p);
/**
* read a little endian 16bit int from a file
*
* @param fp FILE* to read from
* @param p return the integer read
* @return 1 on success, 0 on error
* @since 4.2.6
**/
static inline size_t
fcitx_utils_read_int16(FILE *fp, int16_t *p)
{
return fcitx_utils_read_uint16(fp, (uint16_t*)p);
}
/**
* write a little endian 16bit int to a file
*
* @param fp FILE* to write to
* @param i int to write in host endian
* @return 1 on success, 0 on error
* @since 4.2.6
**/
size_t fcitx_utils_write_uint16(FILE *fp, uint16_t i);
/**
* write a little endian 16bit unsigned int to a file
*
* @param fp FILE* to write to
* @param i int to write in host endian
* @return 1 on success, 0 on error
* @since 4.2.6
**/
static inline size_t
fcitx_utils_write_int16(FILE *fp, int16_t i)
{
return fcitx_utils_write_uint16(fp, (uint16_t)i);
}
size_t fcitx_utils_str_lens(size_t n, const char **str_list,
size_t *size_list);
void fcitx_utils_cat_str(char *out, size_t n, const char **str_list,
const size_t *size_list);
void fcitx_utils_cat_str_with_len(char *out, size_t len, size_t n,
const char **str_list,
const size_t *size_list);
#define fcitx_utils_cat_str_simple(out, n, str_list) do { \
size_t __tmp_size_list[n]; \
fcitx_utils_str_lens(n, str_list, __tmp_size_list); \
fcitx_utils_cat_str(out, n, str_list, __tmp_size_list); \
} while (0)
#define fcitx_utils_cat_str_simple_with_len(out, len, n, str_list) do { \
size_t __tmp_size_list[n]; \
fcitx_utils_str_lens(n, str_list, __tmp_size_list); \
fcitx_utils_cat_str_with_len(out, len, n, str_list, __tmp_size_list); \
} while (0)
#define fcitx_utils_local_cat_str(dest, len, strs...) \
const char *__str_list_##dest[] = {strs}; \
size_t __size_list_##dest[sizeof((const char*[]){strs}) / sizeof(char*)]; \
fcitx_utils_str_lens(sizeof((const char*[]){strs}) / sizeof(char*), \
__str_list_##dest, __size_list_##dest); \
char dest[len]; \
fcitx_utils_cat_str_with_len(dest, len, \
sizeof((const char*[]){strs}) / sizeof(char*), \
__str_list_##dest, __size_list_##dest)
#define fcitx_utils_alloc_cat_str(dest, strs...) do { \
const char *__str_list[] = {strs}; \
size_t __cat_str_n = sizeof(__str_list) / sizeof(char*); \
size_t __size_list[sizeof(__str_list) / sizeof(char*)]; \
size_t __total_size = fcitx_utils_str_lens(__cat_str_n, \
__str_list, __size_list); \
dest = malloc(__total_size); \
fcitx_utils_cat_str(dest, __cat_str_n, \
__str_list, __size_list); \
} while (0)
#define fcitx_utils_set_cat_str(dest, strs...) do { \
const char *__str_list[] = {strs}; \
size_t __cat_str_n = sizeof(__str_list) / sizeof(char*); \
size_t __size_list[sizeof(__str_list) / sizeof(char*)]; \
size_t __total_size = fcitx_utils_str_lens(__cat_str_n, \
__str_list, __size_list); \
dest = realloc(dest, __total_size); \
fcitx_utils_cat_str(dest, __cat_str_n, \
__str_list, __size_list); \
} while (0)
static inline int fcitx_utils_isdir(const char *path)
{
struct stat stats;
return (stat(path, &stats) == 0 && S_ISDIR(stats.st_mode) &&
access(path, R_OK | X_OK) == 0);
}
static inline int fcitx_utils_isreg(const char *path)
{
struct stat stats;
return (stat(path, &stats) == 0 && S_ISREG(stats.st_mode) &&
access(path, R_OK) == 0);
}
static inline int fcitx_utils_islnk(const char *path)
{
struct stat stats;
return stat(path, &stats) == 0 && S_ISLNK(stats.st_mode);
}
char *fcitx_utils_set_str_with_len(char *res, const char *str, size_t len);
static inline char*
fcitx_utils_set_str(char *res, const char *str)
{
return fcitx_utils_set_str_with_len(res, str, strlen(str));
}
char fcitx_utils_unescape_char(char c);
char *fcitx_utils_unescape_str_inplace(char *str);
char *fcitx_utils_set_unescape_str(char *res, const char *str);
#define FCITX_CHAR_NEED_ESCAPE "\a\b\f\n\r\t\e\v\'\"\\"
char fcitx_utils_escape_char(char c);
char *fcitx_utils_set_escape_str_with_set(char *res, const char *str,
const char *set);
static inline char*
fcitx_utils_set_escape_str(char *res, const char *str)
{
return fcitx_utils_set_escape_str_with_set(res, str, NULL);
}
UT_array *fcitx_utils_append_split_string(UT_array *list, const char* str,
const char *delm);
static inline UT_array*
fcitx_utils_append_lines(UT_array *list, const char* str)
{
return fcitx_utils_append_split_string(list, str, "\n");
}
UT_array *fcitx_utils_string_list_append_no_copy(UT_array *list, char *str);
UT_array *fcitx_utils_string_list_append_len(UT_array *list,
const char *str, size_t len);
static inline uintptr_t
fcitx_utils_align_to(uintptr_t len, uintptr_t align)
{
uintptr_t left;
if ((left = len % align))
return len + align - left;
return len;
}
#ifdef __GCC_HAVE_SYNC_COMPARE_AND_SWAP_4
#define __FCITX_ATOMIC_USE_SYNC_FETCH
#elif defined __has_builtin
#if __has_builtin(__sync_fetch_and_add) && \
__has_builtin(__sync_fetch_and_and) && \
__has_builtin(__sync_fetch_and_xor) && \
__has_builtin(__sync_fetch_and_or)
#define __FCITX_ATOMIC_USE_SYNC_FETCH
#endif
#endif
#ifdef __FCITX_ATOMIC_USE_SYNC_FETCH
#define FCITX_UTIL_DECLARE_ATOMIC(name, type) \
type (fcitx_utils_atomic_##name)(volatile type *atomic, type val); \
static inline type \
__fcitx_utils_atomic_##name(volatile type *atomic, type val) \
{ \
return __sync_fetch_and_##name(atomic, val); \
}
#else
#define FCITX_UTIL_DECLARE_ATOMIC(name, type) \
type (fcitx_utils_atomic_##name)(volatile type *atomic, type val); \
static inline type \
__fcitx_utils_atomic_##name(volatile type *atomic, type val) \
{ \
return (fcitx_utils_atomic_##name)(atomic, val); \
}
#endif
FCITX_UTIL_DECLARE_ATOMIC(add, int32_t)
FCITX_UTIL_DECLARE_ATOMIC(and, uint32_t)
FCITX_UTIL_DECLARE_ATOMIC(or, uint32_t)
FCITX_UTIL_DECLARE_ATOMIC(xor, uint32_t)
#define fcitx_utils_atomic_add(atomic, val) \
__fcitx_utils_atomic_add(atomic, val)
#define fcitx_utils_atomic_and(atomic, val) \
__fcitx_utils_atomic_and(atomic, val)
#define fcitx_utils_atomic_or(atomic, val) \
__fcitx_utils_atomic_or(atomic, val)
#define fcitx_utils_atomic_xor(atomic, val) \
__fcitx_utils_atomic_xor(atomic, val)
#undef FCITX_UTIL_DECLARE_ATOMIC
static inline void*
fcitx_array_eltptr(UT_array *ary, int i)
{
if (fcitx_unlikely(i < 0))
return NULL;
return (void*)utarray_eltptr(ary, (unsigned)i);
}
static inline void
fcitx_array_insert(UT_array *ary, void *p, int i)
{
if (fcitx_unlikely(i < 0))
return;
utarray_insert(ary, p, (unsigned)i);
}
static inline void
fcitx_array_inserta(UT_array *ary, UT_array *ary2, int i)
{
if (fcitx_unlikely(i < 0))
return;
utarray_inserta(ary, ary2, (unsigned)i);
}
static inline void
fcitx_array_move(UT_array *ary, int from, int to)
{
if (fcitx_unlikely(from < 0 || to < 0))
return;
utarray_move(ary, (unsigned)from, (unsigned)to);
}
static inline void
fcitx_array_erase(UT_array *ary, int pos, int len)
{
if (fcitx_unlikely(pos < 0 || len < 0))
return;
utarray_erase(ary, (unsigned)pos, (unsigned)len);
}
static inline void
fcitx_array_resize(UT_array *ary, int len)
{
if (fcitx_unlikely(len < 0))
return;
utarray_resize(ary, (unsigned)len);
}
/**
* rely on compiler to optimize out unnecessary memcpy's and branches here
* (and both gcc and clang can do it very well).
**/
#define __FCITX_BYTE_CAST(new_val, old_type, old_val) do { \
old_type __fx_byte_cast_old = (old_val); \
memset(&new_val, 0, sizeof(new_val)); \
memcpy(&new_val, &__fx_byte_cast_old, \
sizeof(old_type) < sizeof(new_val) ? \
sizeof(old_type) : sizeof(new_val)); \
} while (0)
/**
* cast a arbitrary type to integer. for pointers and integers types,
* this is compatible with normal type casting for other types
* (especially float), this will not loss any information
* as long as the size of the types is not larger than the integer type
**/
#define _FCITX_CAST_TO_INT(new_type, new_val, old_type, old_val) do { \
if (sizeof(old_type) <= 1) { \
int8_t __fx_cast_to_int_tmp; \
__FCITX_BYTE_CAST(__fx_cast_to_int_tmp, old_type, old_val); \
new_val = (new_type)__fx_cast_to_int_tmp; \
} else if (sizeof(old_type) <= 2) { \
int16_t __fx_cast_to_int_tmp; \
__FCITX_BYTE_CAST(__fx_cast_to_int_tmp, old_type, old_val); \
new_val = (new_type)__fx_cast_to_int_tmp; \
} else if (sizeof(old_type) <= 4) { \
int32_t __fx_cast_to_int_tmp; \
__FCITX_BYTE_CAST(__fx_cast_to_int_tmp, old_type, old_val); \
new_val = (new_type)__fx_cast_to_int_tmp; \
} else { \
int64_t __fx_cast_to_int_tmp; \
__FCITX_BYTE_CAST(__fx_cast_to_int_tmp, old_type, old_val); \
new_val = (new_type)__fx_cast_to_int_tmp; \
} \
} while (0)
#define _FCITX_CAST_TO_PTR(new_val, old_type, old_val) do { \
intptr_t __fx_cast_to_ptr_tmp; \
_FCITX_CAST_TO_INT(intptr_t, __fx_cast_to_ptr_tmp, \
old_type, old_val); \
new_val = (void*)__fx_cast_to_ptr_tmp; \
} while (0)
#define FCITX_DEF_CAST_TO_INT(new_type, new_val, old_type, old_val) \
new_type new_val; \
_FCITX_CAST_TO_INT(new_type, new_val, old_type, old_val)
#define FCITX_DEF_CAST_TO_PTR(new_val, old_type, old_val) \
void *new_val; \
_FCITX_CAST_TO_PTR(new_val, old_type, old_val)
#define FCITX_RETURN_AS_INT(new_type, old_type, old_val) do { \
FCITX_DEF_CAST_TO_INT(new_type, __fx_return_as_int_tmp, \
old_type, old_val); \
return __fx_return_as_int_tmp; \
} while (0)
#define FCITX_RETURN_AS_PTR(old_type, old_val) do { \
FCITX_DEF_CAST_TO_PTR(__fx_return_as_ptr_tmp, \
old_type, old_val); \
return __fx_return_as_ptr_tmp; \
} while (0)
#define __FCITX_DEF_CAST_TO_PTR_FUNC(name, type) \
static inline void* \
fcitx_utils_##name##_to_ptr(type value) \
{ \
FCITX_RETURN_AS_PTR(type, value); \
}
__FCITX_DEF_CAST_TO_PTR_FUNC(float, float)
__FCITX_DEF_CAST_TO_PTR_FUNC(int, int)
__FCITX_DEF_CAST_TO_PTR_FUNC(intptr, intptr_t)
__FCITX_DEF_CAST_TO_PTR_FUNC(int8, int8_t)
__FCITX_DEF_CAST_TO_PTR_FUNC(int16, int16_t)
__FCITX_DEF_CAST_TO_PTR_FUNC(int32, int32_t)
__FCITX_DEF_CAST_TO_PTR_FUNC(ssize, ssize_t)
__FCITX_DEF_CAST_TO_PTR_FUNC(uint, unsigned int)
__FCITX_DEF_CAST_TO_PTR_FUNC(uintptr, uintptr_t)
__FCITX_DEF_CAST_TO_PTR_FUNC(uint8, uint8_t)
__FCITX_DEF_CAST_TO_PTR_FUNC(uint16, uint16_t)
__FCITX_DEF_CAST_TO_PTR_FUNC(uint32, uint32_t)
__FCITX_DEF_CAST_TO_PTR_FUNC(size, size_t)
__FCITX_DEF_CAST_TO_PTR_FUNC(constptr, const void*)
#define _FCITX_CAST_FROM_INT(new_type, new_val, old_type, old_val) do { \
if (sizeof(new_type) <= 1) { \
int8_t __fx_cast_from_int_tmp = (int8_t)(old_val); \
__FCITX_BYTE_CAST(new_val, int8_t, __fx_cast_from_int_tmp); \
} else if (sizeof(new_type) <= 2) { \
int16_t __fx_cast_from_int_tmp = (int16_t)(old_val); \
__FCITX_BYTE_CAST(new_val, int16_t, __fx_cast_from_int_tmp); \
} else if (sizeof(new_type) <= 4) { \
int32_t __fx_cast_from_int_tmp = (int32_t)(old_val); \
__FCITX_BYTE_CAST(new_val, uint32_t, __fx_cast_from_int_tmp); \
} else { \
int64_t __fx_cast_from_int_tmp = (int64_t)(old_val); \
__FCITX_BYTE_CAST(new_val, int64_t, __fx_cast_from_int_tmp); \
} \
} while (0)
#define _FCITX_CAST_FROM_PTR(new_type, new_val, p) \
_FCITX_CAST_FROM_INT(new_type, new_val, intptr_t, (intptr_t)p)
#define FCITX_DEF_CAST_FROM_INT(new_type, new_val, old_type, old_val) \
new_type new_val; \
_FCITX_CAST_FROM_INT(new_type, new_val, old_type, old_val)
#define FCITX_DEF_CAST_FROM_PTR(new_type, new_val, p) \
FCITX_DEF_CAST_FROM_INT(new_type, new_val, intptr_t, (intptr_t)p)
#define FCITX_RETURN_FROM_INT(new_type, old_type, old_val) do { \
FCITX_DEF_CAST_FROM_INT(new_type, __fx_return_from_int_tmp, \
old_type, old_val); \
return __fx_return_from_int_tmp; \
} while (0)
#define FCITX_RETURN_FROM_PTR(new_type, old_val) do { \
FCITX_DEF_CAST_FROM_PTR(new_type, __fx_return_from_ptr_tmp, \
old_val); \
return __fx_return_from_ptr_tmp; \
} while (0)
#define __FCITX_DEF_CAST_FROM_PTR_FUNC(name, type) \
static inline type \
fcitx_utils_ptr_to_##name(void *value) \
{ \
FCITX_RETURN_FROM_PTR(type, value); \
}
__FCITX_DEF_CAST_FROM_PTR_FUNC(float, float)
__FCITX_DEF_CAST_FROM_PTR_FUNC(int, int)
__FCITX_DEF_CAST_FROM_PTR_FUNC(intptr, intptr_t)
__FCITX_DEF_CAST_FROM_PTR_FUNC(int8, int8_t)
__FCITX_DEF_CAST_FROM_PTR_FUNC(int16, int16_t)
__FCITX_DEF_CAST_FROM_PTR_FUNC(int32, int32_t)
__FCITX_DEF_CAST_FROM_PTR_FUNC(ssize, ssize_t)
__FCITX_DEF_CAST_FROM_PTR_FUNC(uint, unsigned int)
__FCITX_DEF_CAST_FROM_PTR_FUNC(uintptr, uintptr_t)
__FCITX_DEF_CAST_FROM_PTR_FUNC(uint8, uint8_t)
__FCITX_DEF_CAST_FROM_PTR_FUNC(uint16, uint16_t)
__FCITX_DEF_CAST_FROM_PTR_FUNC(uint32, uint32_t)
__FCITX_DEF_CAST_FROM_PTR_FUNC(size, size_t)
#ifdef __cplusplus
}
#endif
#endif
/**
* @}
*/
// kate: indent-mode cstyle; space-indent on; indent-width 0;
|