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

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

E-mail: