home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 275 / DPCS0111DVD.ISO / Toolkit / Audio-Visual / VirtualDub / Source / VirtualDub-1.9.10-src.7z / src / system / source / VDString.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2009-09-14  |  5.5 KB  |  210 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    System library component
  3. //    Copyright (C) 1998-2004 Avery Lee, All Rights Reserved.
  4. //
  5. //    Beginning with 1.6.0, the VirtualDub system library is licensed
  6. //    differently than the remainder of VirtualDub.  This particular file is
  7. //    thus licensed as follows (the "zlib" license):
  8. //
  9. //    This software is provided 'as-is', without any express or implied
  10. //    warranty.  In no event will the authors be held liable for any
  11. //    damages arising from the use of this software.
  12. //
  13. //    Permission is granted to anyone to use this software for any purpose,
  14. //    including commercial applications, and to alter it and redistribute it
  15. //    freely, subject to the following restrictions:
  16. //
  17. //    1.    The origin of this software must not be misrepresented; you must
  18. //        not claim that you wrote the original software. If you use this
  19. //        software in a product, an acknowledgment in the product
  20. //        documentation would be appreciated but is not required.
  21. //    2.    Altered source versions must be plainly marked as such, and must
  22. //        not be misrepresented as being the original software.
  23. //    3.    This notice may not be removed or altered from any source
  24. //        distribution.
  25.  
  26. #include "stdafx.h"
  27. #include <vd2/system/VDString.h>
  28. #include <vd2/system/vdstl.h>
  29.  
  30. const VDStringSpanA::value_type VDStringSpanA::sNull[1] = {0};
  31.  
  32. void VDStringA::push_back_extend() {
  33.     VDASSERT(mpEOS == mpEnd);
  34.     size_type current_size = (size_type)(mpEnd - mpBegin);
  35.  
  36.     reserve_slow(current_size * 2 + 1, current_size);
  37. }
  38.  
  39. void VDStringA::resize_slow(size_type n, size_type current_size) {
  40.     resize_slow(n, current_size, 0);
  41. }
  42.  
  43. void VDStringA::resize_slow(size_type n, size_type current_size, value_type c) {
  44.     VDASSERT(n > current_size);
  45.  
  46.     size_type current_capacity = (size_type)(mpEOS - mpBegin);
  47.     if (n > current_capacity)
  48.         reserve_slow(n, current_capacity);
  49.  
  50.     memset(mpBegin + current_size, c, n - current_size);
  51.     mpEnd = mpBegin + n;
  52.     *mpEnd = 0;
  53. }
  54.  
  55. void VDStringA::reserve_slow(size_type n, size_type current_capacity) {
  56.     VDASSERT(n > current_capacity);
  57.  
  58.     size_type current_size = (size_type)(mpEnd - mpBegin);
  59.     value_type *s = new value_type[n + 1];
  60.     memcpy(s, mpBegin, (current_size + 1) * sizeof(value_type));
  61.     if (mpBegin != sNull)
  62.         delete[] mpBegin;
  63.  
  64.     mpBegin = s;
  65.     mpEnd = s + current_size;
  66.     mpEOS = s + n;
  67. }
  68.  
  69. void VDStringA::reserve_amortized_slow(size_type n, size_type current_size, size_type current_capacity) {
  70.     n += current_size;
  71.  
  72.     size_type doublesize = current_size * 2;
  73.     if (n < doublesize)
  74.         n = doublesize;
  75.  
  76.     reserve_slow(n, current_capacity);
  77. }
  78.  
  79. VDStringA& VDStringA::sprintf(const value_type *format, ...) {
  80.     clear();
  81.     va_list val;
  82.     va_start(val, format);
  83.     append_vsprintf(format, val);
  84.     va_end(val);
  85.     return *this;
  86. }
  87.  
  88. VDStringA& VDStringA::append_sprintf(const value_type *format, ...) {
  89.     va_list val;
  90.     va_start(val, format);
  91.     append_vsprintf(format, val);
  92.     va_end(val);
  93.     return *this;
  94. }
  95.  
  96. VDStringA& VDStringA::append_vsprintf(const value_type *format, va_list val) {
  97.     char buf[2048];
  98.  
  99.     int len = _vsnprintf(buf, 2048, format, val);
  100.     if (len >= 0)
  101.         append(buf, buf+len);
  102.     else {
  103.         int len;
  104.  
  105.         vdfastvector<char> tmp;
  106.         for(int siz = 8192; siz <= 65536; siz += siz) {
  107.             tmp.resize(siz);
  108.  
  109.             char *tmpp = tmp.data();
  110.             len = _vsnprintf(tmp.data(), siz, format, val);
  111.             if (len >= 0) {
  112.                 append(tmpp, tmpp+len);
  113.                 break;
  114.             }
  115.         }
  116.     }
  117.  
  118.     return *this;
  119. }
  120.  
  121. ///////////////////////////////////////////////////////////////////////////////
  122.  
  123. const VDStringSpanW::value_type VDStringSpanW::sNull[1] = {0};
  124.  
  125. void VDStringW::push_back_extend() {
  126.     VDASSERT(mpEOS == mpEnd);
  127.     size_type current_size = (size_type)(mpEnd - mpBegin);
  128.  
  129.     reserve_slow(current_size * 2 + 1, current_size);
  130. }
  131.  
  132. void VDStringW::resize_slow(size_type n, size_type current_size) {
  133.     VDASSERT(n > current_size);
  134.  
  135.     size_type current_capacity = (size_type)(mpEOS - mpBegin);
  136.     if (n > current_capacity)
  137.         reserve_slow(n, current_capacity);
  138.  
  139.     mpEnd = mpBegin + n;
  140.     *mpEnd = 0;
  141. }
  142.  
  143. void VDStringW::reserve_slow(size_type n, size_type current_capacity) {
  144.     VDASSERT(current_capacity == (size_type)(mpEOS - mpBegin));
  145.     VDASSERT(n > current_capacity);
  146.  
  147.     size_type current_size = (size_type)(mpEnd - mpBegin);
  148.     value_type *s = new value_type[n + 1];
  149.     memcpy(s, mpBegin, (current_size + 1) * sizeof(value_type));
  150.     if (mpBegin != sNull)
  151.         delete[] mpBegin;
  152.  
  153.     mpBegin = s;
  154.     mpEnd = s + current_size;
  155.     mpEOS = s + n;
  156. }
  157.  
  158. void VDStringW::reserve_amortized_slow(size_type n, size_type current_size, size_type current_capacity) {
  159.     n += current_size;
  160.  
  161.     size_type doublesize = current_size * 2;
  162.     if (n < doublesize)
  163.         n = doublesize;
  164.  
  165.     reserve_slow(n, current_capacity);
  166. }
  167.  
  168. VDStringW& VDStringW::sprintf(const value_type *format, ...) {
  169.     clear();
  170.     va_list val;
  171.     va_start(val, format);
  172.     append_vsprintf(format, val);
  173.     va_end(val);
  174.     return *this;
  175. }
  176.  
  177. VDStringW& VDStringW::append_sprintf(const value_type *format, ...) {
  178.     va_list val;
  179.     va_start(val, format);
  180.     append_vsprintf(format, val);
  181.     va_end(val);
  182.     return *this;
  183. }
  184.  
  185. VDStringW& VDStringW::append_vsprintf(const value_type *format, va_list val) {
  186.     wchar_t buf[1024];
  187.  
  188.     int len = vswprintf(buf, 1024, format, val);
  189.     if (len >= 0)
  190.         append(buf, buf+len);
  191.     else {
  192.         int len;
  193.  
  194.         vdfastvector<wchar_t> tmp;
  195.         for(int siz = 4096; siz <= 65536; siz += siz) {
  196.             tmp.resize(siz);
  197.  
  198.             wchar_t *tmpp = tmp.data();
  199.             len = vswprintf(tmpp, siz, format, val);
  200.             if (len >= 0) {
  201.                 append(tmpp, tmpp+len);
  202.                 break;
  203.             }
  204.         }
  205.     }
  206.  
  207.     va_end(val);
  208.     return *this;
  209. }
  210.