cGit-UI for Git Repositories

cGit-UI – is a web interface for Git Repositories. cGit CGI script is writen in C and therefore it's fast enough

12 Commits   0 Branches   1 Tag
     5         kx 
     5         kx #ifndef    __STRBUF_H
     5         kx #define    __STRBUF_H
     5         kx 
     5         kx #ifdef __cplusplus
     5         kx extern "C" {
     5         kx #endif
     5         kx 
     5         kx #define STRBUF_MAXLINE (2*PATH_MAX)
     5         kx 
     5         kx typedef void (*strbuf_error)( const char *fmt, ... );
     5         kx 
     5         kx struct strbuf
     5         kx {
     5         kx   size_t  alloc;
     5         kx   size_t  len;
     5         kx   strbuf_error fatal;
     5         kx   char   *buf;
     5         kx };
     5         kx 
     5         kx /************************************************************************
     5         kx   Таблица размещается на стороне клиента, а указатель присваивается
     5         kx   переменной strbuf_envtab, который используется функциями чтения файлов
     5         kx   для подстановки значений вместо переменных:
     5         kx 
     5         kx     ${root-title}, ${root-desc}, ${repo}, ${repo-desc}, ...
     5         kx 
     5         kx   Освобождать выделенную память нет необходимости, так как таблица
     5         kx   используется в CGI-скрипте, который срабатывает однократно.
     5         kx 
     5         kx   При необходимости повторного создания таблицы переменных,
     5         kx   надо освободить память с помощью envtab_release( &strbuf_envtab )
     5         kx   которая обнулит указатель strbuf_envtab, являющийся адресом первой
     5         kx   записи в таблице.
     5         kx  */
     5         kx typedef void (*envtab_error)( const char *fmt, ... );
     5         kx 
     5         kx struct symbol
     5         kx {
     5         kx   char *name;
     5         kx   char *value;
     5         kx 
     5         kx   struct symbol *next;
     5         kx };
     5         kx 
     5         kx extern struct symbol *strbuf_envtab;
     5         kx 
     5         kx extern struct symbol *envtab_install( struct symbol **sym, const char *n, const char *v, envtab_error fatal );
     5         kx extern struct symbol *envtab_lookup( struct symbol **sym, const char *n );
     5         kx extern void envtab_release( struct symbol **sym );
     5         kx /*
     5         kx  ************************************************************************/
     5         kx 
     5         kx 
     5         kx extern char strbuf_slopbuf[];
     5         kx extern void strbuf_fatal( const char *fmt, ... );
     5         kx 
     5         kx #define STRBUF_INIT  { .alloc = 0, .len = 0, .fatal = strbuf_fatal, .buf = strbuf_slopbuf }
     5         kx 
     5         kx extern void  strbuf_grow( struct strbuf *sb, size_t amount );
     5         kx extern void  strbuf_init( struct strbuf *sb, strbuf_error fatal, size_t alloc );
     5         kx extern void  strbuf_release( struct strbuf *sb );
     5         kx extern char *strbuf_detach( struct strbuf *sb, size_t *sz );
     5         kx extern void  strbuf_attach( struct strbuf *sb, void *str, size_t len, size_t mem );
     5         kx 
     5         kx static inline size_t strbuf_avail( const struct strbuf *sb )
     5         kx {
     5         kx   return sb->alloc ? sb->alloc - sb->len - 1 : 0;
     5         kx }
     5         kx 
     5         kx static inline void strbuf_setlen(struct strbuf *sb, size_t len)
     5         kx {
     5         kx   if( len > (sb->alloc ? sb->alloc - 1 : 0) )
     5         kx     sb->fatal( "BUG: strbuf_setlen() beyond buffer" );
     5         kx   sb->len = len;
     5         kx   if (sb->buf != strbuf_slopbuf)
     5         kx     sb->buf[len] = '\0';
     5         kx   else
     5         kx     if( !strbuf_slopbuf[0] ) sb->fatal( "BUG: strbuf_slopbuf[0] = '\0'" );
     5         kx }
     5         kx 
     5         kx #define strbuf_reset(sb)  strbuf_setlen(sb, 0)
     5         kx 
     5         kx 
     5         kx /***************************************************************************
     5         kx   BUILD_ASSERT_OR_ZERO - assert a build-time dependency, as an expression.
     5         kx   @cond: the compile-time condition which must be true.
     5         kx 
     5         kx   Your compile will fail if the condition isn't true, or can't be evaluated
     5         kx   by the compiler.  This can be used in an expression: its value is "0".
     5         kx 
     5         kx   Example:
     5         kx     #define foo_to_char(foo) \
     5         kx       ((char *)(foo)         \
     5         kx        + BUILD_ASSERT_OR_ZERO(offsetof(struct foo, string) == 0))
     5         kx  ***************************************************************************/
     5         kx #define BUILD_ASSERT_OR_ZERO(cond) \
     5         kx   (sizeof(char [1 - 2*!(cond)]) - 1)
     5         kx 
     5         kx #define SWAP(a, b) \
     5         kx   do {                                               \
     5         kx     void *_swap_a_ptr = &(a);                        \
     5         kx     void *_swap_b_ptr = &(b);                        \
     5         kx     unsigned char _swap_buffer[sizeof(a)];           \
     5         kx     memcpy(_swap_buffer, _swap_a_ptr, sizeof(a));    \
     5         kx     memcpy(_swap_a_ptr, _swap_b_ptr, sizeof(a) +     \
     5         kx       BUILD_ASSERT_OR_ZERO(sizeof(a) == sizeof(b))); \
     5         kx     memcpy(_swap_b_ptr, _swap_buffer, sizeof(a));    \
     5         kx   } while (0)
     5         kx 
     5         kx static inline void strbuf_swap( struct strbuf *a, struct strbuf *b )
     5         kx {
     5         kx   SWAP(*a, *b);
     5         kx }
     5         kx 
     5         kx 
     5         kx extern void strbuf_ltrim( struct strbuf *sb );
     5         kx extern void strbuf_rtrim( struct strbuf *sb );
     5         kx extern void strbuf_trim( struct strbuf *sb );
     5         kx 
     5         kx #ifndef is_dir_sep
     5         kx static inline int is_dir_sep( int c )
     5         kx {
     5         kx   return c == '/';
     5         kx }
     5         kx #endif
     5         kx static inline int is_absolute_path( const char *path )
     5         kx {
     5         kx   return is_dir_sep( path[0] );
     5         kx }
     5         kx 
     5         kx extern void strbuf_trim_trailing_dir_sep( struct strbuf *sb );
     5         kx extern void strbuf_trim_trailing_newline( struct strbuf *sb );
     5         kx 
     5         kx extern int strbuf_cmp( const struct strbuf *a, const struct strbuf *b );
     5         kx 
     5         kx 
     5         kx /* Adding data to the buffer */
     5         kx 
     5         kx extern void strbuf_add( struct strbuf *sb, const void *data, size_t len );
     5         kx extern void strbuf_addbuf( struct strbuf *sb, const struct strbuf *sb2 );
     5         kx extern void strbuf_addbuf_percentquote( struct strbuf *dst, const struct strbuf *src );
     5         kx 
     5         kx 
     5         kx /* Add a single ASCII character to the buffer. */
     5         kx static inline void strbuf_addch( struct strbuf *sb, int c )
     5         kx {
     5         kx   if( !strbuf_avail(sb) )
     5         kx     strbuf_grow(sb, 1);
     5         kx   sb->buf[sb->len++] = c;
     5         kx   sb->buf[sb->len] = '\0';
     5         kx }
     5         kx 
     5         kx /* Add n ASCII characters to the buffer. */
     5         kx extern void strbuf_addchars( struct strbuf *sb, int c, size_t n );
     5         kx 
     5         kx /* Add a NUL-terminated string to the buffer. */
     5         kx static inline void strbuf_addstr( struct strbuf *sb, const char *s )
     5         kx {
     5         kx   strbuf_add( sb, (const void *)s, strlen(s) );
     5         kx }
     5         kx 
     5         kx extern void strbuf_vaddf( struct strbuf *sb, const char *fmt, va_list ap );
     5         kx extern void strbuf_addf( struct strbuf *sb, const char *fmt, ... );
     5         kx 
     5         kx extern size_t strbuf_fread( struct strbuf *sb, FILE *fp );
     5         kx extern size_t strbuf_env_fread( struct strbuf *sb, FILE *fp );
     5         kx extern size_t strbuf_fwrite( struct strbuf *sb, FILE *fp );
     5         kx 
     5         kx extern ssize_t strbuf_read( struct strbuf *sb, int fd, size_t hint );
     5         kx extern ssize_t strbuf_write( struct strbuf *sb, int fd );
     5         kx 
     5         kx 
     5         kx /* XML/HTML quoted: */
     5         kx extern void strbuf_addstr_xml_quoted( struct strbuf *sb, const char *s );
     5         kx extern void strbuf_addstr_html_quoted( struct strbuf *sb, const char *s );
     5         kx 
     5         kx /* urlencode: */
     5         kx typedef int (*char_predicate)(char ch);
     5         kx 
     5         kx extern int is_rfc3986_unreserved( char ch );
     5         kx extern int is_rfc3986_reserved_or_unreserved( char ch );
     5         kx 
     5         kx extern void strbuf_addstr_urlencode( struct strbuf *sb, const char *name, char_predicate allow_unencoded_fn );
     5         kx 
     5         kx 
     5         kx extern void strbuf_humanise_bytes( struct strbuf *sb, off_t bytes );
     5         kx extern void strbuf_humanise_rate( struct strbuf *sb, off_t bytes );
     5         kx 
     5         kx 
     5         kx extern int is_directory( const char *path );
     5         kx 
     5         kx extern void strbuf_selfdir( struct strbuf *sb );
     5         kx extern void strbuf_relpath( struct strbuf *sb, const char *path );
     5         kx extern void strbuf_abspath( struct strbuf *sb, const char *path );
     5         kx 
     5         kx 
     5         kx #ifdef __cplusplus
     5         kx }
     5         kx #endif
     5         kx 
     5         kx #endif  /* __STRBUF_H */