/***************************************************************** * test-common.c this sources file functions that is common for * test. * * Copyright (C) 2009 Andrey Y. Rjavskov(aka rjaan) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * *****************************************************************/ #include "test-common.h" /* should be free() for allocated memmory by programm name */ char *get_progname_from( char *path ) { int nbytes = 0; char *ptr = path + strlen ( path ); char *programm = NULL; while ( (--ptr >= path ) && (*ptr != '/') ) nbytes++; programm = calloc ( sizeof(char), nbytes + 1 ); if ( !programm ) return (char*)(""); strncpy ( programm, ptr + 1, nbytes ); return programm; } PngFileMark is_file_png ( const char *file_name ) { PngFileMark mark = FILE_IS_PNG; unsigned char header[8] = { 0 }; FILE *fp = fopen ( file_name, "rb" ); if (!fp) { mark = ERROR_OPEN_PNGFILE; goto oops; } if ( fread ( header, 8, 1, fp ) != 1 ) { fclose(fp); mark = ERROR_READ_PNGFILE; goto oops; } if ( png_sig_cmp ( header, 0, 8 ) ) { fclose(fp); mark = FILE_IS_NOT_PNG; goto oops; } oops: if ( mark != FILE_IS_NOT_PNG ) { if ( fp ) fclose(fp); } return mark; } unsigned char* get_escape_bytea_array ( const char *file_name, size_t *nbytes ) { int pngfd; ssize_t readn; struct stat st; unsigned char *bin_bytes; unsigned char *escaped; *nbytes = 0; if ( lstat(file_name, &st) == -1 ) { perror("stat"); return NULL; } if ( st.st_size >= ARRAY_LIMIT_FOR_BYTEA_TYPE ) { fprintf ( stderr,"limit array was exceed!(>= %d GBytes)!\n", (int)(st.st_size/(1024*1024*1024)) ); return NULL; } bin_bytes = malloc ( st.st_size ); if ( !bin_bytes ) { fprintf (stderr,"memory of out: have to need %d bytes!\n", (int)st.st_size ); return NULL; } pngfd = open ( file_name, O_RDONLY ); if( pngfd == -1 ) { char *name = get_progname_from ( (char*)file_name ); fprintf ( stderr,"failed to read file \'%s\'\n", name ); free( name ); free( bin_bytes ); return NULL; } readn = read ( pngfd, bin_bytes, st.st_size ); if ( readn == -1 ) { char *name = get_progname_from ( (char*)file_name ); fprintf ( stderr, "failed to read file \'%s\'\n", name ); free( name ); free( bin_bytes ); return NULL; } escaped = PQescapeBytea((const unsigned char*)bin_bytes, (size_t)readn, nbytes ); free( bin_bytes ); return escaped; } PGconn* upg_connect_db ( dbhost, dbport, dbname, dbuser, dbupwd ) const char *dbhost; const char *dbport; const char *dbname; const char *dbuser; const char *dbupwd; { PGconn* db; db = PQsetdbLogin( dbhost,dbport,NULL,NULL,dbname,dbuser,dbupwd); if ( PQstatus(db) != CONNECTION_OK ) { char *msg = PQerrorMessage(db); fprintf(stderr,"PQsetdbLogin(): %s", (char*)msg ); return NULL; } return db; } void upg_disconnect_db ( PGconn* db ) { PQfinish(db); } int upg_sqlexec ( PGconn* db,const char* format, ... ) { va_list ap; char *sql_stmt = calloc( sizeof(char), BUFFER_MAXSIZE ); PGresult *resPG = NULL; ExecStatusType statusPGexec; if( !sql_stmt ) { fprintf ( stderr,"%s(): malloc(): memory of out!\n", __FUNCTION__ ); fflush ( stderr); return -EXIT_FAILURE; } va_start(ap, format); (void)vsnprintf ( sql_stmt, BUFFER_MAXSIZE, format, ap ); va_end(ap); PQclear( resPG ); resPG = PQexec( db, sql_stmt ); if( ( statusPGexec = PQresultStatus( resPG ) ) != PGRES_COMMAND_OK && statusPGexec != PGRES_TUPLES_OK ) { char *msg = PQresultErrorMessage ( resPG ); fprintf ( stderr, "PQexecParams(): %s\n", msg ); fflush ( stderr); free ( sql_stmt ); return -EXIT_FAILURE; } while ( PQflush ( db ) ) ; free ( sql_stmt ); return EXIT_SUCCESS; } int bytea_entry_add ( PGconn* db, const char *ptrbytea, const char* ptrdsc, ... ) { va_list ap; char *dsc = calloc( sizeof(char), BUFFER_MAXSIZE ); PGresult *resPG = NULL; ExecStatusType statusPGexec; const char *q[2] = { 0 }; if( !dsc ) { fprintf ( stderr,"%s(): malloc(): memory of out!\n", __FUNCTION__ ); fflush ( stderr); return -EXIT_FAILURE; } va_start(ap, ptrdsc ); (void)vsnprintf ( dsc, BUFFER_MAXSIZE, ptrdsc, ap); va_end(ap); PQclear( resPG ); q[0] = dsc; q[1] = ptrbytea; resPG = PQexecParams(db, "INSERT INTO images(dsc,img) VALUES($1::text,convert_to($2,'SQL_ASCII')::bytea);", 2, NULL, q, NULL, NULL, 0 ); if( ( statusPGexec = PQresultStatus( resPG ) ) != PGRES_COMMAND_OK && statusPGexec != PGRES_TUPLES_OK ) { char *msg = PQresultErrorMessage ( resPG ); fprintf ( stderr, "PQexecParams(): %s\n", msg ); fflush ( stderr); free(dsc); return -EXIT_FAILURE; } while ( PQflush( db ) ) ; free(dsc); return EXIT_SUCCESS; } static struct timeval begin_tmval; void test_begin( char *tst_path ) { char *progname = get_progname_from( tst_path ); fprintf ( stderr, "begin %s", progname ); fflush ( stderr ); gettimeofday ( &begin_tmval, NULL ); } void test_go ( int count, int bp_interval ) { if ( (count % bp_interval ) ) { return; } fprintf( stdout,"." ); fflush ( stdout ); } void test_done( void ) { unsigned long msec, min ; struct timeval tmval; gettimeofday ( &tmval, NULL ); msec = ( ( ( tmval.tv_sec - begin_tmval.tv_sec ) * 1000000 ) + tmval.tv_usec) - begin_tmval.tv_usec ; min = ( tmval.tv_sec - begin_tmval.tv_sec ) / 60; fprintf ( stderr, "done in %03ld min %ld.%03ld sec\n", min , tmval.tv_sec - begin_tmval.tv_sec - (min*60), msec / 1000 ); fflush ( stderr ); } /*eof*/