Click here to Skip to main content
15,892,222 members
Articles / Containers / Virtual Machine

An extendable report editor

Rate me:
Please Sign up or sign in to vote.
5.00/5 (11 votes)
3 Sep 2008CPOL3 min read 41.2K   2K   35  
An extendable report editor. You can simply add your own controls without recompiling the program or writing annoying plug-ins.
#include "stdafx.h"
#include "assert.h"
#include "syslog.h"
#include "mem.h"

CMem::CMem()
{
	this->Init();
}
CMem::~CMem()
{
	this->Destroy();
}

int CMem::Destroy()
{
	if(p == NULL)
		return ERROR;

	if(this->self_alloc)
    {
		DEL_ARRAY(this->p);
	}

	this->p = NULL;
	this->self_alloc = FALSE;
    this->size = 0;
	this->max_size = 0;
    this->offset = 0;

	CFileBase::Destroy();

	return OK;
}
long CMem::Malloc(long asize)
{
    if(asize <= 0)
        return 0;

    if(this->p != NULL)
        return 0;

	NEW_ARRAY(this->p,char,asize);

	this->self_alloc = TRUE;
    this->size = 0;
    this->offset = 0;
	this->max_size = asize;

    return asize;
}
long CMem::Read(void *buf,long n)
{
    long  copy_length;
    
    ASSERT(this->p);

    if(this->size - this->offset > n)
        copy_length=n;
    else
        copy_length=this->size - this->offset;
	
	if(copy_length > 0)
	{
		memcpy(buf,(this->p+this->offset),copy_length);
	    this->offset += copy_length; 
		return copy_length;
	}

	return 0;
}
long CMem::Write(void *buf,long n)
{
    long  copy_length;
	
	ASSERT(this->p);
	
	if(n + this->offset > this->size)
	{
		this->AddBlock(n + this->offset - this->size);
	}
	
    if(this->size - this->offset > n)
        copy_length=n;
    else
        copy_length=this->size - this->offset;

	if(copy_length > 0)
	{
		memcpy((this->p+this->offset),buf,copy_length);
		this->offset += copy_length; 
		return copy_length;  
	}
    
	return 0;
}
int CMem::Zero()
{
	ASSERT(this->p);

    memset(this->p,0,this->max_size);

    this->offset = 0;
	this->size = 0;

	return OK;
}
int CMem::Init()
{
	CFileBase::Init();
    this->p = NULL;
    this->size = 0;
    this->offset = 0;
	this->self_alloc = FALSE;
	this->max_size = 0;

	return OK;
}

int CMem::Print()
{
	syslog.Puts(p);
    LOG("\np = 0x%x\n",this->p);
    LOG("size = %ld\n",this->size);
	LOG("max_size = %ld\n",this->max_size);
    LOG("offset = %ld\n",this->offset);

	return OK;
}

int CMem::SetP(char *p,long s)
{
	
	ASSERT(this->p == NULL);

	this->p = p;
	this->size = s;
	this->max_size = s;

	return OK;
}
int CMem::Seek(long off)
{
	if(off < 0 || off >= this->size)
		this->offset = this->size;
	else
		this->offset = off;
	
	return OK;
}
//��CMem����һ���ַ������淶��
//��ǰ��Ŀո�����
int CMem::StdStr()
{
	int i,l,n;
	
	ASSERT(this->p);

	l = this->GetSize();

	for(i = l - 1; i >= 0; i--)
	{
		if( CFileBase::IsEmptyChar(p[i])	)
			this->p[i] = 0;
		else
			break;
	}
	
	l = i + 1;
	for(i = 0; i < l; i++)
	{
		if(	! CFileBase::IsEmptyChar(p[i]) )
			break;
	}
	l -= i;
	n = i;
	for(i = 0; i < l; i++)
	{
		this->p[i] = this->p[i + n];
	}

	this->p[l] = 0;
	this->SetSize(l + 1);

	return OK;
}
long CMem::GetOffset()
{
	return this->offset;
}

long CMem::GetSize()
{
	return this->size;
}
int CMem::SetSize(long ssize)
{
	if(ssize >= 0 && ssize <= this->max_size)
	{
		this->size = ssize;
		this->AdjustOffset();
		return OK;
	}

	return ERROR;
}

int CMem::AddBlock(long bsize)
{
	//���û�з����ڴ棬��ô����
	//ע��ֻ��һ��
	if(this->p == NULL)
	{
		this->Malloc(bsize);
		return OK;
	}

	this->size += bsize;

	if(this->size < 0)
		this->size = 0;

	if(this->size > this->max_size)
		this->size = this->max_size;

	return OK;
}

int CMem::SizeToMax()
{
	this->size = this->GetMaxSize();

	return OK;
}

int CMem::StrCmp(char *str)
{
	ASSERT(this->p && str);
	return strcmp(this->p,str);
}

int CMem::StrICmp(char *str)
{
	ASSERT(this->p && str);
	return stricmp(this->p,str);
}

int CMem::SetStr(char *str)
{
	ASSERT(str);

	CMem mem;
	mem.Init();
	mem.SetP(str,strlen(str) + 1);

	this->Copy(&mem);

	return OK;
}

long CMem::GetMaxSize()
{
	return this->max_size;
}

int CMem::Copy(CMem *mem)
{
	if(this->GetMaxSize() < mem->GetSize())
	{
		this->Destroy();
		this->Init();
		this->Malloc(mem->GetSize());
	}

	this->SetSize(0);
	this->WriteFile(mem);

	return OK;
}

int CMem::StartWith(char *str,int case_sensive)
{
	ASSERT(str);
	int c,l = strlen(str);

	if(l < 1) return FALSE;

	if(l > this->GetSize())
		return FALSE;

	char ch = this->p[l];
	this->p[l] = 0;

	if(case_sensive) 
		c = strcmp(this->p,str);
	else
		c = stricmp(this->p,str);

	if( c == 0 )
	{
		this->p[l] = ch;
		return TRUE;
	}

	this->p[l] = ch;
	
	return FALSE;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
China China
26 years old, 2 years work experience.

Comments and Discussions