ALDT (Arnulf's LaTeX Documentation Tool)

Check-in [1fac9a39dd]
Login

Check-in [1fac9a39dd]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:initial version
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 1fac9a39ddc1f1d16596832b426f18997fa7f1e2
User & Date: arnulf 2012-08-26 12:57:24.499
Context
2012-08-26
13:18
fixes and new code check-in: ed313ac4cb user: arnulf tags: trunk
12:57
initial version check-in: 1fac9a39dd user: arnulf tags: trunk
11:46
initil version check-in: 56555330e2 user: arnulf tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Added StringObjType.tcl.




































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#=====================================================
# StringObjType.tcl 
# *
# * ALDT (Arnulf's Latex Documentation Tool)
# * A Tcl parser for LaTeX files for generating document output (for example html)
# *
# * Store info of a string LatexObj
# *
# * Released under same BSD license as Tcl.
# * (Tcl BSD license found at <http://www.tcl.tk/software/tcltk/license.html>)
# *
# * Copyright 2012 Arnulf P. Wiedemann
#=====================================================

namespace eval ::Parser {

::itcl::class StringObjType {
  public common oid 0

  private variable id

  private variable default_trim_chars " \t"

  constructor {} {}
  public method mySelf {}
  public method toString {}
  public method toDebugString {}
  public method setFromAny {obj_ptr}
  public method dupInternalRep {src_ptr dup_ptr}
  public method getString {obj_ptr}
  public method getStringLength {obj_ptr}
  public method newStringObjNoAlloc {str len where}
  public method newStringObj {str len where}
  public method newEmptyStringObj {where}
  public method _appendString {obj_ptr str len}
  public method appendString {obj_ptr str len}
  public method stringToWide {str wide_ptr}
  public method stringToDouble {str double_ptr}
  public method appendObj {obj_ptr append_obj_ptr}
}

  # ==================== constructor ==================================
::itcl::body StringObjType::constructor {} {
    incr oid
    set id $oid
  }

  # ==================== mySelf ==================================
::itcl::body StringObjType::mySelf {} {
    String str = "StringObjType!"+id+"!";
    return str;
  }

  # ==================== toString =====================================
::itcl::body StringObjType::toString {} {
    return "[mySelf]!"
  }

  # ==================== toDebugString =====================================
::itcl::body StringObjType::toDebugString {} {
    set str "[mySelf]\n"
    return $str
  }

  # ==================== setFromAny ==================================
::itcl::body StringObjType::setFromAny {obj_ptr} {
    # Get a fresh string representation. 
    obj_ptr.getString();
    /* Free any other internal representation. */
    obj_ptr.freeIntRep();
    /* Set it as string, i.e. just set the maxLength field. */
    obj_ptr.obj_type = OBJ_TYPE_STRING;
    obj_ptr.strValue_SetMaxLength(obj_ptr.len);
    /* Don't know the utf-8 length yet */
    obj_ptr.strValue_SetCharLength(-1);
    return OK;
  }

  # ==================== dupInternalRep ===================================== 
::itcl::body StringObjType::dupInternalRep {src_ptr dup_ptr} {
#    throw "string dupInternalRep not yet implemented";
    return ERROR;
  }

  # ==================== getString ==================================
  # Return the string representation for obj_ptr. If the object
  # string representation is invalid, calls the method to create
  # a new one starting from the internal representation of the object.

::itcl::body StringObjType::getString {obj_ptr} {
    return [$obj_ptr getString]
  }

  # ==================== getStringLength ==================================
::itcl::body StringObjType::getStringLength {obj_ptr} {
    return [$obj_ptr getLen]
  }

  # ==================== newStringObjNoAlloc ================================== 
::itcl::body StringObjType::newStringObjNoAlloc {str len where} {
    set obj_ptr [::Parser::LatexObj #auto]
    if {$len == -1} {
      set len [string length $str]
    }
    $obj_ptr setStringRep $obj_ptr $str $len
    $obj_ptr setObjType OBJ_TYPE_STRING
    $obj_ptr setAllocated $where
    $obj_ptr setFreed ""
    return $obj_ptr;
  }

  # ==================== newStringObj ================================== 
  # len is in bytes -- see also newStringObjUtf8() 
::itcl::body StringObjType::newStringObj {str len where} {
    set obj_ptr [::Parser::LatexObj #auto]

    # Need to find out how many bytes the string requires 
    if {$len == -1} {
      set len [string length $str]
    }
    $obj_ptr strValue_SetCharLength $len
    $obj_ptr strValue_SetMaxLength $len
    if {len == 0} {
      $obj_ptr setLen 0
      $obj_ptr setBbytes ""
    } else {
      $obj_ptr setBytes ""
      $obj_ptr setBytes "[$obj_ptr getBytes][string range $str 0 $len]"
      $obj_ptr setLen $len
    }

    # No obj_type field for the vanilla string object. 
    $obj_ptr setObjType OBJ_TYPE_STRING
    $obj_ptr setAllocated $where
    $obj_ptr setFreed ""
    return $obj_ptr;
  }

  # ==================== newEmptyStringObj ==================================
::itcl::body StringObjType::newEmptyStringObj {where} {
    set obj_ptr [newStringObj "" 0 "STRING_OBJ_x"]
    $obj_ptr setAllocated $where
    $obj_ptr setFreed ""
    return $obj_ptr;
  }

  # ==================== _appendString ==================================
  # Low-level string append. Use it only against objects
  # of type "string". 

::itcl::body StringObjType::_appendString {obj_ptr str len} {
    set need_len -1

    if {$len == -1} {
      set len [string length $str]
    }
    set need_len [expr {[$obj_ptr getLen] + $len}]
    # next line: force string respresentation 
    $obj_ptr getString
    if {[$obj_ptr strValue_GetMaxLength] < $need_len || [$obj_ptr strValue_GetMaxLength] == 0} {
      set need_len [expr {$need_len * 2}]
      # Inefficient to malloc() for less than 8 bytes 
      if {$need_len < 7} {
        set need_len 7
      }
      $obj_ptr strValue_SetMaxLength $need_len
    }
# FIXME this is just a hack!!
#if (obj_ptr.len < obj_ptr.bytes.length()) {
#obj_ptr.bytes.delete(obj_ptr.len, obj_ptr.bytes.length());
#}
    $obj_ptr setBytes "[$obj_ptr getBytes]$str"
    if {[$obj_ptr strValue_GetCharLength] >= 0} {
        # Update the utf-8 char length 
        $obj_ptr strValue_SetCharLength [expr {[$obj_ptr strValue_GetCharLength] + [string length [$obj_ptr getBytes]]}]
    }
    $obj_ptr setLen [expr {[$obj_ptr getLen] + $len}]
    return true
  }

  # ==================== appendString ==================================
  # Higher level API to append strings to objects.
::itcl::body StringObjType::appendString {obj_ptr str len} {
    panic [$obj_ptr isShared] "appendString called with shared object"
    if {[$obj_ptr getObjType] != OBJ_TYPE_STRING} {
      setFromAny $obj_ptr
    }
    _appendString $obj_ptr $str $len
    return true
  }

  # ==================== stringToWide ================================== 
::itcl::body StringObjType::stringToWide {str wide_ptr} {
    set val ""

    try {
      val = Long.parseLong(str);
    } catch(NumberFormatException e) {
      return ERROR;
    }
    wide_ptr.add(val);
    return true
  }

  # ==================== stringToDouble ==================================
::itcl::body StringObjType::stringToDouble {str double_ptr} {
    set val ""

    try {
      val = Double.parseDouble(str);
    } catch(NumberFormatException e) {
      return ERROR;
    } 
    double_ptr.add(val);
    return true
  }

  # ==================== appendObj ================================== 
::itcl::body StringObjType::appendObj {obj_ptr append_obj_ptr} {
    set str [$append_obj_ptr getString]
    set len [$append_obj_ptr getStringLength]
    appendString $obj_ptr $str $len
    return true
  }

}