lilypond-1.5.2
[lilypond.git] / lib / binary-source-file.cc
blobeb6cbbb0ba8408707d927d652e9ecbde136c52ae
1 /*
2 binary-source-file.cc -- implement Binary_source_file
4 source file of the LilyPond music typesetter
6 (c) 1997--2000 Jan Nieuwenhuizen
7 */
10 #include <limits.h> // INT_MAX
11 #include <assert.h>
13 #include "proto.hh"
14 #include "string.hh"
15 #include "source-file.hh"
16 #include "binary-source-file.hh"
17 #include "string-convert.hh"
19 Binary_source_file::Binary_source_file (String& filename_str)
20 : Source_file (filename_str)
24 Binary_source_file::~Binary_source_file ()
28 String
29 Binary_source_file::error_str (char const* pos_ch_C) const
31 assert (this);
32 if (!in_b (pos_ch_C))
33 return "";
35 char const* begin_ch_C = pos_ch_C - 8 >? ch_C ();
36 char const* end_ch_C = pos_ch_C + 7 <? ch_C () + length_i ();
38 String pre_str ((Byte const*)begin_ch_C, pos_ch_C - begin_ch_C);
39 pre_str = String_convert::bin2hex_str (pre_str);
40 for (int i = 2; i < pre_str.length_i (); i += 3)
41 pre_str = pre_str.left_str (i) + " " + pre_str.cut_str (i, INT_MAX);
42 String post_str ((Byte const*)pos_ch_C, end_ch_C - pos_ch_C);
43 post_str = String_convert::bin2hex_str (post_str);
44 for (int i = 2; i < post_str.length_i (); i += 3)
45 post_str = post_str.left_str (i) + " " + post_str.cut_str (i, INT_MAX);
47 String str = pre_str
48 + to_str ('\n')
49 + to_str (' ', pre_str.length_i () + 1)
50 + post_str;
51 return str;
54 int
55 Binary_source_file::line_i (char const* pos_ch_C) const
57 if (!in_b (pos_ch_C))
58 return 0;
60 return pos_ch_C - ch_C ();
64 Binary_source_file::get_U8 ()
66 return *(U8*)forward_ch_C (1);
70 U16
71 Binary_source_file::get_U16 ()
73 U16 b;
75 b = get_U8 () << 8;
76 b |= get_U8 ();
78 return b;
82 U32
83 Binary_source_file::get_U32()
85 U32 b;
87 b = get_U8 () << 24;
88 b |= get_U8 () << 16;
89 b |= get_U8 () << 8;
90 b |= get_U8 ();
92 return b;