
/* OpenWebSpider
 *
 *  Authors:     Stefano Alimonti AND Stefano Fantin
 *  Version:     0.8
 *  E-Mails:     shen139 [at] openwebspider (dot) org AND stefanofantinguz@yahoo.it
 *
 *
 * This file is part of OpenWebSpider
 *
 * 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
 *
 */

#ifndef __MyMACRO
#define __MyMACRO

/*MACRO*/
#define MIN(a,b)        (a<b)?a:b

#define FREE(x)         if(x)free(x);

#define CREATE_TMP_TABLE(tab)   " CREATE TABLE `%s` (                                               " \
                                "     `id` int(11) NOT NULL auto_increment,                         " \
                                "     `host_id` int(10) unsigned NOT NULL,                          " \
                                "     `hostname` varchar(100) NOT NULL,                             " \
                                "     `page` varchar(255) NOT NULL,                                 " \
                                "     `title` varchar(255) NOT NULL,                                " \
                                "     `text` longtext NOT NULL,                                     " \
                                "     `cache` longblob,                                             " \
                                "     `version` int(11) unsigned NOT NULL default '0',              " \
                                "     `level` int(11) unsigned NOT NULL default '0',                " \
                                "     `rank` int(11) unsigned NOT NULL default '0',                 " \
                                "     `date` varchar(10) NOT NULL,                                  " \
                                "     `time` varchar(10) NOT NULL,                                  " \
                                "     PRIMARY KEY  (`id`)                                      " \
                                "   ) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ",tab


#define ERROR_LOG(msg)	{							\
                        FILE* file;					\
                        time_t long_time;			\
                        struct tm *newtime;			\
                            time( &long_time );		\
                            newtime=localtime(&long_time);	\
                            if((file = fopen("error.log","a"))!=NULL)     \
                            { \
                                fprintf(file,"%i\\%i\\%i %i:%i:%i - %s\n",newtime->tm_mday ,newtime->tm_mon +1, newtime->tm_year +1900,newtime->tm_hour ,newtime->tm_min ,newtime->tm_sec,msg);	\
                                fclose(file);	\
                            }	\
                         }

#define OWS_LOG(msg)	{							\
                        FILE* file;					\
                        time_t long_time;			\
                        struct tm *newtime;			\
                            time( &long_time );		\
                            newtime=localtime(&long_time);	\
                            if((file = fopen("ows_server.log","a"))!=NULL)     \
                            { \
                                fprintf(file,"%i\\%i\\%i %i:%i:%i - %s\n",newtime->tm_mday ,newtime->tm_mon +1, newtime->tm_year +1900,newtime->tm_hour ,newtime->tm_min ,newtime->tm_sec,msg);	\
                                fclose(file);	\
                            }	\
                         }

#define DEBUG_LOG(msg)	{							\
                        FILE* file;					\
                        time_t long_time;			\
                        struct tm *newtime;			\
                            time( &long_time );		\
                            newtime=localtime(&long_time);	\
                            if((file = fopen("debug.log","a"))!=NULL)     \
                            { \
                                fprintf(file,"%i\\%i\\%i %i:%i:%i - %s\n",newtime->tm_mday ,newtime->tm_mon +1, newtime->tm_year +1900,newtime->tm_hour ,newtime->tm_min ,newtime->tm_sec,msg);	\
                                fclose(file);	\
                            }	\
                         }

#define CRAWLER_PROFILE_LOG(msg)	{							\
                        FILE* file;					\
                        time_t long_time;			\
                        struct tm *newtime;			\
                            time( &long_time );		\
                            newtime=localtime(&long_time);	\
                            if((file = fopen("crawler_profile.log","a"))!=NULL)     \
                            { \
                                fprintf(file,"%i\\%i\\%i %i:%i:%i - %s\n",newtime->tm_mday ,newtime->tm_mon +1, newtime->tm_year +1900,newtime->tm_hour ,newtime->tm_min ,newtime->tm_sec,msg);	\
                                fclose(file);	\
                            }	\
                         }

#define CRAWLER_PROFILE_JSONL(msg)	{							\
                        FILE* file;					\
                            if((file = fopen("crawler_profile_events.jsonl","a"))!=NULL)     \
                            { \
                                fprintf(file,"%s\n",msg);	\
                                fclose(file);	\
                            }	\
                         }

static void OwsJsonEscapeCopy(const char *src, char *dst, size_t dstSize)
{
    size_t i, j = 0;
    if (!dst || dstSize == 0)
        return;
    dst[0] = '\0';
    if (!src)
        return;
    for (i = 0; src[i] && j + 1 < dstSize; i++)
    {
        unsigned char c = (unsigned char)src[i];
        if ((c == '"' || c == '\\') && j + 2 < dstSize)
        {
            dst[j++] = '\\';
            dst[j++] = (char)c;
        }
        else if (c == '\n' && j + 2 < dstSize)
        {
            dst[j++] = '\\';
            dst[j++] = 'n';
        }
        else if (c == '\r' && j + 2 < dstSize)
        {
            dst[j++] = '\\';
            dst[j++] = 'r';
        }
        else if (c == '\t' && j + 2 < dstSize)
        {
            dst[j++] = '\\';
            dst[j++] = 't';
        }
        else if (c >= 32)
        {
            dst[j++] = (char)c;
        }
    }
    dst[j] = '\0';
}

#endif


/* EOF*/
