]> err.no Git - sope/blob - sope-xml/STXSaxDriver/ExtraSTX/StructuredLine.m
fixed copyrights for 2005
[sope] / sope-xml / STXSaxDriver / ExtraSTX / StructuredLine.m
1 /*
2   Copyright (C) 2004 eXtrapola Srl
3
4   This file is part of SOPE.
5
6   SOPE is free software; you can redistribute it and/or modify it under
7   the terms of the GNU Lesser General Public License as published by the
8   Free Software Foundation; either version 2, or (at your option) any
9   later version.
10
11   SOPE is distributed in the hope that it will be useful, but WITHOUT ANY
12   WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
14   License for more details.
15
16   You should have received a copy of the GNU Lesser General Public
17   License along with SOPE; see the file COPYING.  If not, write to the
18   Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19   02111-1307, USA.
20 */
21
22 #import "StructuredLine.h"
23 #include "common.h"
24
25 @implementation StructuredLine
26
27 - (id)initWithString:(NSString *)aString level:(int)aLevel {
28   if ((self = [super init])) {
29     [self setText:aString];
30     level = aLevel;
31   }
32   return self;
33 }
34
35 - (void)dealloc {
36   [self->_text         release];
37   [self->_originalText release];
38   [super dealloc];
39 }
40
41 /* accessors */
42
43 - (NSString *)text {
44   NSMutableString *result;
45   NSArray *components;
46   int i, count;
47   
48   if (self->_text)
49     return self->_text;
50
51   result     = [NSMutableString stringWithCapacity:16];
52   components = [_originalText componentsSeparatedByString:@"\n"];
53   count      = [components count];
54
55   for (i = 0; i < count; i++) {
56     NSString *s;
57     
58     if (i > 0)
59       [result appendString:@" "];
60     
61     s = [components objectAtIndex:i];
62     s = [s stringByTrimmingCharactersInSet:
63              [NSCharacterSet whitespaceCharacterSet]];
64     [result appendString:s];
65   }
66   
67   self->_text = [result copy];
68   return self->_text;
69 }
70
71 - (NSString *)originalText {
72   return self->_originalText;
73 }
74
75 - (void)setText:(NSString *)aString {
76   BOOL     running = YES;
77   int      i, length;
78   NSString *ptr;
79   
80   numberOfSpaces = 0;
81   length = [aString length];
82   
83   for (i = 0; i < length; i++) {
84     switch (([aString characterAtIndex:i])) {
85     case ' ':
86     case 0x09:
87       break;
88
89     default:
90       running = NO;
91       break;
92     }
93     
94     if (!running)
95       break;
96   }
97
98   if (running)
99     return;
100   
101   numberOfSpaces = i;
102
103   ptr = _originalText;
104   self->_originalText = [aString retain];
105
106   [ptr release];
107   [self->_text release]; self->_text = nil;
108 }
109
110 - (void)setLevel:(int)aLevel {
111   level = aLevel;
112 }
113 - (int)level {
114   return level;
115 }
116
117 - (int)numberOfSpacesAtBeginning {
118   return numberOfSpaces;
119 }
120
121 @end /* StructuredLine */