LCOV - code coverage report
Current view: top level - hwpfilter/source - mzstring.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 0 116 0.0 %
Date: 2014-04-11 Functions: 0 25 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : /* PURPOSE
      21             :  *   supposed to be used instead of std::string
      22             :  */
      23             : 
      24             : #include "mzstring.h"
      25             : 
      26             : #ifndef WIN32
      27             : #else
      28             : 
      29             :     #if defined _MSC_VER
      30             :         #pragma warning(push, 1)
      31             :     #endif
      32             : # include <windows.h>
      33             :     #if defined _MSC_VER
      34             :         #pragma warning(pop)
      35             :     #endif
      36             : #endif                                            /* WIN32 */
      37             : #include <stdio.h>
      38             : #include <stdlib.h>
      39             : #include <string.h>
      40             : 
      41             : #ifndef WIN32
      42             : # define wsprintf sprintf
      43             : #endif
      44             : 
      45             : const int AllocSize = 8;
      46             : 
      47           0 : inline int get_alloc_size(int len)
      48             : {
      49           0 :     return (len + AllocSize - 1) / AllocSize * AllocSize;
      50             : }
      51             : 
      52             : 
      53           0 : MzString::MzString()
      54             : {
      55           0 :     Length    = 0;
      56           0 :     Allocated = 0;
      57           0 :     Data      = 0;
      58           0 : }
      59             : 
      60             : 
      61           0 : MzString::~MzString()
      62             : {
      63           0 :     if (Data)
      64           0 :         free(Data);
      65           0 : }
      66             : 
      67             : 
      68           0 : MzString &MzString::operator = (MzString &s)
      69             : {
      70           0 :     int n = s.length();
      71           0 :     if (allocate(n))
      72             :     {
      73           0 :         if (n > 0) memcpy(Data, s.Data, n);
      74           0 :         Length = n;
      75             :     }
      76           0 :     return *this;
      77             : }
      78             : 
      79             : 
      80           0 : MzString &MzString::operator = (const char *s)
      81             : {
      82           0 :     if (s == NULL)
      83           0 :         s = "";
      84           0 :     int n = strlen(s);
      85           0 :     if (allocate(n))
      86             :     {
      87           0 :         if (n > 0) memcpy(Data, s, n);
      88           0 :         Length = n;
      89             :     }
      90           0 :     return *this;
      91             : }
      92             : 
      93             : 
      94           0 : void MzString::append(const char *s, int slen)
      95             : {
      96           0 :     if(!s || slen <= 0)
      97           0 :         return;
      98             : 
      99           0 :     int new_len = Length + slen;
     100           0 :     if (resize(new_len))
     101             :     {
     102           0 :         memcpy(Data + Length, s, slen);
     103           0 :         Length = new_len;
     104             :     }
     105             : }
     106             : 
     107             : 
     108           0 : void MzString::append(MzString const &s)
     109             : {
     110           0 :     if (s.Data)
     111           0 :         append(s.Data, s.length());
     112           0 : }
     113             : 
     114             : 
     115           0 : void MzString::append(const char *s)
     116             : {
     117           0 :     if (!s) return;
     118           0 :     append(s, strlen(s));
     119             : }
     120             : 
     121             : 
     122           0 : int MzString::compare(const char *s)
     123             : {
     124           0 :     if (!Data)   return -1;
     125           0 :     if (s==NULL) return 1;
     126             : 
     127           0 :     Data[Length] = 0;
     128           0 :     return strcmp(Data, s);
     129             : }
     130             : 
     131             : 
     132           0 : int MzString::find(char ch)
     133             : {
     134           0 :     return find(ch,0);
     135             : }
     136             : 
     137             : 
     138           0 : int MzString::find(char ch, int pos)
     139             : {
     140           0 :     for (int i = pos; i < Length; i++)
     141             :     {
     142           0 :         if (Data[i] == ch)
     143           0 :             return i;
     144             :     }
     145           0 :     return -1;
     146             : }
     147             : 
     148             : 
     149           0 : int MzString::rfind(char ch)
     150             : {
     151           0 :     return rfind(ch, Length - 1);
     152             : }
     153             : 
     154             : 
     155           0 : int MzString::rfind(char ch, int pos)
     156             : {
     157           0 :     if (pos >= Length)
     158           0 :         return -1;
     159             : 
     160           0 :     while (pos >= 0)
     161             :     {
     162           0 :         if (Data[pos] == ch)
     163           0 :             return pos;
     164           0 :         pos--;
     165             :     }
     166           0 :     return -1;
     167             : }
     168             : 
     169             : 
     170             : // += operator
     171             : 
     172           0 : MzString &MzString::operator += (char ch)
     173             : {
     174           0 :     append(&ch, 1);
     175           0 :     return *this;
     176             : }
     177             : 
     178             : 
     179           0 : MzString &MzString::operator += (const char *str)
     180             : {
     181           0 :     append(str);
     182           0 :     return *this;
     183             : }
     184             : 
     185             : 
     186           0 : MzString &MzString::operator += (MzString const &s)
     187             : {
     188           0 :     append(s);
     189           0 :     return *this;
     190             : }
     191             : 
     192             : 
     193             : // << operator
     194           0 : MzString &MzString::operator << (const char *str)
     195             : {
     196           0 :     append(str);
     197           0 :     return *this;
     198             : }
     199             : 
     200             : 
     201           0 : MzString &MzString::operator << (char ch)
     202             : {
     203           0 :     append(&ch, 1);
     204           0 :     return *this;
     205             : }
     206             : 
     207             : 
     208           0 : MzString &MzString::operator << (int i)
     209             : {
     210             :     char str[80];
     211             : 
     212           0 :     wsprintf(str, "%d", i);
     213           0 :     append(str);
     214           0 :     return *this;
     215             : }
     216             : 
     217             : 
     218           0 : MzString &MzString::operator << (long l)
     219             : {
     220             :     char str[80];
     221             : 
     222           0 :     wsprintf(str, "%ld", l);
     223           0 :     append(str);
     224           0 :     return *this;
     225             : }
     226             : 
     227             : 
     228           0 : MzString &MzString::operator << (MzString const &s)
     229             : {
     230           0 :     append(s);
     231           0 :     return *this;
     232             : }
     233             : 
     234             : 
     235           0 : char MzString::operator [] (int n)
     236             : {
     237           0 :     if (Data && 0 <= n && n < Length)
     238           0 :         return Data[n];
     239             : 
     240           0 :     return 0;
     241             : }
     242             : 
     243             : 
     244           0 : void MzString::replace(int pos, char ch)
     245             : {
     246           0 :     if (Data && 0 <= pos && pos < Length)
     247           0 :         Data[pos] = ch;
     248           0 : }
     249             : 
     250             : 
     251             : 
     252             : // Private Methods.
     253             : 
     254             : 
     255           0 : bool MzString::allocate(int len)
     256             : {
     257           0 :     len++;                                        // In case we want to add a null.
     258             : 
     259           0 :     if (len < 0)
     260           0 :         return false;
     261             : 
     262           0 :     if (Data)
     263             :     {
     264           0 :         if (len < Allocated)
     265           0 :             return true;
     266             :         else
     267             :         {
     268           0 :             int   n = get_alloc_size(len);
     269           0 :             char *p = (char *)realloc(Data, n);
     270           0 :             if (p)
     271             :             {
     272           0 :                 Data      = p;
     273           0 :                 Allocated = n;
     274           0 :                 return true;
     275             :             }
     276             :         }
     277             :     }
     278             :     else
     279             :     {
     280             : // In case we want to add a null.
     281           0 :         int n = get_alloc_size(len);
     282           0 :         Data  = (char *)malloc(n);
     283           0 :         if (Data)
     284             :         {
     285           0 :             Allocated = n;
     286           0 :             return true;
     287             :         }
     288             :     }
     289             : 
     290           0 :     return false;
     291             : }
     292             : 
     293             : 
     294           0 : bool MzString::resize(int len)
     295             : {
     296           0 :     return allocate(len);
     297             : }
     298             : 
     299             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10