~ubuntu-branches/ubuntu/trusty/pdfmod/trusty

« back to all changes in this revision

Viewing changes to lib/PdfSharp/PdfSharp.Pdf.IO/ShiftStack.cs

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2010-06-18 03:44:46 UTC
  • Revision ID: james.westby@ubuntu.com-20100618034446-bogifrsscpayp361
Tags: upstream-0.8.3
ImportĀ upstreamĀ versionĀ 0.8.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region PDFsharp - A .NET library for processing PDF
 
2
//
 
3
// Authors:
 
4
//   Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
 
5
//
 
6
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
 
7
//
 
8
// http://www.pdfsharp.com
 
9
// http://sourceforge.net/projects/pdfsharp
 
10
//
 
11
// Permission is hereby granted, free of charge, to any person obtaining a
 
12
// copy of this software and associated documentation files (the "Software"),
 
13
// to deal in the Software without restriction, including without limitation
 
14
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
 
15
// and/or sell copies of the Software, and to permit persons to whom the
 
16
// Software is furnished to do so, subject to the following conditions:
 
17
//
 
18
// The above copyright notice and this permission notice shall be included
 
19
// in all copies or substantial portions of the Software.
 
20
//
 
21
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
22
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
23
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 
24
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 
25
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 
26
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 
27
// DEALINGS IN THE SOFTWARE.
 
28
#endregion
 
29
 
 
30
using System;
 
31
using System.Diagnostics;
 
32
using System.Collections;
 
33
using System.Text;
 
34
using System.IO;
 
35
using PdfSharp.Pdf;
 
36
 
 
37
namespace PdfSharp.Pdf.IO
 
38
{
 
39
  /// <summary>
 
40
  /// Represents the stack for the shift-reduce parser. It seems that it is only neede for
 
41
  /// reduction of indirect references.
 
42
  /// </summary>
 
43
  internal class ShiftStack
 
44
  {
 
45
    // TODO: make Lexer.PeekChars(20) and scan for 'R' to detect indirect references
 
46
 
 
47
    public ShiftStack()
 
48
    {
 
49
      this.items = new ArrayList();
 
50
    }
 
51
 
 
52
    public PdfItem[] ToArray(int start, int length)
 
53
    {
 
54
      PdfItem[] items = new PdfItem[length];
 
55
      for (int i = 0, j = start; i < length; i++, j++)
 
56
        items[i] = (PdfItem)this.items[j];
 
57
      return items;
 
58
    }
 
59
 
 
60
    /// <summary>
 
61
    /// Gets the stack pointer index.
 
62
    /// </summary>
 
63
    public int SP
 
64
    {
 
65
      get {return this.sp;}
 
66
    }
 
67
 
 
68
    /// <summary>
 
69
    /// Gets the value at the specifed index. Valid index is in range 0 up to sp-1.
 
70
    /// </summary>
 
71
    public PdfItem this[int index]
 
72
    {
 
73
      get 
 
74
      {
 
75
        if (index >= this.sp)
 
76
          throw new ArgumentOutOfRangeException("index", index, "Value greater than stack index.");
 
77
        return (PdfItem)this.items[index];
 
78
      }
 
79
    }
 
80
 
 
81
    /// <summary>
 
82
    /// Gets an item relative to the current stack pointer. The index must be a negative value (-1, -2, etc.).
 
83
    /// </summary>
 
84
    public PdfItem GetItem(int relativeIndex)
 
85
    {
 
86
      if (relativeIndex >= 0 || -relativeIndex > this.sp)
 
87
        throw new ArgumentOutOfRangeException("index", relativeIndex, "Value out of stack range.");
 
88
      return (PdfItem)this.items[this.sp + relativeIndex];
 
89
    }
 
90
 
 
91
    /// <summary>
 
92
    /// Gets an item relative to the current stack pointer. The index must be a negative value (-1, -2, etc.).
 
93
    /// </summary>
 
94
    public int GetInteger(int relativeIndex)
 
95
    {
 
96
      if (relativeIndex >= 0 || -relativeIndex > this.sp)
 
97
        throw new ArgumentOutOfRangeException("index", relativeIndex, "Value out of stack range.");
 
98
      return ((PdfInteger)this.items[this.sp + relativeIndex]).Value;
 
99
    }
 
100
 
 
101
    /// <summary>
 
102
    /// Pushs the specified item onto the stack.
 
103
    /// </summary>
 
104
    public void Shift(PdfItem item)
 
105
    {
 
106
      Debug.Assert(item != null);
 
107
      this.items.Add(item);
 
108
      this.sp++;
 
109
    }
 
110
 
 
111
    /// <summary>
 
112
    /// Replaces the last 'count' items with the specified item.
 
113
    /// </summary>
 
114
    public void Reduce(int count)
 
115
    {
 
116
      if (count > this.sp)
 
117
        throw new ArgumentException("count causes stack underflow.");
 
118
      this.items.RemoveRange(this.sp - count, count);
 
119
      this.sp -= count;
 
120
    }
 
121
 
 
122
    /// <summary>
 
123
    /// Replaces the last 'count' items with the specified item.
 
124
    /// </summary>
 
125
    public void Reduce(PdfItem item, int count)
 
126
    {
 
127
      Debug.Assert(item != null);
 
128
      Reduce(count);
 
129
      this.items.Add(item);
 
130
      this.sp++;
 
131
    }
 
132
 
 
133
    /// <summary>
 
134
    /// The stack pointer index. Points to the next free item.
 
135
    /// </summary>
 
136
    int sp;
 
137
 
 
138
    /// <summary>
 
139
    /// An array representing the stack.
 
140
    /// </summary>
 
141
    ArrayList items;
 
142
  }
 
143
}