<commit_before>/*=============================================================================

Copyright 2008 <NAME> (<EMAIL>)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
=============================================================================*/

#define APPEND_TO_EOL_BUF(_buf,_size) { \
	DEBUG1(mp,3,"Appending %lu bytes to eol buffer",(unsigned long)(_size)); \
	mp->eol_buffer.buf=allocate(mp->eol_buffer.buf \
		,mp->eol_buffer.len+_size); \
	memcpy(&(mp->eol_buffer.buf[mp->eol_buffer.len]),_buf,_size); \
	mp->eol_buffer.len += _size; \
	}

#define WRITE_EOL_BUF(_flags,_t) { \
	write_level2(mp,mp->eol_buffer.buf,mp->eol_buffer.len,_flags,_t); \
	FREE_EOL_BUF(); \
	}

#define FREE_EOL_BUF() \
	mp->eol_buffer.buf=allocate(mp->eol_buffer.buf,mp->eol_buffer.len=0);

/*----------------------------------------------*/

static TIMESTAMP last_write_time=0;

/*----------------------------------------------*/
/* Called when the manager is closed. Even if we don't have an EOL, we
* must write the buffer */

LIB_INTERNAL void write_end(LOGMANAGER mp)
{
WRITE_EOL_BUF(0,last_write_time);
}

/*----------------------------------------------*/
/* Buffer output so that the files are cut on line boundaries ('\n' char) */

void logmanager_write(LOGMANAGER mp, const char *buf, apr_off_t size
	,unsigned int flags, TIMESTAMP t)
{
int i;

CHECK_MP(mp);
NORMALIZE_TIMESTAMP(t);

DEBUG1(mp,2,"Starting logmanager_write (size=%lu)",(unsigned long)size);
INCR_STAT_COUNT(mp,write);

last_write_time=t;

if ((!buf) || (!size)) return;

if (mp->flags & LMGR_IGNORE_EOL)
	{
	write_level2(mp,buf,size,flags,t);
	return;
	}

/* 1. If eol_buffer contains some data from a previous write, search a '\n'
from the beginning. If found, output the buffer and input data up to \n,
truncate data. If not found, append data to the buffer */

if (mp->eol_buffer.buf)
	{
	for (i=0;i<size;i++)
		{
		if (buf[i]=='\n')
			{
			/* Append to eol buf first, so that the whole line is sent to write_level2 in only one call */
			/* So, rotation can occur. If we did it with 2 calls, we should disable rotation for the */
			/* second call, and file size could be exceeded */
			APPEND_TO_EOL_BUF(buf,i+1);
			buf += (i+1);
			size -= (i+1);
			WRITE_EOL_BUF(flags,t);
			break;
			}
		}

	if (mp->eol_buffer.buf) /* if not found, append to eol_buffer.buf */
		{
		APPEND_TO_EOL_BUF(buf,size);
		return;
		}
	}

/* 2. Search last \n. If found, move trailing data to eol_buffer and truncate.
If not found, put everything in eol_buffer and return.
When entering this section, eol_buffer is always empty. */

if (!size) return;

for (i=size-1;;i--)
	{
	if ((i<0) || (buf[i]=='\n'))
		{
		mp->eol_buffer.len=(size-i-1);
		if (mp->eol_buffer.len)
			{
			DEBUG1(mp,3,"Storing %lu bytes in eol buffer"
				,(unsigned long)(mp->eol_buffer.len));
			size=i+1;
			mp->eol_buffer.buf=duplicate_mem(&(buf[size]),mp->eol_buffer.len);
			}
		break;
		}
	}<commit_msg>write.c: Add EOL_BUF_IS_EMPTY() macro to improve readability<commit_after>/*=============================================================================

Copyright 2008 <NAME> (<EMAIL>)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
=============================================================================*/

#define APPEND_TO_EOL_BUF(_buf,_size) { \
	DEBUG1(mp,3,"Appending %lu bytes to eol buffer",(unsigned long)(_size)); \
	mp->eol_buffer.buf=allocate(mp->eol_buffer.buf \
		,mp->eol_buffer.len+_size); \
	memcpy(&(mp->eol_buffer.buf[mp->eol_buffer.len]),_buf,_size); \
	mp->eol_buffer.len += _size; \
	}

#define WRITE_EOL_BUF(_flags,_t) { \
	write_level2(mp,mp->eol_buffer.buf,mp->eol_buffer.len,_flags,_t); \
	FREE_EOL_BUF(); \
	}

#define FREE_EOL_BUF() \
	mp->eol_buffer.buf=allocate(mp->eol_buffer.buf,mp->eol_buffer.len=0);

#define EOL_BUF_IS_EMPTY()	(mp->eol_buffer.buf == NULL)

/*----------------------------------------------*/

static TIMESTAMP last_write_time=0;

/*----------------------------------------------*/
/* Called when the manager is closed. Even if we don't have an EOL, we
* must write the buffer */

LIB_INTERNAL void write_end(LOGMANAGER mp)
{
WRITE_EOL_BUF(0,last_write_time);
}

/*----------------------------------------------*/
/* Buffer output so that the files are cut on line boundaries ('\n' char) */

void logmanager_write(LOGMANAGER mp, const char *buf, apr_off_t size
	,unsigned int flags, TIMESTAMP t)
{
int i;

CHECK_MP(mp);
NORMALIZE_TIMESTAMP(t);

DEBUG1(mp,2,"Starting logmanager_write (size=%lu)",(unsigned long)size);
INCR_STAT_COUNT(mp,write);

last_write_time=t;

if ((!buf) || (!size)) return;

if (mp->flags & LMGR_IGNORE_EOL)
	{
	write_level2(mp,buf,size,flags,t);
	return;
	}

/* 1. If eol_buffer contains some data from a previous write, search a '\n'
from the beginning. If found, output the buffer and input data up to \n,
truncate data. If not found, append data to the buffer */

if (! EOL_BUF_IS_EMPTY())
	{
	for (i=0;i<size;i++)
		{
		if (buf[i]=='\n')
			{
			/* Append to eol buf first, so that the whole line is sent to write_level2 in only one call */
			/* So, rotation can occur. If we did it with 2 calls, we should disable rotation for the */
			/* second call, and file size could be exceeded */
			APPEND_TO_EOL_BUF(buf,i+1);
			buf += (i+1);
			size -= (i+1);
			WRITE_EOL_BUF(flags,t);
			break;
			}
		}

	if (! EOL_BUF_IS_EMPTY()) /* if not found, append to eol_buffer.buf */
		{
		APPEND_TO_EOL_BUF(buf,size);
		return;
		}
	}

/* 2. Search last \n. If found, move trailing data to eol_buffer and truncate.
If not found, put everything in eol_buffer and return.
When entering this section, eol_buffer is always empty. */

if (!size) return;

for (i=size-1;;i--)
	{
	if ((i<0) || (buf[i]=='\n'))
		{
		mp->eol_buffer.len=(size-i-1);
		if (mp->eol_buffer.len)
			{
			DEBUG1(mp,3,"Storing %lu bytes in eol buffer"
				,(unsigned long)(mp->eol_buffer.len));
			size=i+1;
			mp->eol_buffer.buf=duplicate_mem(&(buf[size]),mp->eol_buffer.len);
			}
		break;
		}
	}
{'fraction_non_alphanumeric': 0.21150543789353177, 'fraction_numerical': 0.008872352604464796, 'mean_word_length': 3.417825537294564, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2697784', 'n_tokens_mistral': 2537, 'n_tokens_neox': 2399, 'n_words': 670}