lilypond-1.1.36
[lilypond.git] / lib / binary-source-file.cc
blobe3f122d5ce760b119009acf7e73a7b9483ff4011
1 /*
2 binary-source-file.cc -- implement Binary_source_file
4 source file of the LilyPond music typesetter
6 (c) 1997--1999 Jan Nieuwenhuizen
7 */
10 #include <limits.h> // INT_MAX
11 #include <assert.h>
13 #include "proto.hh"
14 #include "plist.hh"
15 #include "string.hh"
16 #include "source-file.hh"
17 #include "binary-source-file.hh"
18 #include "string-convert.hh"
20 Binary_source_file::Binary_source_file (String& filename_str)
21 : Source_file (filename_str)
25 Binary_source_file::~Binary_source_file ()
29 String
30 Binary_source_file::error_str (char const* pos_ch_C) const
32 assert (this);
33 if (!in_b (pos_ch_C))
34 return "";
36 char const* begin_ch_C = pos_ch_C - 8 >? ch_C ();
37 char const* end_ch_C = pos_ch_C + 7 <? ch_C () + length_i ();
39 String pre_str ((Byte const*)begin_ch_C, pos_ch_C - begin_ch_C);
40 pre_str = String_convert::bin2hex_str (pre_str);
41 for (int i = 2; i < pre_str.length_i (); i += 3)
42 pre_str = pre_str.left_str (i) + " " + pre_str.cut_str (i, INT_MAX);
43 String post_str ((Byte const*)pos_ch_C, end_ch_C - pos_ch_C);
44 post_str = String_convert::bin2hex_str (post_str);
45 for (int i = 2; i < post_str.length_i (); i += 3)
46 post_str = post_str.left_str (i) + " " + post_str.cut_str (i, INT_MAX);
48 String str = pre_str
49 + to_str ('\n')
50 + to_str (' ', pre_str.length_i () + 1)
51 + post_str;
52 return str;
55 int
56 Binary_source_file::line_i (char const* pos_ch_C) const
58 if (!in_b (pos_ch_C))
59 return 0;
61 return pos_ch_C - ch_C ();
65 Binary_source_file::get_U8 ()
67 return *(U8*)forward_ch_C (1);
71 U16
72 Binary_source_file::get_U16 ()
74 U16 b;
76 b = get_U8 () << 8;
77 b |= get_U8 ();
79 return b;
83 U32
84 Binary_source_file::get_U32()
86 U32 b;
88 b = get_U8 () << 24;
89 b |= get_U8 () << 16;
90 b |= get_U8 () << 8;
91 b |= get_U8 ();
93 return b;