[Aptitude-svn-commit] r3430 - in branches/aptitude-0.3/aptitude: . src/vscreen
Daniel Burrows
dburrows@costa.debian.org
Sat, 25 Jun 2005 18:25:46 +0000
Author: dburrows
Date: Sat Jun 25 18:25:44 2005
New Revision: 3430
Modified:
branches/aptitude-0.3/aptitude/ChangeLog
branches/aptitude-0.3/aptitude/src/vscreen/transcode.cc
Log:
Get rid of auto_ptr and new[]; use malloc/realloc/free instead, as
(among other things) auto_ptr doesn't like handling
arrays.
Modified: branches/aptitude-0.3/aptitude/ChangeLog
==============================================================================
--- branches/aptitude-0.3/aptitude/ChangeLog (original)
+++ branches/aptitude-0.3/aptitude/ChangeLog Sat Jun 25 18:25:44 2005
@@ -2,6 +2,12 @@
* src/vscreen/transcode.cc:
+ Get rid of the use of auto_ptr and new[], they aren't really
+ appropriate here. Use good old malloc/realloc/free calls to
+ manage our dynamic store.
+
+ * src/vscreen/transcode.cc:
+
Use nl_langinfo(), not localeset(), to find out what the current
charset is when transcoding.
Modified: branches/aptitude-0.3/aptitude/src/vscreen/transcode.cc
==============================================================================
--- branches/aptitude-0.3/aptitude/src/vscreen/transcode.cc (original)
+++ branches/aptitude-0.3/aptitude/src/vscreen/transcode.cc Sat Jun 25 18:25:44 2005
@@ -42,65 +42,60 @@
size_t outbufsize=1024, outbufremaining=1024;
size_t inremaining=strlen(s);
- try
+ char *outbufhead=(char *) malloc(outbufsize), *outbufcur=outbufhead;
+ if(!outbufhead)
{
- // auto_ptr used so we free stuff automatically when we return
- // (especially abnormally).
- auto_ptr<char> outbufhead(new char[outbufsize]);
- char *outbufcur=outbufhead.get();
+ errno=ENOMEM;
+ return false;
+ }
- while(inremaining>0)
+ while(inremaining>0)
+ {
+ if(iconv(converter,
+ const_cast<char **>(&s), &inremaining,
+ &outbufcur, &outbufremaining) == ((size_t)-1))
{
- if(iconv(converter,
- const_cast<char **>(&s), &inremaining,
- &outbufcur, &outbufremaining) == ((size_t)-1))
+ // Some error conditions can be corrected. There are three
+ // reasons iconv can terminate abnormally:
+ //
+ // (1) an invalid multibyte sequence occured. We do not
+ // attempt to recover in this case.
+ //
+ // (2) an incomplete multibyte sequence occured; as the
+ // input string is all the input we have, this reduces
+ // to case (1).
+ //
+ // (3) no room left in the output buffer. We respond by
+ // doubling the output buffer's size, or failing if
+ // it's doubled as far as it can go.
+
+ if(errno != E2BIG)
{
- // Some error conditions can be corrected. There are three
- // reasons iconv can terminate abnormally:
- //
- // (1) an invalid multibyte sequence occured. We do not
- // attempt to recover in this case.
- //
- // (2) an incomplete multibyte sequence occured; as the
- // input string is all the input we have, this reduces
- // to case (1).
- //
- // (3) no room left in the output buffer. We respond by
- // doubling the output buffer's size, or failing if
- // it's doubled as far as it can go.
-
- if(errno != E2BIG)
- {
- if(outbufremaining<outbufsize)
- out=wstring((wchar_t *) outbufhead.get(),
- outbufsize-outbufremaining);
- return false;
- }
- else
- {
- auto_ptr<char> outbufnew(new char[outbufsize]);
- memcpy(outbufnew.get(), outbufhead.get(),
- outbufsize-outbufremaining);
- outbufremaining+=outbufsize;
- outbufsize*=2;
- // will deallocate outbufhead.
- outbufhead=outbufnew;
- }
+ if(outbufremaining<outbufsize)
+ out=wstring((wchar_t *) outbufhead,
+ outbufsize-outbufremaining);
+
+ free(outbufhead);
+ return false;
}
else
- // if this fails, my understanding of iconv is wrong: the
- // iconv docs say that if it doesn't fail, then the whole
- // input sequence was converted.
- assert(inremaining==0);
+ {
+ size_t idx=outbufcur-outbufhead;
+ outbufremaining+=outbufsize;
+ outbufsize*=2;
+ outbufhead=(char *) realloc(outbufhead, outbufsize);
+ outbufcur=outbufhead+idx;
+ }
}
-
- out=wstring((wchar_t *) outbufhead.get(), outbufsize-outbufremaining);
- return true;
- }
- catch(bad_alloc)
- {
- errno=ENOMEM;
- return false;
+ else
+ // if this fails, my understanding of iconv is wrong: the
+ // iconv docs say that if it doesn't fail, then the whole
+ // input sequence was converted.
+ assert(inremaining==0);
}
+
+ out=wstring((wchar_t *) outbufhead, (outbufsize-outbufremaining)/sizeof(wchar_t));
+ free(outbufhead);
+ return true;
}