3709192002-03-20Martin Nilsson #pike __REAL_VERSION__
35eee72001-01-08David Norlin // This module contains utility functions for XML creation and // some other useful stuff common to all the modules.
d47c322001-03-06David Norlin #include "./debug.h"
456f4b2001-01-08Henrik Grubbström (Grubba) static constant DOC_COMMENT = "//!";
35eee72001-01-08David Norlin  static int isDigit(int c) { return '0' <= c && c <= '9'; } static int isDocComment(string s) {
456f4b2001-01-08Henrik Grubbström (Grubba)  return has_prefix(s, DOC_COMMENT);
35eee72001-01-08David Norlin } //FIXME: should it return 0 for keywords ?? static int isIdent(string s) { if (!s || s == "") return 0; int first = s[0]; switch(s[0]) { case 'a'..'z': case 'A'..'Z': case '_': case '`': break; default: if (s[0] < 128) return 0; } return 1; } static string xmlquote(string s) { return replace(s, ({ "<", ">", "&" }), ({ "&lt;", "&gt;", "&amp;" })); } static string attributequote(string s) { return replace(s, ({ "<", ">", "\"", "'", "&" }), ({ "&lt;", "&gt;", "&#34;", "&#39;", "&amp;" })); }
d5569c2001-05-01Johan Sundström static string writeattributes(mapping(string:string) attrs) {
35eee72001-01-08David Norlin  string s = "";
d5569c2001-05-01Johan Sundström  foreach(sort(indices(attrs || ([]))), string attr)
35eee72001-01-08David Norlin  s += sprintf(" %s='%s'", attr, attributequote(attrs[attr])); return s; } static string opentag(string t, mapping(string:string)|void attributes) { return "<" + t + writeattributes(attributes) + ">"; } static string closetag(string t) { return "</" + t + ">"; } static string xmltag(string t, string|mapping(string:string)|void arg1, string|void arg2) { mapping attributes = mappingp(arg1) ? arg1 : 0; string content = stringp(arg1) ? arg1 : stringp(arg2) ? arg2 : 0; if (content && content != "") return opentag(t, attributes) + content + closetag(t);
d47c322001-03-06David Norlin  string s = "<" + t + writeattributes(attributes) + "/>"; return s;
35eee72001-01-08David Norlin }
dd2d3b2001-01-09David Norlin  class SourcePosition { string filename; int firstline; int lastline;
d47c322001-03-06David Norlin  static void create(string filename, int firstline,
dd2d3b2001-01-09David Norlin  int|void lastline) {
d47c322001-03-06David Norlin  if (!firstline) { werror("**********************************************************\n"); werror("* NO FIRST LINE !!!!! \n"); werror("**********************************************************\n"); werror("%s", describe_backtrace(backtrace())); werror("**********************************************************\n"); werror("**********************************************************\n"); }
7bffb72002-09-21Martin Stjernholm  this_program::filename = filename; this_program::firstline = firstline; this_program::lastline = lastline;
dd2d3b2001-01-09David Norlin  }
d47c322001-03-06David Norlin  SourcePosition copy() { return SourcePosition(filename, firstline, lastline); }
dd2d3b2001-01-09David Norlin  string _sprintf() { string res = "SourcePosition(File: " + (filename ? filename : "?"); if (firstline) if (lastline) res += sprintf(", lines: %d..%d", firstline, lastline); else res += sprintf(", line: %d", firstline); return res + ")"; }
d47c322001-03-06David Norlin  string xml() { mapping(string:string) m = ([]); m["file"] = filename || "?"; if (firstline) m["first-line"] = (string) firstline; if (lastline) m["last-line"] = (string) lastline; return xmltag("source-position", m); }
dd2d3b2001-01-09David Norlin }
d47c322001-03-06David Norlin class AutoDocError { SourcePosition position; string part; // which part of the autodoc system... string message; static void create(SourcePosition _position, string _part, string _message) { position = _position; part = _part; message = _message; } string _sprintf() { return sprintf("AutoDocError(%O, %O, %O)", position, part, message); } }