1
#region PDFsharp - A .NET library for processing PDF
4
// Stefan Lange (mailto:Stefan.Lange@pdfsharp.com)
6
// Copyright (c) 2005-2008 empira Software GmbH, Cologne (Germany)
8
// http://www.pdfsharp.com
9
// http://sourceforge.net/projects/pdfsharp
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:
18
// The above copyright notice and this permission notice shall be included
19
// in all copies or substantial portions of the Software.
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.
31
using System.Diagnostics;
32
using System.Collections;
37
namespace PdfSharp.Pdf.IO
40
/// Represents the stack for the shift-reduce parser. It seems that it is only neede for
41
/// reduction of indirect references.
43
internal class ShiftStack
45
// TODO: make Lexer.PeekChars(20) and scan for 'R' to detect indirect references
49
this.items = new ArrayList();
52
public PdfItem[] ToArray(int start, int length)
54
PdfItem[] items = new PdfItem[length];
55
for (int i = 0, j = start; i < length; i++, j++)
56
items[i] = (PdfItem)this.items[j];
61
/// Gets the stack pointer index.
69
/// Gets the value at the specifed index. Valid index is in range 0 up to sp-1.
71
public PdfItem this[int index]
76
throw new ArgumentOutOfRangeException("index", index, "Value greater than stack index.");
77
return (PdfItem)this.items[index];
82
/// Gets an item relative to the current stack pointer. The index must be a negative value (-1, -2, etc.).
84
public PdfItem GetItem(int relativeIndex)
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];
92
/// Gets an item relative to the current stack pointer. The index must be a negative value (-1, -2, etc.).
94
public int GetInteger(int relativeIndex)
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;
102
/// Pushs the specified item onto the stack.
104
public void Shift(PdfItem item)
106
Debug.Assert(item != null);
107
this.items.Add(item);
112
/// Replaces the last 'count' items with the specified item.
114
public void Reduce(int count)
117
throw new ArgumentException("count causes stack underflow.");
118
this.items.RemoveRange(this.sp - count, count);
123
/// Replaces the last 'count' items with the specified item.
125
public void Reduce(PdfItem item, int count)
127
Debug.Assert(item != null);
129
this.items.Add(item);
134
/// The stack pointer index. Points to the next free item.
139
/// An array representing the stack.