(a2.get());
if (!a2ip)
{
sub_context_ty sc;
sc.var_set_charstar("Function", "wrap");
sc.var_set_long("Number", 2);
sc.var_set_charstar("Name", argv[1]->name());
nstring s
(
sc.subst_intl
(
i18n("$function: argument $number: integer value "
"required (was given $name)")
)
);
return rpt_value_error::create(ep->get_pos(), s);
}
long width = a2ip->query();
if (width < 1)
{
sub_context_ty sc;
sc.var_set_charstar("Function", "wrap");
sc.var_set_long("Number", 2);
sc.var_set_long("Value", width);
nstring s
(
sc.subst_intl
(
i18n("$function: argument $number: width $value out of range")
)
);
return rpt_value_error::create(ep->get_pos(), s);
}
//
// the result is a list
// create an empty one se we can start filling it
//
rpt_value_list *p = new rpt_value_list();
rpt_value::pointer result(p);
const char *sp = subject.c_str();
while (*sp)
{
//
// find where the line ends
//
const char *end_p = sp;
while (end_p - sp < width && *end_p && *end_p != '\n')
++end_p;
if (*end_p && *end_p != '\n')
{
//
// see if there is a better place to wrap
//
const char *w = end_p;
while (w > sp && !isspace((unsigned char)(w[-1])))
--w;
if (w > sp + 1)
end_p = w - 1;
else
{
w = end_p;
while (w > sp && !ispunct((unsigned char)(w[-1])))
--w;
if (w > sp + 1)
end_p = w - 1;
}
}
//
// append the line to the result
//
nstring os(sp, end_p - sp);
rpt_value::pointer tmp = rpt_value_string::create(os);
p->append(tmp);
//
// skip line terminator and spaces
//
// Insert HTML paragraph breaks or line breaks
// if appropriate.
//
sp = end_p;
while (*sp && *sp != '\n' && isspace((unsigned char)*sp))
++sp;
if (*sp == '\n')
{
++sp;
if (*sp && isspace((unsigned char)*sp))
{
os = nstring("");
tmp = rpt_value_string::create(os);
p->append(tmp);
}
else
{
os = nstring("
");
tmp = rpt_value_string::create(os);
p->append(tmp);
}
}
while (*sp && isspace((unsigned char)*sp))
++sp;
}
return result;
}