1#pragma once
  2
  3#include <stdio.h>
  4
  5#include <pw_types.h>
  6#include <pw_task.h>
  7
  8#ifdef __cplusplus
  9extern "C" {
 10#endif
 11
 12#define PW_SUCCESS                      0
 13#define PW_STATUS_VA_END                1  // used as a terminator for variadic arguments
 14#define PW_ERROR                        2  // basic error for use as custom error in pw_status macro; description is recommended
 15#define PW_ERROR_ERRNO                  3
 16#define PW_ERROR_OOM                    4
 17#define PW_ERROR_NOT_IMPLEMENTED        5
 18#define PW_ERROR_INCOMPATIBLE_TYPE      6
 19#define PW_ERROR_INTERFACE_NOT_DEFINED  7
 20#define PW_ERROR_EOF                    8
 21#define PW_ERROR_TIMEOUT                9
 22#define PW_ERROR_STRING_TOO_LONG       10
 23#define PW_ERROR_DATA_SIZE_TOO_BIG     11
 24#define PW_ERROR_INDEX_OUT_OF_RANGE    12
 25#define PW_ERROR_ITERATION_IN_PROGRESS 13
 26#define PW_ERROR_BAD_NUMBER            14
 27#define PW_ERROR_BAD_DATETIME          15
 28#define PW_ERROR_BAD_TIMESTAMP         16
 29#define PW_ERROR_NUMERIC_OVERFLOW      17
 30#define PW_ERROR_INCOMPLETE_UTF8       18
 31
 32// array errors
 33#define PW_ERROR_EXTRACT_FROM_EMPTY_ARRAY  19
 34#define PW_ERROR_DELETE_FROM_EMPTY_ARRAY   20
 35
 36// map errors
 37#define PW_ERROR_KEY_NOT_FOUND        21
 38
 39// File errors
 40#define PW_ERROR_FILE_ALREADY_OPENED  22
 41#define PW_ERROR_FD_ALREADY_SET       23
 42#define PW_ERROR_CANT_SET_FILENAME    24
 43#define PW_ERROR_FILE_CLOSED          25
 44#define PW_ERROR_NOT_REGULAR_FILE     26
 45#define PW_ERROR_UNBUFFERED_FILE      27
 46#define PW_ERROR_WRITE                28
 47
 48// StringIO errors
 49#define PW_ERROR_UNREAD_FAILED        29
 50
 51
 52uint16_t pw_define_status(char* status);
 53/*
 54 * Define status in the global table.
 55 * Return status code or PW_ERROR_OOM
 56 *
 57 * This function should be called from the very beginning of main() function
 58 * or from constructors that are called before main().
 59 */
 60
 61char* pw_status_str(uint16_t status_code);
 62/*
 63 * Get status string by status code.
 64 */
 65
 66
 67static inline bool pw_is_error(PwValuePtr value)
 68{
 69    if (pw_is_status(value)) {
 70        return value->is_error;
 71    } else {
 72        return false;
 73    }
 74}
 75
 76static inline bool pw_is_eof()
 77{
 78    if (!pw_is_status(&current_task->status)) {
 79        return false;
 80    }
 81    return current_task->status.status_code == PW_ERROR_EOF;
 82}
 83
 84static inline bool pw_is_errno(int _errno)
 85{
 86    if (!pw_is_status(&current_task->status)) {
 87        return false;
 88    }
 89    return current_task->status.status_code == PW_ERROR_ERRNO
 90        && current_task->status.pw_errno == _errno;
 91}
 92
 93static inline bool pw_is_timeout()
 94{
 95    if (!pw_is_status(&current_task->status)) {
 96        return false;
 97    }
 98    return current_task->status.status_code == PW_ERROR_TIMEOUT;
 99}
100
101static inline bool pw_is_va_end(PwValuePtr status)
102{
103    if (!pw_is_status(status)) {
104        return false;
105    }
106    return status->status_code == PW_STATUS_VA_END;
107}
108
109
110#define pw_set_status(_status, ...)  \
111    do {  \
112        pw_destroy(&current_task->status);  \
113        current_task->status = _status;  \
114        __VA_OPT__( _pw_set_status_desc(&current_task->status, __VA_ARGS__); )  \
115    } while (false)
116
117#define pw_set_status_desc(...)  \
118    _pw_set_status_desc(&current_task->status, __VA_ARGS__)
119
120void _pw_set_status_location(PwValuePtr status, char* file_name, unsigned line_number);
121void _pw_set_status_desc(PwValuePtr status, char* fmt, ...);
122void _pw_set_status_desc_ap(PwValuePtr status, char* fmt, va_list ap);
123/*
124 * Set description for status.
125 * If out of memory assign PW_ERROR_OOM to status.
126 */
127
128void pw_print_status(FILE* fp, PwValuePtr status);
129
130
131#ifdef __cplusplus
132}
133#endif