File:  [ELWIX - Embedded LightWeight unIX -] / libaitio / inc / aitio.h
Revision 1.6: download - view: text, annotated - select for diffs - revision graph
Thu Feb 10 22:01:34 2011 UTC (13 years, 5 months ago) by misho
Branches: MAIN
CVS tags: io1_6, io1_5, IO1_5, IO1_4, HEAD
final version

    1: /*************************************************************************
    2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
    3: *  by Michael Pounov <misho@openbsd-bg.org>
    4: *
    5: * $Author: misho $
    6: * $Id: aitio.h,v 1.6 2011/02/10 22:01:34 misho Exp $
    7: *
    8: *************************************************************************/
    9: #ifndef __AITIO_H
   10: #define __AITIO_H
   11: 
   12: 
   13: typedef struct _tagURLItem {
   14: 	int	vallen;
   15: 	char	*value;
   16: } url_Item_t;
   17: 
   18: struct tagIOURL {
   19: 	unsigned char	url_line[BUFSIZ];
   20: 
   21: 	url_Item_t	url_tech;
   22: 	url_Item_t	url_user;
   23: 	url_Item_t	url_pass;
   24: 	url_Item_t	url_host;
   25: 	url_Item_t	url_port;
   26: 	url_Item_t	url_path;
   27: 	url_Item_t	url_args;
   28: 
   29: 	char		*url_reserved;
   30: };
   31: 
   32: struct tagReqXML {
   33: 	unsigned char	xml_line[BUFSIZ];
   34: 
   35: 	url_Item_t	xml_namespace;
   36: 	union {
   37: 		url_Item_t	container;
   38: 		url_Item_t	path;
   39: 	}		xml_node;
   40: 	url_Item_t	xml_data;
   41: 	url_Item_t	xml_attribute;
   42: 	url_Item_t	xml_value;
   43: };
   44: 
   45: 
   46: // io_GetErrno() Get error code of last operation
   47: inline int io_GetErrno();
   48: // io_GetError() Get error text of last operation
   49: inline const char *io_GetError();
   50: 
   51: 
   52: /*
   53:  * ioPromptRead() Read data from input h[0] with prompt to output h[1]
   54:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
   55:  * @csPrompt = Prompt before input, may be NULL
   56:  * @psData = Readed data
   57:  * @dataLen = Length of data
   58:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
   59: */
   60: int ioPromptRead(int *h, const char *csPrompt, char * __restrict psData, int dataLen);
   61: /*
   62:  * ioPromptPassword() Read password from input h[0] with prompt to output h[1]
   63:  * @h = file handles h[0] = input, h[1] = output, if NULL use stdin, stdout
   64:  * @csPrompt = Prompt before input, may be NULL
   65:  * @psPass = Readed password
   66:  * @passLen = Length of password
   67:  * @confirm = Confirm password, 0 - get password, !=0 Ask for confirmation
   68:  * return: 0 EOF; -1 error:: can`t read; >0 count of readed chars
   69: */
   70: int ioPromptPassword(int *h, const char *csPrompt, char * __restrict psPass, int passLen, int confirm);
   71: 
   72: /*
   73:  * ioRegexVerify() Function for verify data match in regex expression
   74:  * @csRegex = Regulare expression pattern
   75:  * @csData = Data for check and verify
   76:  * @startPos = Return start positions
   77:  * @endPos = Return end positions
   78:  * return: NULL not match or error; !=NULL begin of matched data
   79: */
   80: const char *ioRegexVerify(const char *csRegex, const char *csData, int *startPos, int *endPos);
   81: /*
   82:  * ioRegexGet() Function for get data match in regex expression
   83:  * @csRegex = Regulare expression pattern
   84:  * @csData = Data from get
   85:  * @psString = Returned string if match
   86:  * @strLen = Length of string
   87:  * return: 0 not match; >0 count of returned chars
   88: */
   89: int ioRegexGet(const char *csRegex, const char *csData, char * __restrict psString, int strLen);
   90: /*
   91:  * ioRegexReplace() Function for replace data match in regex expression with newdata
   92:  * @csRegex = Regulare expression pattern
   93:  * @csData = Source data
   94:  * @csNew = Data for replace
   95:  * return: NULL not match or error; !=NULL allocated new string, must be free after use!
   96: */
   97: char *ioRegexReplace(const char *csRegex, const char *csData, const char *csNew);
   98: 
   99: /*
  100:  * ioVarAst() Function for evaluate string like asterisk variable "{text[:[-]#[:#]]}"
  101:  * @csString = Input string
  102:  * return: NULL error, !=NULL Allocated new string evaluated from input string, must be free()
  103: */
  104: char *ioVarAst(const char *csString);
  105: 
  106: /*
  107:  * io_Path2File() Parse and make path/filename pair
  108:  * @csArgs = Input argument line
  109:  * @psPath = Output Path, if ==NULL path not returned
  110:  * @pathLen = Size of path array
  111:  * @psFile = Output File
  112:  * @fileLen = Size of file array
  113:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
  114: */
  115: inline int io_Path2File(const char * __restrict csArgs, char * __restrict psPath, int pathLen, 
  116: 		char * __restrict psFile, int fileLen);
  117: /*
  118:  * io_MakeAV() Parse and make attribute/value pair
  119:  * @csArgs = Input argument line
  120:  * @csDelim = Delimiter for separate
  121:  * @psAttr = Output Attribute
  122:  * @attrLen = Size of attribute array
  123:  * @psValue = Output Value, if ==NULL this element not present value or not wanted for return
  124:  * @valLen = Size of value array
  125:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
  126: */
  127: inline int io_MakeAV(const char * __restrict csArgs, const char *csDelim, 
  128: 		char * __restrict psAttr, int attrLen, char * __restrict psValue, int valLen);
  129: /*
  130:  * io_SizeArray() Parse and calculate size of array
  131:  * @csArgs = Input arguments line
  132:  * @csDelim = Delimiter(s) for separate
  133:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of items
  134: */
  135: inline int io_SizeArray(const char *csArgs, const char *csDelim);
  136: /*
  137:  * io_MakeArray() Parse and make array of arguments values ... 
  138:  *	(input string will be modified! and output array must be free)
  139:  * @psArgs = Input arguments line, after execute string is modified!!!
  140:  * @csDelim = Delimiter(s) for separate
  141:  * @args = Output array of arguments ... (must be free() after procced function!)
  142:  * @nargs = Maximum requested count of arguments from input string psArgs
  143:  * return: 0 error format; -1 error:: can`t read; >0 ok, number of readed items
  144: */
  145: inline int io_MakeArray(char * __restrict psArgs, const char *csDelim, 
  146: 		char *** __restrict args, int nargs);
  147: 
  148: /*
  149:  * io_UnquotStr() Remove quots from input text string 
  150:  * @psLine = Text string
  151:  * return: 0 nothing to do; 1 successful unquoted string
  152: */
  153: inline int io_UnquotStr(unsigned char * __restrict psLine);
  154: /*
  155:  * io_LTrimStr() Remove left whitespaces from text string
  156:  * @psLine = Text string
  157:  * return: 0 nothing to do; !=0 Removed bytes
  158: */
  159: inline int io_LTrimStr(unsigned char * __restrict psLine);
  160: /*
  161:  * io_RTrimStr() Remove right whitespaces from text string
  162:  * @psLine = Text string
  163:  * return: 0 nothing to do; !=0 Removed bytes
  164: */
  165: inline int io_RTrimStr(unsigned char * __restrict psLine);
  166: /*
  167:  * io_TrimStr() Remove left and right whitespaces from text string
  168:  * @psLine = Text string
  169:  * return: 0 nothing to do; !=0 Removed bytes
  170: */
  171: inline int io_TrimStr(unsigned char * __restrict psLine);
  172: /*
  173:  * io_Ch2Hex() Convert from Char string to Hex string
  174:  * @psLine = Text string
  175:  * @lineLen = Length of Text string
  176:  * return: NULL nothing to do or error; !=0 Allocated new converted data without term\0 (must be free)
  177: */
  178: inline unsigned char *io_Ch2Hex(unsigned char *psLine, int lineLen);
  179: /*
  180:  * io_Hex2Ch() Convert from Hex string to Char string
  181:  * @psLine = Text string
  182:  * @lineLen = Length of Text string
  183:  * return: NULL nothing to do or error; !=0 Allocated new converted string(must be free)
  184: */
  185: inline char *io_Hex2Ch(unsigned char *psLine, int lineLen);
  186: 
  187: /*
  188:  * ioURLGet() Parse and get data from input URL
  189:  * @csURL = Input URL line
  190:  * @url = Output parsed URL
  191:  * return: 0 error format not find tech:// and return URL like path; 
  192:  		-1 error:: can`t read; >0 ok, up bits for known elements
  193: */
  194: int ioURLGet(const char *csURL, struct tagIOURL *url);
  195: /*
  196:  * ioURLGetValue() Get value from parsed URL
  197:  * @url = Input parsed URL
  198:  * @csAttr = Attribute for search
  199:  * @psValue = Return value of attribute, if ==NULL only check for existence of attribute
  200:  * @valLen = Size of psValue array
  201:  * return: 0 error attribute not find; -1 error:: can`t read; >0 ok, find at position
  202: */
  203: int ioURLGetValue(struct tagIOURL *url, const char *csAttr, char * __restrict psValue, int valLen);
  204: /*
  205:  * ioURLGetFile() Get file from parsed URL
  206:  * @url = Input parsed URL
  207:  * @psValue = Return filename, if not specified file in url path, replace with /
  208:  * @valLen = Size of psValue array
  209:  * return: -1 error:: can`t read; 0 ok
  210: */
  211: int ioURLGetFile(struct tagIOURL *url, char * __restrict psValue, int valLen);
  212: 
  213: 
  214: /*
  215:  * ioXMLGet() Parse and get data from input XML request string [ns:]container[|attribute[=value]][?data]
  216:  * @csXML = Input XML request line
  217:  * @xml = Output parsed XML request
  218:  * return: 0 error format incorrect, -1 error:: can`t read; >0 ok readed elements bits
  219: */
  220: int ioXMLGet(const char *csXML, struct tagReqXML *xml);
  221: 
  222: 
  223: /*
  224:  * ioMkDir() Function for racursive directory creation and validation
  225:  * @csDir = Full directory path
  226:  * @mode = Mode for directory creation if missing dir
  227:  * return: -1 error, 0 directory path exist, >0 created missing dirs
  228: */
  229: int ioMkDir(const char *csDir, int mode);
  230: 
  231: /*
  232:  * ioWatchDirLoop() Function for watching changes in directory and fire callback
  233:  * @csDir = Full directory path
  234:  * @callback = Callback if raise event! nOp -1 delete, 0 change/move, 1 create
  235:  * return: -1 error, !=-1 ok, number of total signaled events
  236: */
  237: int ioWatchDirLoop(const char *csDir, int (*callback)(const char *csName, int nOp));
  238: 
  239: 
  240: /*
  241:  * io_rread() Raw VFS read function
  242:  * @fd = File handle
  243:  * @buf = Read buffer
  244:  * @nbytes = Read buffer size
  245:  * @offset = Read from position, if -1 read nbytes from current position
  246:  * @update = Update file handle position !0
  247:  * return: -1 error or !=-1 readed bytes
  248:  */
  249: inline int io_rread(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update);
  250: /*
  251:  * io_rwrite() Raw VFS write function
  252:  * @fd = File handle
  253:  * @buf = Write buffer
  254:  * @nbytes = Write bytes from buffer
  255:  * @offset = Write at position, if -1 write nbytes from current position
  256:  * @update = Update file handle position !0
  257:  * return: -1 error or !=-1 writed bytes
  258:  */
  259: inline int io_rwrite(int fd, void * __restrict buf, size_t nbytes, off_t offset, int update);
  260: 
  261: /* Disk I/O helper macros */
  262: #define io_read(f, b, n) io_rread(f, b, n, -1, 1)
  263: #define io_write(f, b, n) io_rwrite(f, b, n, -1, 1)
  264: 
  265: 
  266: /* Debug helper macros */
  267: extern int io_Debug;
  268: 
  269: #define io_initDebug(x)		io_Debug = (x);
  270: #define io_addDebug		io_Debug++
  271: #define ioDEBUG(x, fmt, ...)	do { \
  272: 					assert((fmt)); \
  273: 					char str[STRSIZ] = { 0 }; \
  274: 					snprintf(str, STRSIZ, (fmt), ##__VA_ARGS__); \
  275: 					if ((x) <= io_Debug) \
  276: 						syslog(LOG_DEBUG, "ioDebug(%d):%s(%d): %s\n", \
  277: 								(x), __func__, __LINE__, str); \
  278: 				} while (0)
  279: 
  280: #define ioERROR(x, fmt, ...)	do { \
  281: 					assert((fmt)); \
  282: 					char str[STRSIZ] = { 0 }; \
  283: 					snprintf(str, STRSIZ, (fmt), ##__VA_ARGS__); \
  284: 					syslog(LOG_ERR, "ioError():%s(%d): #%d - %s\n", \
  285: 							 __func__, __LINE__, (x), str); \
  286: 				} while (0)
  287: #define io_sysERROR(x)		do { \
  288: 					if (x > 0 || errno) \
  289: 						syslog(LOG_ERR, "ioError(sys):%s(%d): #%d - %s\n", \
  290: 								__func__, __LINE__, x > 0 ? x : errno, \
  291: 								strerror(x > 0 ? x : errno)); \
  292: 				} while (0)
  293: #define io_aitERROR(ait)	do { \
  294: 					if (ait##_GetErrno()) \
  295: 						syslog(LOG_ERR, "ioError(ait):%s(%d): #%d - %s\n", \
  296: 								__func__, __LINE__, ait##_GetErrno(), \
  297: 								ait##_GetError()); \
  298: 				} while (0)
  299: 
  300: 
  301: #endif

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>