+2005-06-08 Han-Wen Nienhuys <hanwen@xs4all.nl>
+
+ * ttftool/util.c (syserror): use errno for better error reporting.
+
+ * lily/source-file.cc (get_counts): new function. Calc column,
+ line and char count in one go.
+
+ * lily/binary-source-file.cc (quote_input): rename to quote_input
+
+ * lily/input.cc (set): new function.
+
2005-06-08 Jan Nieuwenhuizen <janneke@gnu.org>
* lily/main.cc (prepend_env_path): Bugfix: Check directory
}
String
-Binary_source_file::error_string (char const *pos_str0) const
+Binary_source_file::quote_input (char const *pos_str0) const
{
assert (this);
if (!contains (pos_str0))
Byte get_Byte () {return get_U8 (); }
int get_int () { return get_U32 (); }
- virtual String error_string (char const *pos_str0) const;
+ virtual String quote_input (char const *pos_str0) const;
virtual int get_line (char const *pos_str0) const;
};
*/
class Input
{
-public:
char const *start_;
char const *end_;
Source_file *source_file_;
+public:
+ Source_file *get_source_file () const;
+ char const *start () const;
+ char const *end () const;
+
+ void set (Source_file *, char const *, char const *);
void warning (String) const; // should use member func?
void non_fatal_error (String) const;
void error (String) const;
Input spot () const;
String location_string () const;
String line_number_string () const;
-
String file_string ()const;
+
int line_number ()const;
int column_number ()const;
int end_line_number ()const;
int end_column_number ()const;
+ void get_counts (int*line, int *char_count, int *col) const;
+
Input (Input const &i);
Input ();
};
virtual ~Source_file ();
char const *to_str0 () const;
- virtual String error_string (char const *pos_str0) const;
+ virtual String quote_input (char const *pos_str0) const;
std::istream *get_istream ();
bool contains (char const *pos_str0) const;
int length () const;
public:
Slice line_slice (char const *pos_str0) const;
String line_string (char const *pos_str0) const;
- int get_column (char const *pos_str0) const;
- int get_char_of_line (char const *pos_str0) const;
+ void get_counts (char const *pos_str0, int*, int*, int*) const;
/*
JUNKME.
return SCM_UNSPECIFIED;
}
-LY_DEFINE (ly_input_file_line_column, "ly:input-file-line-column", 1, 0, 0, (SCM sip),
- "Return input location in @var{sip} as (file-name line column).")
+LY_DEFINE (ly_input_file_line_column, "ly:input-file-line-char-column", 1, 0, 0, (SCM sip),
+ "Return input location in @var{sip} as (file-name line char column).")
{
Input *ip = unsmob_input (sip);
SCM_ASSERT_TYPE (ip, sip, SCM_ARG1, __FUNCTION__, "input location");
- return scm_list_3 (scm_makfrom0str (ip->file_string ().to_str0 ()),
- scm_int2num (ip->line_number ()),
- scm_int2num (ip->column_number ()));
+
+ int l, ch, col;
+ ip->get_counts (&l, &ch, &col);
+ return scm_list_4 (scm_makfrom0str (ip->file_string ().to_str0 ()),
+ scm_int2num (l),
+ scm_int2num (ch),
+ scm_int2num (col));
}
LY_DEFINE (ly_input_both_locations, "ly:input-both-locations", 1, 0, 0, (SCM sip),
{
if (source_file_)
s = location_string () + ": " + s + "\n"
- + source_file_->error_string (start_);
+ + source_file_->quote_input (start_);
::message (s);
}
int
Input::column_number () const
{
- if (source_file_)
- return source_file_->get_column (start_);
- return 0;
+ int line, chr, col = 0;
+ source_file_->get_counts (start_, &line, &chr, &col);
+
+ return col;
}
int
int
Input::end_column_number () const
{
- if (source_file_)
- return source_file_->get_column (end_);
- return 0;
+ int line, chr, col = 0;
+ source_file_->get_counts (end_, &line, &chr, &col);
+
+ return col;
+}
+
+void
+Input::get_counts (int *line, int *chr, int*col) const
+{
+ source_file_->get_counts (start_, line, chr, col);
+}
+
+void
+Input::set (Source_file *sf, char const *start, char const *end)
+{
+ source_file_ = sf;
+ start_ = start;
+ end_ = end;
+}
+
+Source_file*
+Input::get_source_file () const
+{
+ return source_file_;
+}
+
+
+char const *
+Input::start () const
+{
+ return start_;
+}
+
+char const *
+Input::end () const
+{
+ return end_;
}
s = s.left_string (s.index_last ('\"'));
yy_pop_state ();
- this->here_input().source_file_->name_ = s;
+ this->here_input().get_source_file ()->name_ = s;
message (_f ("Renaming input to: `%s'", s.to_str0 ()));
progress_indication ("\n");
scm_module_define (scm_car (scopes_),
int n = 0;
Input hi = here_input();
hi.step_forward ();
- SCM sval = ly_parse_scm (hi.start_, &n, hi,
+ SCM sval = ly_parse_scm (hi.start (), &n, hi,
be_safe_global && is_main_input_);
if (sval == SCM_UNDEFINED)
void
Lily_lexer::add_lexed_char (int count)
{
- lexloc->source_file_ = get_source_file ();
- lexloc->start_ = here_str0 ();
- lexloc->end_ = lexloc->start_ + count;
+ char const * start = here_str0 ();
+ lexloc->set (get_source_file (),
+ start, start + count);
char_count_stack_.top () += count;
}
SCM
internal_ly_parse_scm (Parse_start *ps)
{
- Source_file *sf = ps->start_location_.source_file_;
+ Source_file *sf = ps->start_location_.get_source_file ();
SCM port = sf->get_port ();
- int off = ps->start_location_.start_ - sf->to_str0 ();
+ int off = ps->start_location_.start () - sf->to_str0 ();
scm_seek (port, scm_long2num (off), scm_long2num (SEEK_SET));
SCM from = scm_ftell (port);
if (!to_str0 ())
return " (" + _ ("position unknown") + ")";
else
- return name_string () + ":" + to_string (get_line (context_str0))
- + ":" + to_string (get_column (context_str0));
+ {
+ int l, ch, col;
+ get_counts (context_str0, &l, &ch, &col);
+
+ return name_string () + ":" + to_string (l)
+ + ":" + to_string (col);
+ }
+}
+
+
+
+String
+Source_file::quote_input (char const* pos_str0) const
+{
+ if (!contains (pos_str0))
+ return " (" + _ ("position unknown") + ")";
+
+ int l, ch, col;
+ get_counts (pos_str0, &l, &ch, &col);
+ String line = line_string (pos_str0);
+ String context = line.left_string (ch)
+ + to_string ('\n')
+ + to_string (' ', col)
+ + line.cut_string (ch, INT_MAX);
+ return context;
}
String
return String ((Byte const *)data_str0 + line[LEFT], line.length ());
}
-int
-Source_file::get_char_of_line (char const *pos_str0) const
-{
- if (!contains (pos_str0))
- return 0;
-
- char const *data_str0 = to_str0 ();
- return pos_str0 - (line_slice (pos_str0)[SMALLER] + data_str0);
-}
-int
-Source_file::get_column (char const *pos_str0) const
+void
+Source_file::get_counts (char const *pos_str0,
+ int *line_number,
+ int *line_char,
+ int *column) const
{
if (!contains (pos_str0))
- return 0;
+ return;
+ *line_number = get_line (pos_str0);
+
Slice line = line_slice (pos_str0);
char const *data = to_str0 ();
Byte const *line_start = (Byte const *)data + line[LEFT];
String line_begin (line_start, left);
char const *line_chars = line_begin.to_str0();
- int column = 0;
+ *column = 0;
+ *line_char = 0;
+
mbstate_t state;
/* Initialize the state. */
thislen = 1;
if (thislen == 1 && line_chars[0] == '\t')
- column = (column / 8 + 1) * 8;
+ (*column) = (*column / 8 + 1) * 8;
else
- column ++;
-
+ (*column) ++;
+
+ (*line_char) ++;
/* Advance past this character. */
line_chars += thislen;
left -= thislen;
}
- return column;
-}
-
-String
-Source_file::error_string (char const* pos_str0) const
-{
- if (!contains (pos_str0))
- return " (" + _ ("position unknown") + ")";
-
- int ch_i = get_char_of_line (pos_str0);
- String line = line_string (pos_str0);
- String context = line.left_string (ch_i)
- + to_string ('\n')
- + to_string (' ', get_column (pos_str0))
- + line.cut_string (ch_i, INT_MAX);
- return context;
}
-
bool
Source_file::contains (char const* pos_str0) const
{
if (contains (pos_str0))
pos_str0_ = pos_str0;
else
- error (error_string (pos_str0) + "invalid pos");
+ error (quote_input (pos_str0) + "invalid pos");
}
char const *
if (contains (new_str0))
pos_str0_ = new_str0;
else
- error (error_string (new_str0) + "seek past eof");
+ error (quote_input (new_str0) + "seek past eof");
return pos_str0_;
}
if (contains (new_str0))
pos_str0_ = new_str0;
else
- error (error_string (new_str0) + "forward past eof");
+ error (quote_input (new_str0) + "forward past eof");
return old_pos;
}
(ly:music-property cause 'origin))))
(if (not (ly:input-location? music-origin))
""
- (let* ((location (ly:input-file-line-column music-origin))
+ (let* ((location (ly:input-file-line-char-column music-origin))
(raw-file (car location))
(file (if (is-absolute? raw-file)
raw-file
(if (and (< 0 (interval-length x-ext))
(< 0 (interval-length y-ext)))
- (format "~a ~a ~a ~a (textedit://~a:~a:~a) mark_URI\n"
+ (format "~a ~a ~a ~a (textedit://~a:~a:~a:~a) mark_URI\n"
(+ (car offset) (car x-ext))
(+ (cdr offset) (car y-ext))
(+ (car offset) (cdr x-ext))
(+ (cdr offset) (cdr y-ext))
file
(cadr location)
- (caddr location))
+ (caddr location)
+ (cadddr location))
"")))))
(define (lily-def key val)
ly:grob-system
ly:grob-translate-axis!
ly:grob?
- ly:input-file-line-column
+ ly:input-file-line-char-column
ly:input-location?
ly:input-message
ly:intlog2
void *mymalloc (size_t size);
void *mycalloc (size_t nelem, size_t elsize);
void *myrealloc (void *ptr, size_t size);
-void error (char *string);
+void ttf_error (char *string);
void syserror (char *string);
ssize_t surely_read (int fildes, void *buf, size_t nbyte);
char *unistrncpy (char *dst, char *str, size_t length);
void create_type42 (char const *, void *);
-
#ifdef __cplusplus
}
#endif
surely_read (fd, &format, sizeof (USHORT));
FIX_UH (format);
if (format != 0)
- error ("Bad TTF file. Format should be 0\n");
+ ttf_error ("Format should be 0\n");
+
surely_read (fd, &nrecords, sizeof (USHORT));
FIX_UH (nrecords);
surely_read (fd, &offset, sizeof (USHORT));
ht->fontRevision.mantissa, ht->fontRevision.fraction);
}
if (ht->magicNumber != 0x5F0F3CF5)
- error ("Bad magic number in TTF file");
+ ttf_error ("Bad magic number");
if (verbosity >= 2)
fprintf (stderr, " %d units per Em\n", ht->unitsPerEm);
}
return 1; /* MacGlyphEncoding */
case 2:
if (pt->formatType.fraction != 0)
- error ("Unsupported `post' table format");
+ ttf_error ("Unsupported `post' table format");
surely_read (fd, &nglyphspost, sizeof (USHORT));
FIX_UH (nglyphspost);
if (nglyphspost != nglyphs)
- error ("Inconsistency between `maxp' and `nglyphs' tables!");
+ ttf_error ("Inconsistency between `maxp' and `nglyphs' tables!");
if (verbosity >= 2)
fprintf (stderr, " %d glyphs\n", nglyphs);
glyphNameIndex = mymalloc (sizeof (USHORT) * nglyphs);
return offsets;
}
/*NOTREACHED*/ default:
- error ("Unknown `loca' table format");
+ ttf_error ("Unknown `loca' table format");
/*NOTREACHED*/}
/*NOTREACHED*/}
fprintf (stderr, " version %d.%u\n",
hhea->version.mantissa, hhea->version.fraction);
if (hhea->metricDataFormat != 0)
- error ("Unknown metric data format");
+ ttf_error ("Unknown metric data format");
return hhea;
}
}
}
if (maxpOff == 0 || headOff == 0 || postOff == 0 || nameOff == 0)
- error ("Incomplete TTF file\n");
+ ttf_error ("Incomplete TTF file\n");
if (verbosity >= 1)
fprintf (stderr, "Processing `maxp' table\n");
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <errno.h>
+
#include "types.h"
#include "proto.h"
-#define ALIAS_FILE_TO_FILECOOKIE
-
#include "libc-extension.hh"
+
+
void *
mymalloc (size_t size)
{
void *p;
if ((p = malloc (size)) == NULL)
- error ("Unable to allocate memory\n");
+ ttf_error ("Unable to allocate memory\n");
return p;
}
{
void *p;
if ((p = calloc (nelem, elsize)) == NULL)
- error ("Unable to allocate memory\n");
+ ttf_error ("Unable to allocate memory\n");
return p;
}
+void
+ttf_error (char *string)
+{
+ fprintf (stderr, "TTF tool: %s\n", string);
+ exit (3);
+ /*NOTREACHED*/
+}
+
+void
+syserror (char *string)
+{
+ char *sys_err = strerror (errno);
+ fprintf (stderr, "TTF tool: %s (%s)\n",
+ string,
+ sys_err);
+ exit (3);
+}
+
void *
myrealloc (void *ptr, size_t size)
{
void *p;
if ((p = realloc (ptr, size)) == NULL)
- error ("Unable to allocate memory\n");
+ ttf_error ("Unable to allocate memory\n");
return p;
}
{
off_t result;
if ((result = lseek (fildes, offset, whence)) < 0)
- error ("Bad TTF file. Cannot seek");
+ syserror ("Cannot seek");
return result;
}
-void
-error (char *string)
-{
- fprintf (stderr, "%s\n", string);
- exit (3);
- /*NOTREACHED*/}
-
-void
-syserror (char *string)
-{
- perror (string);
- exit (3);
- /*NOTREACHED*/}
-
ssize_t
surely_read (int fildes, void *buf, size_t nbyte)
{
ssize_t n;
if ((n = read (fildes, buf, nbyte)) < nbyte)
- error ("Bad TTF file. Read too little bytes in surely_read()");
+ {
+ syserror ("read too little in surely_read()");
+ }
return n;
}