author | zautrix <zautrix> | 2005-07-04 19:17:50 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2005-07-04 19:17:50 (UTC) |
commit | 2710cddd5b0b69efc1c5a7f8516d5b451ff258f1 (patch) (side-by-side diff) | |
tree | abcfa306ccfcdf7aba536cc3f476c0bdbe9ef12b | |
parent | 4f3238355f67a256f338986ca13322ef23960895 (diff) | |
download | kdepimpi-2710cddd5b0b69efc1c5a7f8516d5b451ff258f1.zip kdepimpi-2710cddd5b0b69efc1c5a7f8516d5b451ff258f1.tar.gz kdepimpi-2710cddd5b0b69efc1c5a7f8516d5b451ff258f1.tar.bz2 |
free ring buffer
-rw-r--r-- | libical/src/libical/icalmemory.c | 2 | ||||
-rw-r--r-- | libkcal/icalformat.cpp | 4 |
2 files changed, 4 insertions, 2 deletions
diff --git a/libical/src/libical/icalmemory.c b/libical/src/libical/icalmemory.c index 18d7ef9..3ed38ad 100644 --- a/libical/src/libical/icalmemory.c +++ b/libical/src/libical/icalmemory.c @@ -1,368 +1,368 @@ /* -*- Mode: C -*- ====================================================================== FILE: icalmemory.c CREATOR: eric 30 June 1999 $Id$ $Locker$ The contents of this file are subject to the Mozilla Public License Version 1.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.mozilla.org/MPL/ Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. This program is free software; you can redistribute it and/or modify it under the terms of either: The LGPL as published by the Free Software Foundation, version 2.1, available at: http://www.fsf.org/copyleft/lesser.html Or: The Mozilla Public License Version 1.0. You may obtain a copy of the License at http://www.mozilla.org/MPL/ The Original Code is icalmemory.h ======================================================================*/ /** * @file icalmemory.c * @brief Common memory management routines. * * libical often passes strings back to the caller. To make these * interfaces simple, I did not want the caller to have to pass in a * memory buffer, but having libical pass out newly allocated memory * makes it difficult to de-allocate the memory. * * The ring buffer in this scheme makes it possible for libical to pass * out references to memory which the caller does not own, and be able * to de-allocate the memory later. The ring allows libical to have * several buffers active simultaneously, which is handy when creating * string representations of components. */ #define ICALMEMORY_C #ifdef HAVE_CONFIG_H #include "config.h" #endif #ifdef DMALLOC #include "dmalloc.h" #endif #include "icalmemory.h" #include "icalerror.h" #include <stdio.h> /* for printf (debugging) */ #include <stdlib.h> /* for malloc, realloc */ #include <string.h> /* for memset(), strdup */ #ifdef WIN32 #include <windows.h> #endif -#define BUFFER_RING_SIZE 50 +#define BUFFER_RING_SIZE 100 #define MIN_BUFFER_SIZE 64 /* HACK. Not threadsafe */ typedef struct { int pos; void *ring[BUFFER_RING_SIZE]; } buffer_ring; void icalmemory_free_tmp_buffer (void* buf); void icalmemory_free_ring_byval(buffer_ring *br); static buffer_ring* global_buffer_ring = 0; #ifdef HAVE_PTHREAD #include <pthread.h> static pthread_key_t ring_key; static pthread_once_t ring_key_once = PTHREAD_ONCE_INIT; static void ring_destroy(void * buf) { if (buf) icalmemory_free_ring_byval((buffer_ring *) buf); pthread_setspecific(ring_key, NULL); } static void ring_key_alloc(void) { pthread_key_create(&ring_key, ring_destroy); } #endif static buffer_ring * buffer_ring_new(void) { buffer_ring *br; int i; br = (buffer_ring *)malloc(sizeof(buffer_ring)); for(i=0; i<BUFFER_RING_SIZE; i++){ br->ring[i] = 0; } br->pos = 0; return(br); } #ifdef HAVE_PTHREAD static buffer_ring* get_buffer_ring_pthread(void) { buffer_ring *br; pthread_once(&ring_key_once, ring_key_alloc); br = pthread_getspecific(ring_key); if (!br) { br = buffer_ring_new(); pthread_setspecific(ring_key, br); } return(br); } #endif /* get buffer ring via a single global for a non-threaded program */ static buffer_ring* get_buffer_ring_global(void) { if (global_buffer_ring == 0) { global_buffer_ring = buffer_ring_new(); } return(global_buffer_ring); } static buffer_ring *get_buffer_ring(void) { #ifdef HAVE_PTHREAD return(get_buffer_ring_pthread()); #else return get_buffer_ring_global(); #endif } /** Add an existing buffer to the buffer ring */ void icalmemory_add_tmp_buffer(void* buf) { buffer_ring *br = get_buffer_ring(); /* Wrap around the ring */ if(++(br->pos) == BUFFER_RING_SIZE){ br->pos = 0; } /* Free buffers as their slots are overwritten */ if ( br->ring[br->pos] != 0){ free( br->ring[br->pos]); } /* Assign the buffer to a slot */ br->ring[br->pos] = buf; } /** * Create a new temporary buffer on the ring. Libical owns these and * will deallocate them. */ void* icalmemory_tmp_buffer (size_t size) { char *buf; if (size < MIN_BUFFER_SIZE){ size = MIN_BUFFER_SIZE; } buf = (void*)malloc(size); if( buf == 0){ icalerror_set_errno(ICAL_NEWFAILED_ERROR); return 0; } memset(buf,0,size); icalmemory_add_tmp_buffer(buf); return buf; } /** get rid of this buffer ring */ void icalmemory_free_ring_byval(buffer_ring *br) { int i; for(i=0; i<BUFFER_RING_SIZE; i++){ if ( br->ring[i] != 0){ free( br->ring[i]); } } free(br); } void icalmemory_free_ring() { buffer_ring *br; br = get_buffer_ring(); icalmemory_free_ring_byval(br); if ( global_buffer_ring == br ) global_buffer_ring = 0; } /** Like strdup, but the buffer is on the ring. */ char* icalmemory_tmp_copy(const char* str) { char* b = icalmemory_tmp_buffer(strlen(str)+1); strcpy(b,str); return b; } char* icalmemory_strdup(const char *s) { return strdup(s); } void icalmemory_free_tmp_buffer (void* buf) { if(buf == 0) { return; } free(buf); } /* * These buffer routines create memory the old fashioned way -- so the * caller will have to deallocate the new memory */ void* icalmemory_new_buffer(size_t size) { void *b = malloc(size); if( b == 0){ icalerror_set_errno(ICAL_NEWFAILED_ERROR); return 0; } memset(b,0,size); return b; } void* icalmemory_resize_buffer(void* buf, size_t size) { void *b = realloc(buf, size); if( b == 0){ icalerror_set_errno(ICAL_NEWFAILED_ERROR); return 0; } return b; } void icalmemory_free_buffer(void* buf) { free(buf); } void icalmemory_append_string(char** buf, char** pos, size_t* buf_size, const char* string) { char *new_buf; char *new_pos; size_t data_length, final_length, string_length; #ifndef ICAL_NO_INTERNAL_DEBUG icalerror_check_arg_rv( (buf!=0),"buf"); icalerror_check_arg_rv( (*buf!=0),"*buf"); icalerror_check_arg_rv( (pos!=0),"pos"); icalerror_check_arg_rv( (*pos!=0),"*pos"); icalerror_check_arg_rv( (buf_size!=0),"buf_size"); icalerror_check_arg_rv( (*buf_size!=0),"*buf_size"); icalerror_check_arg_rv( (string!=0),"string"); #endif string_length = strlen(string); data_length = (size_t)*pos - (size_t)*buf; final_length = data_length + string_length; if ( final_length >= (size_t) *buf_size) { *buf_size = (*buf_size) * 2 + final_length; new_buf = realloc(*buf,*buf_size); new_pos = (void*)((size_t)new_buf + data_length); *pos = new_pos; *buf = new_buf; } strcpy(*pos, string); *pos += string_length; } void icalmemory_append_char(char** buf, char** pos, size_t* buf_size, char ch) { char *new_buf; char *new_pos; size_t data_length, final_length; #ifndef ICAL_NO_INTERNAL_DEBUG icalerror_check_arg_rv( (buf!=0),"buf"); icalerror_check_arg_rv( (*buf!=0),"*buf"); icalerror_check_arg_rv( (pos!=0),"pos"); icalerror_check_arg_rv( (*pos!=0),"*pos"); icalerror_check_arg_rv( (buf_size!=0),"buf_size"); icalerror_check_arg_rv( (*buf_size!=0),"*buf_size"); #endif data_length = (size_t)*pos - (size_t)*buf; final_length = data_length + 2; if ( final_length > (size_t) *buf_size ) { *buf_size = (*buf_size) * 2 + final_length +1; new_buf = realloc(*buf,*buf_size); new_pos = (void*)((size_t)new_buf + data_length); *pos = new_pos; *buf = new_buf; } **pos = ch; *pos += 1; **pos = 0; } diff --git a/libkcal/icalformat.cpp b/libkcal/icalformat.cpp index d9fe40b..5877406 100644 --- a/libkcal/icalformat.cpp +++ b/libkcal/icalformat.cpp @@ -1,460 +1,462 @@ /* This file is part of libkcal. Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This library 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 Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include <qdatetime.h> #include <qstring.h> #include <qptrlist.h> #include <qregexp.h> #include <qclipboard.h> #include <qfile.h> #include <qtextstream.h> #include <qtextcodec.h> #include <stdlib.h> #include <kdebug.h> #include <kglobal.h> #include <klocale.h> extern "C" { #include <ical.h> #include <icalss.h> #include <icalparser.h> #include <icalrestriction.h> } #include "calendar.h" #include "calendarlocal.h" #include "journal.h" #include "icalformat.h" #include "icalformatimpl.h" #define _ICAL_VERSION "2.0" using namespace KCal; ICalFormat::ICalFormat( ) { mImpl = new ICalFormatImpl( this ); tzOffsetMin = 0; //qDebug("new ICalFormat() "); } ICalFormat::~ICalFormat() { delete mImpl; //qDebug("delete ICalFormat "); } bool ICalFormat::load( Calendar *calendar, const QString &fileName) { clearException(); QFile file( fileName ); if (!file.open( IO_ReadOnly ) ) { setException(new ErrorFormat(ErrorFormat::LoadError)); return false; } QTextStream ts( &file ); QString text; ts.setEncoding( QTextStream::Latin1 ); text = ts.read(); file.close(); return fromString( calendar, text ); } //#include <qdatetime.h> bool ICalFormat::save( Calendar *calendar, const QString &fileName ) { //kdDebug(5800) << "ICalFormat::save(): " << fileName << endl; //qDebug("ICalFormat::save "); clearException(); QString text = toString( calendar ); //return false; // qDebug("to string takes ms: %d ",is.elapsed() ); if ( text.isNull() ) return false; // TODO: write backup file //is.restart(); QFile file( fileName ); if (!file.open( IO_WriteOnly ) ) { setException(new ErrorFormat(ErrorFormat::SaveError, i18n("Could not open file '%1'").arg(fileName))); return false; } QTextStream ts( &file ); ts.setEncoding( QTextStream::Latin1 ); ts << text; file.close(); //qDebug("saving file takes ms: %d ", is.elapsed() ); return true; } bool ICalFormat::fromString( Calendar *cal, const QString &text ) { setTimeZone( cal->timeZoneId(), !cal->isLocalTime() ); // qDebug("ICalFormat::fromString tz: %s ", cal->timeZoneId().latin1()); // Get first VCALENDAR component. // TODO: Handle more than one VCALENDAR or non-VCALENDAR top components icalcomponent *calendar; //calendar = icalcomponent_new_from_string( text.local8Bit().data()); // good calendar = icalcomponent_new_from_string( text.utf8().data()); calendar = icalcomponent_new_from_string( (char*)text.latin1()); if (!calendar) { setException(new ErrorFormat(ErrorFormat::ParseErrorIcal)); return false; } bool success = true; if (icalcomponent_isa(calendar) != ICAL_VCALENDAR_COMPONENT) { setException(new ErrorFormat(ErrorFormat::NoCalendar)); success = false; } else { // put all objects into their proper places if ( !mImpl->populate( cal, calendar ) ) { if ( !exception() ) { setException(new ErrorFormat(ErrorFormat::ParseErrorKcal)); } success = false; } else mLoadedProductId = mImpl->loadedProductId(); } icalcomponent_free( calendar ); + icalmemory_free_ring(); return success; } Incidence *ICalFormat::fromString( const QString &text ) { CalendarLocal cal( mTimeZoneId ); fromString(&cal, text); Incidence *ical = 0; QPtrList<Event> elist = cal.events(); if ( elist.count() > 0 ) { ical = elist.first(); } else { QPtrList<Todo> tlist = cal.todos(); if ( tlist.count() > 0 ) { ical = tlist.first(); } else { QPtrList<Journal> jlist = cal.journals(); if ( jlist.count() > 0 ) { ical = jlist.first(); } } } return ical; } #include <qapp.h> QString ICalFormat::toString( Calendar *cal ) { setTimeZone( cal->timeZoneId(), !cal->isLocalTime() ); icalcomponent *calendar = mImpl->createCalendarComponent(cal); icalcomponent *component; // todos QPtrList<Todo> todoList = cal->rawTodos(); QPtrListIterator<Todo> qlt(todoList); for (; qlt.current(); ++qlt) { component = mImpl->writeTodo(qlt.current()); icalcomponent_add_component(calendar,component); //qDebug(" todos "); qApp->processEvents(); } // events QPtrList<Event> events = cal->rawEvents(); Event *ev; for(ev=events.first();ev;ev=events.next()) { component = mImpl->writeEvent(ev); icalcomponent_add_component(calendar,component); //qDebug("events "); qApp->processEvents(); } // journals QPtrList<Journal> journals = cal->journals(); Journal *j; for(j=journals.first();j;j=journals.next()) { component = mImpl->writeJournal(j); icalcomponent_add_component(calendar,component); //qDebug("journals "); qApp->processEvents(); } const char *text; QString ret =""; text = icalcomponent_as_ical_string( calendar ); qApp->processEvents(); // text = "BEGIN:VCALENDAR\nPRODID\n :-//K Desktop Environment//NONSGML libkcal 3.1//EN\nVERSION\n :2.0\nBEGIN:VEVENT\nDTSTAMP\n :20031231T213514Z\nORGANIZER\n :MAILTO:lutz@putz.de\nCREATED\n :20031231T213513Z\nUID\n :libkcal-1295166342.120\nSEQUENCE\n :0\nLAST-MODIFIED\n :20031231T213513Z\nSUMMARY\n :test1\nCLASS\n :PUBLIC\nPRIORITY\n :3\nDTSTART\n :20040101T090000Z\nDTEND\n :20040101T110000Z\nTRANSP\n :OPAQUE\nEND:VEVENT\nEND:VCALENDAR\n"; if ( text ) { ret = QString ( text ); } icalcomponent_free( calendar ); if (!text) { setException(new ErrorFormat(ErrorFormat::SaveError, i18n("libical error"))); + icalmemory_free_ring(); return QString::null; } - + icalmemory_free_ring(); return ret; } QString ICalFormat::toICalString( Incidence *incidence ) { CalendarLocal cal( mTimeZoneId ); cal.addIncidence( incidence->clone() ); return toString( &cal ); } QString ICalFormat::toString( Incidence *incidence ) { icalcomponent *component; component = mImpl->writeIncidence( incidence ); const char *text = icalcomponent_as_ical_string( component ); icalcomponent_free( component ); return QString::fromLocal8Bit( text ); } QString ICalFormat::toString( Recurrence *recurrence ) { icalproperty *property; property = mImpl->writeRecurrenceRule( recurrence ); const char *text = icalproperty_as_ical_string( property ); icalproperty_free( property ); return QString::fromLocal8Bit( text ); } /* bool ICalFormat::fromString( Recurrence * recurrence, const QString& rrule ) { bool success = true; icalerror_clear_errno(); struct icalrecurrencetype recur = icalrecurrencetype_from_string( rrule ); if ( icalerrno != ICAL_NO_ERROR ) { kdDebug() << "Recurrence parsing error: " << icalerror_strerror( icalerrno ) << endl; success = false; } if ( success ) { mImpl->readRecurrence( recur, recurrence ); } return success; } */ QString ICalFormat::createScheduleMessage(IncidenceBase *incidence, Scheduler::Method method) { icalcomponent *message = mImpl->createScheduleComponent(incidence,method); QString messageText = icalcomponent_as_ical_string(message); return messageText; } ScheduleMessage *ICalFormat::parseScheduleMessage( Calendar *cal, const QString &messageText ) { setTimeZone( cal->timeZoneId(), !cal->isLocalTime() ); clearException(); if (messageText.isEmpty()) return 0; icalcomponent *message; message = icalparser_parse_string(messageText.local8Bit()); if (!message) return 0; icalproperty *m = icalcomponent_get_first_property(message, ICAL_METHOD_PROPERTY); if (!m) return 0; icalcomponent *c; IncidenceBase *incidence = 0; c = icalcomponent_get_first_component(message,ICAL_VEVENT_COMPONENT); if (c) { incidence = mImpl->readEvent(c); } if (!incidence) { c = icalcomponent_get_first_component(message,ICAL_VTODO_COMPONENT); if (c) { incidence = mImpl->readTodo(c); } } if (!incidence) { c = icalcomponent_get_first_component(message,ICAL_VFREEBUSY_COMPONENT); if (c) { incidence = mImpl->readFreeBusy(c); } } if (!incidence) { kdDebug() << "ICalFormat:parseScheduleMessage: object is not a freebusy, event or todo" << endl; return 0; } kdDebug(5800) << "ICalFormat::parseScheduleMessage() getting method..." << endl; icalproperty_method icalmethod = icalproperty_get_method(m); Scheduler::Method method; switch (icalmethod) { case ICAL_METHOD_PUBLISH: method = Scheduler::Publish; break; case ICAL_METHOD_REQUEST: method = Scheduler::Request; break; case ICAL_METHOD_REFRESH: method = Scheduler::Refresh; break; case ICAL_METHOD_CANCEL: method = Scheduler::Cancel; break; case ICAL_METHOD_ADD: method = Scheduler::Add; break; case ICAL_METHOD_REPLY: method = Scheduler::Reply; break; case ICAL_METHOD_COUNTER: method = Scheduler::Counter; break; case ICAL_METHOD_DECLINECOUNTER: method = Scheduler::Declinecounter; break; default: method = Scheduler::NoMethod; kdDebug(5800) << "ICalFormat::parseScheduleMessage(): Unknow method" << endl; break; } if (!icalrestriction_check(message)) { setException(new ErrorFormat(ErrorFormat::Restriction, Scheduler::translatedMethodName(method) + ": " + mImpl->extractErrorProperty(c))); return 0; } icalcomponent *calendarComponent = mImpl->createCalendarComponent(cal); Incidence *existingIncidence = cal->event(incidence->uid()); if (existingIncidence) { // TODO: check, if cast is required, or if it can be done by virtual funcs. if (existingIncidence->typeID() == todoID ) { Todo *todo = static_cast<Todo *>(existingIncidence); icalcomponent_add_component(calendarComponent, mImpl->writeTodo(todo)); } if (existingIncidence->typeID() == eventID ) { Event *event = static_cast<Event *>(existingIncidence); icalcomponent_add_component(calendarComponent, mImpl->writeEvent(event)); } } else { calendarComponent = 0; } qDebug("icalclassify commented out "); ScheduleMessage::Status status; #if 0 icalclass result = icalclassify(message,calendarComponent,(char *)""); switch (result) { case ICAL_PUBLISH_NEW_CLASS: status = ScheduleMessage::PublishNew; break; case ICAL_OBSOLETE_CLASS: status = ScheduleMessage::Obsolete; break; case ICAL_REQUEST_NEW_CLASS: status = ScheduleMessage::RequestNew; break; case ICAL_REQUEST_UPDATE_CLASS: status = ScheduleMessage::RequestUpdate; break; case ICAL_UNKNOWN_CLASS: default: status = ScheduleMessage::Unknown; break; } #endif status = ScheduleMessage::RequestUpdate; return new ScheduleMessage(incidence,method,status); } void ICalFormat::setTimeZone( const QString &id, bool utc ) { mTimeZoneId = id; mUtc = utc; tzOffsetMin = KGlobal::locale()->timezoneOffset(mTimeZoneId); //qDebug("ICalFormat::setTimeZoneOffset %s %d ",mTimeZoneId.latin1(), tzOffsetMin); } QString ICalFormat::timeZoneId() const { return mTimeZoneId; } bool ICalFormat::utc() const { return mUtc; } int ICalFormat::timeOffset() { return tzOffsetMin; } const char *ICalFormat::tzString() { const char* ret = (const char* ) mTzString; return ret; } |