Annotation of parser3/src/include/pa_common.h, revision 1.190

1.15      paf         1: /** @file
1.16      paf         2:        Parser: commonly used functions.
                      3: 
1.188     moko        4:        Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com)
1.74      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: */
                      7: 
                      8: #ifndef PA_COMMON_H
                      9: #define PA_COMMON_H
1.78      paf        10: 
1.190   ! moko       11: #define IDENT_PA_COMMON_H "$Id: pa_common.h,v 1.189 2020/12/20 20:45:24 moko Exp $"
1.1       paf        12: 
1.33      paf        13: #include "pa_string.h"
1.93      paf        14: #include "pa_hash.h"
1.1       paf        15: 
1.128     misha      16: class Request;
                     17: 
1.120     misha      18: // defines
1.164     moko       19: #define HTTP_STATUS            "status"
1.140     misha      20: #define HTTP_STATUS_CAPITALIZED        "Status"
                     21: 
1.164     moko       22: #define HTTP_CONTENT_LENGTH            "content-length"
1.136     misha      23: 
1.159     moko       24: #define HTTP_CONTENT_TYPE              "content-type"
                     25: #define HTTP_CONTENT_TYPE_UPPER                "CONTENT-TYPE"
1.138     misha      26: #define HTTP_CONTENT_TYPE_CAPITALIZED  "Content-Type"
1.120     misha      27: 
1.159     moko       28: #define CONTENT_DISPOSITION            "content-disposition"
                     29: #define CONTENT_DISPOSITION_UPPER      "CONTENT-DISPOSITION"
1.138     misha      30: #define CONTENT_DISPOSITION_CAPITALIZED        "Content-Disposition"
1.164     moko       31: 
                     32: #define CONTENT_DISPOSITION_ATTACHMENT "attachment"
                     33: #define CONTENT_DISPOSITION_INLINE     "inline"
1.134     misha      34: #define CONTENT_DISPOSITION_FILENAME_NAME "filename"
                     35: 
1.164     moko       36: #define HTTP_CONTENT_TYPE_FORM_URLENCODED      "application/x-www-form-urlencoded"
                     37: #define HTTP_CONTENT_TYPE_MULTIPART_FORMDATA   "multipart/form-data"
                     38: #define HTTP_CONTENT_TYPE_MULTIPART_RELATED    "multipart/related"
                     39: #define HTTP_CONTENT_TYPE_MULTIPART_MIXED      "multipart/mixed"
                     40: 
1.134     misha      41: const String content_disposition_filename_name(CONTENT_DISPOSITION_FILENAME_NAME);
                     42: 
1.132     misha      43: #define HASH_ORDER
                     44: 
                     45: #ifdef HASH_ORDER
                     46: #undef PA_HASH_CLASS
                     47: #include "pa_hash.h"
                     48: #endif
                     49: 
1.87      paf        50: class Value;
1.132     misha      51: typedef HASH_STRING<Value*> HashStringValue;
1.64      paf        52: 
                     53: // replace system s*nprintf with our versions
1.69      paf        54: #undef vsnprintf 
1.167     moko       55: int pa_vsnprintf(char *, size_t, const char* , va_list);
                     56: #define vsnprintf pa_vsnprintf 
1.64      paf        57: #undef snprintf
1.167     moko       58: int pa_snprintf(char *, size_t, const char* , ...);
                     59: #define snprintf pa_snprintf
1.63      paf        60: 
1.153     moko       61: #ifdef _MSC_VER
1.17      paf        62: 
                     63: //access
                     64: #define F_OK 0
                     65: #define X_OK 1
                     66: #define W_OK 2
                     67: #define R_OK 4
                     68: 
1.20      paf        69: #ifndef strcasecmp
                     70: #      define strcasecmp _stricmp
                     71: #endif
1.2       paf        72: 
1.157     moko       73: #ifndef strncasecmp
                     74: #      define strncasecmp _strnicmp
                     75: #endif
                     76: 
1.171     moko       77: #define stat __stat64
                     78: #define pa_fstat _fstat64
                     79: 
1.173     moko       80: int pa_stat(const char *pathname, struct stat *buffer);
                     81: int pa_open(const char *pathname, int flags, int mode=0);
                     82: FILE *pa_fopen(const char *pathname, const char *mode);
                     83: 
1.185     moko       84: #define pa_lseek _lseeki64
1.184     moko       85: 
1.171     moko       86: #else
                     87: 
                     88: #define pa_stat stat
                     89: #define pa_fstat fstat
                     90: 
1.173     moko       91: #define pa_open open
                     92: #define pa_fopen fopen
                     93: 
1.185     moko       94: #define pa_lseek lseek
1.184     moko       95: 
1.17      paf        96: #endif
1.59      paf        97: 
1.154     moko       98: /** 
                     99:        file related functions
                    100: */
1.136     misha     101: 
1.154     moko      102: #define FILE_BUFFER_SIZE       4096
1.95      paf       103: 
                    104: int pa_lock_shared_blocking(int fd);
                    105: int pa_lock_exclusive_blocking(int fd);
                    106: int pa_lock_exclusive_nonblocking(int fd);
                    107: int pa_unlock(int fd);
1.101     paf       108: 
                    109: void create_dir_for_file(const String& file_spec);
1.93      paf       110: 
1.154     moko      111: int pa_get_valid_file_options_count(HashStringValue& options);
                    112: 
1.163     moko      113: typedef void (*File_read_action)(struct stat& finfo, int f, const String& file_spec, void *context);
1.83      paf       114: 
                    115: /**
                    116:        shared-lock specified file, 
                    117:        do actions under lock.
                    118:        if fail_on_read_problem is true[default] throws an exception
                    119:        
                    120:        @returns true if read OK
                    121: */
1.93      paf       122: bool file_read_action_under_lock(const String& file_spec, 
                    123:                                const char* action_name, File_read_action action, void *context,
1.83      paf       124:                                bool as_text=false,
                    125:                                bool fail_on_read_problem=true);
1.128     misha     126: 
1.15      paf       127: /**
1.179     moko      128:        read specified text file
1.15      paf       129:        if fail_on_read_problem is true[default] throws an exception
1.83      paf       130: 
1.93      paf       131:        WARNING: charset is used for http header case conversion, it's not a charset of input file!
1.15      paf       132: */
1.179     moko      133: char *file_read_text(Request_charsets& charsets, const String& file_spec, bool fail_on_read_problem = true);
                    134: char *file_load_text(Request& r, const String& file_spec, bool fail_on_read_problem = true, HashStringValue* options = 0, bool transcode_result = true);
1.128     misha     135: 
1.93      paf       136: struct File_read_result {
                    137:        bool success;
                    138:        char* str; size_t length;
                    139:        HashStringValue* headers;
                    140: };
1.17      paf       141: 
                    142: /**
1.179     moko      143:        read specified binary file
1.29      paf       144:        if fail_on_read_problem is true[default] throws an exception
1.179     moko      145: */
                    146: 
1.184     moko      147: File_read_result file_read_binary(const String& file_spec, bool fail_on_read_problem = true, char* buf = 0, uint64_t offset = 0, size_t limit = 0);
1.83      paf       148: 
1.179     moko      149: /**
                    150:        load specified file
                    151:        if fail_on_read_problem is true[default] throws an exception
1.128     misha     152: */
1.93      paf       153: 
1.180     moko      154: File_read_result file_load(Request& r, const String& file_spec, bool as_text, HashStringValue* options=0, bool fail_on_read_problem=true, bool transcode_text_result=true);
1.83      paf       155: 
                    156: typedef void (*File_write_action)(int f, void *context);
1.29      paf       157: 
                    158: /**
1.71      paf       159:        lock specified file exclusively, 
                    160:        do actions under lock.
                    161:        throws an exception in case of problems
1.72      paf       162:        
                    163:        if block=false does non-blocking lock
                    164:        @returns true if locked OK, or false if non-blocking locking failed
1.71      paf       165: */
1.72      paf       166: bool file_write_action_under_lock(
1.71      paf       167:                                const String& file_spec, 
1.114     misha     168:                                const char* action_name,
                    169:                                File_write_action action,
                    170:                                void *context,
1.71      paf       171:                                bool as_text=false,
1.72      paf       172:                                bool do_append=false,
1.77      paf       173:                                bool do_block=true,
                    174:                                bool fail_on_lock_problem=true);
1.71      paf       175: 
                    176: /**
                    177:        write data to specified file, 
1.17      paf       178:        throws an exception in case of problems
                    179: */
1.70      paf       180: void file_write(
1.137     misha     181:                                Request_charsets& charsets,
1.25      paf       182:                                const String& file_spec,
1.137     misha     183:                                const char* data,
                    184:                                size_t size, 
1.58      paf       185:                                bool as_text,
1.137     misha     186:                                bool do_append=false,
                    187:                                Charset* asked_charset=0);
1.26      paf       188: 
                    189: /**
                    190:        delete specified file 
                    191:        throws an exception in case of problems
                    192: */
1.149     misha     193: bool file_delete(const String& file_spec, bool fail_on_problem=true, bool keep_empty_dirs=false);
1.47      parser    194: /**
                    195:        move specified file 
                    196:        throws an exception in case of problems
                    197: */
1.149     misha     198: void file_move(const String& old_spec, const String& new_spec, bool keep_empty_dirs=false);
1.27      paf       199: 
1.93      paf       200: bool entry_exists(const char* fname, struct stat *afinfo=0);
1.82      paf       201: bool entry_exists(const String& file_spec);
1.107     paf       202: bool file_exist(const String& file_spec);
                    203: bool dir_exists(const String& file_spec);
                    204: const String* file_exist(const String& path, const String& name);
1.36      paf       205: bool file_executable(const String& file_spec);
1.37      paf       206: 
1.168     moko      207: bool file_stat(const String& file_spec, uint64_t& rsize, time_t& ratime, time_t& rmtime, time_t& rctime, bool fail_on_read_problem=true);
1.181     moko      208: size_t check_file_size(uint64_t size, const String* file_spec);
1.15      paf       209: 
1.154     moko      210: size_t stdout_write(const void *buf, size_t size);
                    211: 
                    212: void check_safe_mode(struct stat finfo, const String& file_spec, const char* fname); 
                    213: 
1.186     moko      214: ssize_t file_block_read(const int f, void* buffer, const size_t size);
1.154     moko      215: 
                    216: /** 
                    217:        String related functions
                    218: */
                    219: 
                    220: /** under WIN32 "t" mode fixes DOS chars OK, 
                    221:        can't say that about other systems/ line break styles
1.15      paf       222: */
1.154     moko      223: void fix_line_breaks(char *str,        size_t& length /* < may change! used to speedup next actions */);
                    224: 
1.7       paf       225: char *getrow(char **row_ref,char delim='\n');
1.76      paf       226: char *lsplit(char *string, char delim);
1.7       paf       227: char *lsplit(char **string_ref,char delim);
1.8       paf       228: char *rsplit(char *string, char delim);
1.165     moko      229: 
1.151     moko      230: const char* format(double value, const char *fmt);
1.10      paf       231: 
1.142     misha     232: char* unescape_chars(const char* cp, int len, Charset* client_charset=0, bool js=false/*true==decode \uXXXX and don't convert '+' to space*/);
1.23      paf       233: 
1.148     misha     234: char *search_stop(char*& current, char cstop_at);
                    235: 
1.157     moko      236: inline int pa_strncasecmp(const char* str, const char* substr, size_t count=0) {
                    237:        return strncasecmp(str, substr, count ? count : strlen(substr));
                    238: }
                    239: 
1.23      paf       240: #ifdef WIN32
                    241: void back_slashes_to_slashes(char *s);
                    242: #endif
                    243: 
1.121     misha     244: size_t strpos(const char *str, const char *substr);
                    245: 
1.116     misha     246: int remove_crlf(char *start, char *end);
1.90      paf       247: 
1.165     moko      248: inline bool pa_isalpha(unsigned char c) { return (((c>='A') && (c<='Z')) || ((c>='a') && (c<='z'))); }
                    249: inline bool pa_isalnum(unsigned char c) { return (((c>='0') && (c<='9')) || pa_isalpha(c)); }
1.144     misha     250: 
1.190   ! moko      251: void pa_strncpy(char *dst, const char *src, size_t n);
1.182     moko      252: char *pa_strcat(const char *a, const char *b, const char *c = 0);
1.183     moko      253: const char *pa_filename(const char *path);
1.182     moko      254: 
1.165     moko      255: const char* capitalize(const char* s);
1.189     moko      256: char *str_lower(const char *s, size_t length);
                    257: char *str_upper(const char *s, size_t length);
                    258: inline char *str_lower(const char *s) { return str_lower(s, strlen(s)); }
                    259: inline char *str_upper(const char *s) { return str_upper(s, strlen(s)); }
1.144     misha     260: 
1.154     moko      261: const char* hex_string(unsigned char* bytes, size_t size, bool upcase);
1.155     moko      262: extern const char* hex_digits;
1.99      paf       263: 
1.158     moko      264: const char *pa_idna_encode(const char *in, Charset &source);
                    265: const char *pa_idna_decode(const char *in, Charset &source);
                    266: 
1.169     moko      267: unsigned long pa_crc32(const char *in, size_t in_size);
                    268: unsigned long pa_crc32(const String& file_spec);
1.93      paf       269: 
1.154     moko      270: /** 
                    271:        Mix functions
                    272: */
1.106     paf       273: 
1.164     moko      274: #define PA_DEFAULT(A,B) ((A) ? (A):(B) )
                    275: 
1.154     moko      276: Charset* detect_charset(const char* content_type);
                    277: 
1.93      paf       278: // globals
                    279: 
                    280: extern const String file_status_name;
1.1       paf       281: 
1.103     paf       282: // global defines for file options which are handled but not checked elsewhere, we check them
                    283: 
                    284: #define PA_COLUMN_SEPARATOR_NAME "separator"
                    285: #define PA_COLUMN_ENCLOSER_NAME "encloser"
1.113     misha     286: #define PA_CHARSET_NAME "charset"
1.150     moko      287: #define PA_RESPONSE_CHARSET_NAME "response-charset"
1.103     paf       288: 
1.115     misha     289: // globals defines for sql options
                    290: 
                    291: #define SQL_BIND_NAME "bind"
1.175     moko      292: #define PA_SQL_LIMIT_NAME "limit"
                    293: #define PA_SQL_OFFSET_NAME "offset"
1.115     misha     294: #define SQL_DEFAULT_NAME "default"
                    295: #define SQL_DISTINCT_NAME "distinct"
                    296: #define SQL_VALUE_TYPE_NAME "type"
                    297: 
1.175     moko      298: extern String sql_bind_name;
                    299: extern String sql_limit_name;
                    300: extern String sql_offset_name;
                    301: extern String sql_default_name;
                    302: extern String sql_distinct_name;
                    303: extern String sql_value_type_name;
                    304: 
1.115     misha     305: #ifndef DOXYGEN
                    306: enum Table2hash_distint { D_ILLEGAL, D_FIRST };
1.166     moko      307: enum Table2hash_value_type { C_HASH, C_STRING, C_TABLE, C_CODE };
1.115     misha     308: #endif
                    309: 
1.1       paf       310: #endif
1.115     misha     311: 

E-mail: