File: kfixedsizebuffer.cpp

package info (click to toggle)
kdeutils 4%3A3.3.2-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 14,628 kB
  • ctags: 13,095
  • sloc: cpp: 95,511; sh: 9,030; perl: 2,786; ansic: 2,425; makefile: 931; lex: 302; xml: 287; yacc: 167
file content (133 lines) | stat: -rw-r--r-- 3,751 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/***************************************************************************
                          kfixedsizebuffer.cpp  -  description
                             -------------------
    begin                : Mit Jun 03 2003
    copyright            : (C) 2003 by Friedrich W. H. Kossebau
    email                : Friedrich.W.H@Kossebau.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This library is free software; you can redistribute it and/or         *
 *   modify it under the terms of the GNU Library General Public           *
 *   License version 2 as published by the Free Software Foundation.       *
 *                                                                         *
 ***************************************************************************/


// c specific
#include <string.h>
// lib specific
#include "kfixedsizebuffer.h"

using namespace KHE;

KFixedSizeBuffer::KFixedSizeBuffer( char *D, int S, char FUC )
 : Data( D ),
   Size( S ),
   FillUpChar( FUC ),
   ReadOnly( true ),
   Modified( false )
{
}

KFixedSizeBuffer::~KFixedSizeBuffer()
{
}



int KFixedSizeBuffer::insert( int Pos, const char* D, int InputLength )
{
  // check all parameters
  if( Pos >= Size || InputLength == 0 )
    return 0;
  if( Pos + InputLength > Size )
    InputLength = Size - Pos;

  int BehindInsertPos = Pos + InputLength;
  // move right data behind the input range
  move( BehindInsertPos, Pos, Size-BehindInsertPos );
  // insert input
  copy( Pos, D, InputLength );

  Modified = true;
  return InputLength;
}


int KFixedSizeBuffer::remove( KSection Remove )
{
  if( Remove.start() >= Size || Remove.width() == 0 )
    return 0;

  Remove.restrictEndTo( Size-1 );

  int RemoveLength = Remove.width();
  int BehindRemovePos = Remove.end()+1;;
  // move right data behind the input range
  move( Remove.start(), BehindRemovePos, Size-BehindRemovePos );
  // clear freed space
  reset( Size-RemoveLength, RemoveLength );

  Modified = true;
  return RemoveLength;
}


int KFixedSizeBuffer::replace( KSection Remove, const char* D, int InputLength )
{
  // check all parameters
  if( Remove.startsBehind( Size-1 ) || (Remove.width()==0 && InputLength==0) )
    return 0;

  Remove.restrictEndTo( Size-1 );
  if( Remove.start() + InputLength > Size )
    InputLength = Size - Remove.start();

  int SizeDiff = InputLength - Remove.width();

  // is input longer than removed?
  if( SizeDiff > 0 )
  {
    int BehindInsertPos = Remove.start() + InputLength;
    // move right data behind the input range
    move( BehindInsertPos, Remove.end()+1, Size-BehindInsertPos );
  }
  // is input smaller than removed?
  else if( SizeDiff < 0 )
  {
    int BehindRemovePos = Remove.end()+1;
    // move right data behind the input range
    move( Remove.start()+InputLength, BehindRemovePos, Size-BehindRemovePos );
    // clear freed space
    reset( Size-SizeDiff, SizeDiff );
  }
  // insert input
  copy( Remove.start(), D, InputLength );

  Modified = true;
  return InputLength;
}


void KFixedSizeBuffer::move( int DestPos, int SourcePos, int Length )
{
  memmove( &Data[DestPos], &Data[SourcePos], Length );
}


void KFixedSizeBuffer::reset( int Pos, int Length )
{
  memset( &Data[Pos], FillUpChar, Length );
}


void KFixedSizeBuffer::copy( int DestPos, const char *Source, int SourceLength )
{
  memcpy( &Data[DestPos], Source, SourceLength );
}


int KFixedSizeBuffer::find( const char*, int /*Length*/, int /*Pos*/ ) const  { return 0; }
int KFixedSizeBuffer::rfind( const char*, int /*Length*/, int /*Pos*/ ) const { return 0; }