]> git.donarmstrong.com Git - lilypond.git/blob - lily/binary-source-file.cc
2003 -> 2004
[lilypond.git] / lily / binary-source-file.cc
1 /*
2   binary-source-file.cc -- implement Binary_source_file
3
4   source file of the LilyPond music typesetter
5
6   (c) 1997--2004 Jan Nieuwenhuizen
7 */
8
9
10 #include <limits.h>             // INT_MAX
11 #include <assert.h>
12
13 #include "flower-proto.hh"
14 #include "string.hh"
15 #include "source-file.hh"
16 #include "binary-source-file.hh"
17 #include "string-convert.hh"
18
19 Binary_source_file::Binary_source_file (String& filename_string)
20   : Source_file (filename_string)
21 {
22 }
23
24 Binary_source_file::~Binary_source_file ()
25 {
26 }
27
28 String
29 Binary_source_file::error_string (char const* pos_str0) const
30 {
31   assert (this);
32   if (!in_b (pos_str0))
33     return "";
34
35   char const* begin_str0 = pos_str0 - 8 >? to_str0 ();
36   char const* end_str0 = pos_str0 + 7 <? to_str0 () + length ();
37
38   String pre_string ((Byte const*)begin_str0, pos_str0 - begin_str0);
39   pre_string = String_convert::bin2hex (pre_string);
40   for (int i = 2; i < pre_string.length (); i += 3)
41     pre_string = pre_string.left_string (i) + " " + pre_string.cut_string (i, INT_MAX);
42   String post_string ((Byte const*)pos_str0, end_str0 - pos_str0);
43   post_string = String_convert::bin2hex (post_string);
44   for (int i = 2; i < post_string.length (); i += 3)
45     post_string = post_string.left_string (i) + " " + post_string.cut_string (i, INT_MAX);
46
47   String str = pre_string
48     + to_string ('\n')
49     + to_string (' ', pre_string.length () + 1) 
50     + post_string;
51   return str;
52 }
53
54 int
55 Binary_source_file::get_line (char const* pos_str0) const
56 {
57   if (!in_b (pos_str0))
58     return 0;
59
60   return pos_str0 - to_str0 ();
61 }
62
63 U8
64 Binary_source_file::get_U8 ()
65 {
66   return * (U8*)forward_str0 (1);
67 }
68
69
70 U16
71 Binary_source_file::get_U16 ()
72 {
73   U16 b;
74
75   b = get_U8 () << 8;
76   b |= get_U8 ();
77
78   return b;
79 }
80
81 /*
82   naming is wrong. This is a UNIX-endian-32 (as opposed to xinu or ixun)
83  */
84
85 U32
86 Binary_source_file::get_U32 ()
87 {
88   U32 b;
89   
90   b = get_U8 () << 24;
91   b |= get_U8 () << 16;
92   b |= get_U8 () << 8;
93   b |= get_U8 ();
94
95   return b;
96 }
97
98