summaryrefslogtreecommitdiff
path: root/src/main/escp2-papers.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/escp2-papers.c')
-rw-r--r--src/main/escp2-papers.c276
1 files changed, 108 insertions, 168 deletions
diff --git a/src/main/escp2-papers.c b/src/main/escp2-papers.c
index 5eebc49..19d1b98 100644
--- a/src/main/escp2-papers.c
+++ b/src/main/escp2-papers.c
@@ -16,8 +16,7 @@
* for more details.
*
* You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
@@ -32,42 +31,23 @@
static stp_mxml_node_t *
get_media_size_xml(const stp_vars_t *v)
{
- stpi_escp2_printer_t *printdef = stp_escp2_get_printer(v);
+ stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v);
return printdef->media_sizes;
}
int
-stp_escp2_load_media_sizes(const stp_vars_t *v, const char *name)
+stpi_escp2_load_media_sizes(const stp_vars_t *v, const char *name)
{
- stpi_escp2_printer_t *printdef = stp_escp2_get_printer(v);
- stp_list_t *dirlist = stpi_data_path();
- stp_list_item_t *item;
- int found = 0;
- item = stp_list_get_start(dirlist);
- while (item)
- {
- const char *dn = (const char *) stp_list_item_get_data(item);
- char *ffn = stpi_path_merge(dn, name);
- stp_mxml_node_t *sizes =
- stp_mxmlLoadFromFile(NULL, ffn, STP_MXML_NO_CALLBACK);
- stp_free(ffn);
- if (sizes)
- {
- stp_mxml_node_t **xnode =
- (stp_mxml_node_t **) &(printdef->media_sizes);
- *xnode = sizes;
- found = 1;
- break;
- }
- item = stp_list_item_next(item);
- }
- stp_list_destroy(dirlist);
- STPI_ASSERT(found, v);
- return found;
+ stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v);
+ stp_mxml_node_t *node =
+ stp_xml_parse_file_from_path_safe(name, "escp2MediaSizes", NULL);
+ stp_mxml_node_t **xnode = (stp_mxml_node_t **) &(printdef->media_sizes);
+ *xnode = node;
+ return 1;
}
void
-stp_escp2_set_media_size(stp_vars_t *v, const stp_vars_t *src)
+stpi_escp2_set_media_size(stp_vars_t *v, const stp_vars_t *src)
{
const char *name = stp_get_string_parameter(src, "PageSize");
if (name)
@@ -98,74 +78,59 @@ paper_namefunc(const void *item)
}
int
-stp_escp2_load_media(const stp_vars_t *v, const char *name)
+stpi_escp2_load_media(const stp_vars_t *v, const char *name)
{
- stpi_escp2_printer_t *printdef = stp_escp2_get_printer(v);
- stp_list_t *dirlist = stpi_data_path();
- stp_list_item_t *item;
- int found = 0;
- item = stp_list_get_start(dirlist);
- while (item)
+ stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v);
+ stp_mxml_node_t *node =
+ stp_xml_parse_file_from_path_safe(name, "escp2Papers", NULL);
+ stp_mxml_node_t **xnode = (stp_mxml_node_t **) &(printdef->media);
+ stp_list_t **xcache = (stp_list_t **) &(printdef->media_cache);
+ stp_string_list_t **xpapers = (stp_string_list_t **) &(printdef->papers);
+ static const char *xcache_cache = "escp2Papers_xcache";
+ static const char *xpapers_cache = "escp2Papers_xpapers";
+ *xnode = node->parent;
+ *xcache = (stp_list_t *) stp_refcache_find_item(xcache_cache, name);
+ if (! *xcache)
{
- const char *dn = (const char *) stp_list_item_get_data(item);
- char *ffn = stpi_path_merge(dn, name);
- stp_mxml_node_t *media =
- stp_mxmlLoadFromFile(NULL, ffn, STP_MXML_NO_CALLBACK);
- stp_free(ffn);
- if (media)
+ *xcache = stp_list_create();
+ stp_list_set_namefunc(*xcache, paper_namefunc);
+ stp_refcache_add_item(xcache_cache, name, *xcache);
+ }
+ *xpapers = (stp_string_list_t *) stp_refcache_find_item(xpapers_cache, name);
+ if (! *xpapers)
+ {
+ *xpapers = stp_string_list_create();
+ stp_refcache_add_item(xpapers_cache, name, *xpapers);
+ node = node->child;
+ while (node)
{
- stp_mxml_node_t **xnode =
- (stp_mxml_node_t **) &(printdef->media);
- stp_list_t **xlist =
- (stp_list_t **) &(printdef->media_cache);
- stp_string_list_t **xpapers =
- (stp_string_list_t **) &(printdef->papers);
- stp_mxml_node_t *node = stp_mxmlFindElement(media, media,
- "escp2Papers", NULL,
- NULL, STP_MXML_DESCEND);
- *xnode = media;
- *xlist = stp_list_create();
- stp_list_set_namefunc(*xlist, paper_namefunc);
- *xpapers = stp_string_list_create();
- if (node)
- {
- node = node->child;
- while (node)
- {
- if (node->type == STP_MXML_ELEMENT &&
- strcmp(node->value.element.name, "paper") == 0)
- stp_string_list_add_string(*xpapers,
- stp_mxmlElementGetAttr(node, "name"),
- stp_mxmlElementGetAttr(node, "text"));
- node = node->next;
- }
- }
- found = 1;
- break;
+ if (node->type == STP_MXML_ELEMENT &&
+ strcmp(node->value.element.name, "paper") == 0)
+ stp_string_list_add_string(*xpapers,
+ stp_mxmlElementGetAttr(node, "name"),
+ stp_mxmlElementGetAttr(node, "text"));
+ node = node->next;
}
- item = stp_list_item_next(item);
}
- stp_list_destroy(dirlist);
- STPI_ASSERT(found, v);
- return found;
+ return 1;
}
static stp_mxml_node_t *
get_media_xml(const stp_vars_t *v)
{
- stpi_escp2_printer_t *printdef = stp_escp2_get_printer(v);
+ stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v);
return printdef->media;
}
static stp_list_t *
get_media_cache(const stp_vars_t *v)
{
- stpi_escp2_printer_t *printdef = stp_escp2_get_printer(v);
+ stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v);
return printdef->media_cache;
}
int
-stp_escp2_has_media_feature(const stp_vars_t *v, const char *name)
+stpi_escp2_has_media_feature(const stp_vars_t *v, const char *name)
{
stp_mxml_node_t *doc = get_media_xml(v);
if (doc)
@@ -246,10 +211,10 @@ get_media_type_named(const stp_vars_t *v, const char *name,
{
paper_t *answer = NULL;
int i;
- stpi_escp2_printer_t *printdef = stp_escp2_get_printer(v);
+ stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v);
const stp_string_list_t *p = printdef->papers;
- const res_t *res = ignore_res ? NULL : stp_escp2_find_resolution(v);
- const inklist_t *inklist = stp_escp2_inklist(v);
+ const res_t *res = ignore_res ? NULL : stpi_escp2_find_resolution(v);
+ const inklist_t *inklist = stpi_escp2_inklist(v);
char *media_id = build_media_id(name, inklist, res);
stp_list_t *cache = get_media_cache(v);
stp_list_item_t *li = stp_list_get_item_by_name(cache, media_id);
@@ -265,15 +230,9 @@ get_media_type_named(const stp_vars_t *v, const char *name,
{
if (!strcmp(name, stp_string_list_param(p, i)->name))
{
-#ifdef HAVE_LOCALE_H
- char *locale = stp_strdup(setlocale(LC_ALL, NULL));
- setlocale(LC_ALL, "C");
-#endif
+ stp_xml_init();
answer = build_media_type(v, name, inklist, res);
-#ifdef HAVE_LOCALE_H
- setlocale(LC_ALL, locale);
- stp_free(locale);
-#endif
+ stp_xml_exit();
break;
}
}
@@ -287,9 +246,9 @@ get_media_type_named(const stp_vars_t *v, const char *name,
}
const paper_t *
-stp_escp2_get_media_type(const stp_vars_t *v, int ignore_res)
+stpi_escp2_get_media_type(const stp_vars_t *v, int ignore_res)
{
- stpi_escp2_printer_t *printdef = stp_escp2_get_printer(v);
+ stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v);
const stp_string_list_t *p = printdef->papers;
if (p)
{
@@ -301,9 +260,9 @@ stp_escp2_get_media_type(const stp_vars_t *v, int ignore_res)
}
const paper_t *
-stp_escp2_get_default_media_type(const stp_vars_t *v)
+stpi_escp2_get_default_media_type(const stp_vars_t *v)
{
- stpi_escp2_printer_t *printdef = stp_escp2_get_printer(v);
+ stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v);
const stp_string_list_t *p = printdef->papers;
if (p)
{
@@ -323,69 +282,56 @@ slots_namefunc(const void *item)
}
int
-stp_escp2_load_input_slots(const stp_vars_t *v, const char *name)
+stpi_escp2_load_input_slots(const stp_vars_t *v, const char *name)
{
- stpi_escp2_printer_t *printdef = stp_escp2_get_printer(v);
- stp_list_t *dirlist = stpi_data_path();
- stp_list_item_t *item;
- int found = 0;
- item = stp_list_get_start(dirlist);
- while (item)
+ stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v);
+ stp_mxml_node_t *node =
+ stp_xml_parse_file_from_path_safe(name, "escp2InputSlots", NULL);
+ stp_mxml_node_t **xnode = (stp_mxml_node_t **) &(printdef->slots);
+ stp_list_t **xcache = (stp_list_t **) &(printdef->slots_cache);
+ stp_string_list_t **xslots = (stp_string_list_t **) &(printdef->input_slots);
+ static const char *xcache_cache = "escp2InputSlots_xcache";
+ static const char *xslots_cache = "escp2InputSlots_slots";
+
+ *xnode = node->parent;
+ *xcache = (stp_list_t *) stp_refcache_find_item(xcache_cache, name);
+ if (! *xcache)
+ {
+ *xcache = stp_list_create();
+ stp_list_set_namefunc(*xcache, slots_namefunc);
+ stp_refcache_add_item(xcache_cache, name, *xcache);
+ }
+
+ *xslots = (stp_string_list_t *) stp_refcache_find_item(xslots_cache, name);
+ if (! *xslots)
{
- const char *dn = (const char *) stp_list_item_get_data(item);
- char *ffn = stpi_path_merge(dn, name);
- stp_mxml_node_t *slots =
- stp_mxmlLoadFromFile(NULL, ffn, STP_MXML_NO_CALLBACK);
- stp_free(ffn);
- if (slots)
+ *xslots = stp_string_list_create();
+ stp_refcache_add_item(xslots_cache, name, *xslots);
+ node = node->child;
+ while (node)
{
- stp_mxml_node_t **xnode =
- (stp_mxml_node_t **) &(printdef->slots);
- stp_list_t **xlist =
- (stp_list_t **) &(printdef->slots_cache);
- stp_string_list_t **xslots =
- (stp_string_list_t **) &(printdef->input_slots);
- stp_mxml_node_t *node = stp_mxmlFindElement(slots, slots,
- "escp2InputSlots", NULL,
- NULL, STP_MXML_DESCEND);
- *xnode = slots;
- *xlist = stp_list_create();
- stp_list_set_namefunc(*xlist, slots_namefunc);
- *xslots = stp_string_list_create();
- if (node)
- {
- node = node->child;
- while (node)
- {
- if (node->type == STP_MXML_ELEMENT &&
- strcmp(node->value.element.name, "slot") == 0)
- stp_string_list_add_string(*xslots,
- stp_mxmlElementGetAttr(node, "name"),
- stp_mxmlElementGetAttr(node, "text"));
- node = node->next;
- }
- }
- found = 1;
- break;
+ if (node->type == STP_MXML_ELEMENT &&
+ strcmp(node->value.element.name, "slot") == 0)
+ stp_string_list_add_string(*xslots,
+ stp_mxmlElementGetAttr(node, "name"),
+ stp_mxmlElementGetAttr(node, "text"));
+ node = node->next;
}
- item = stp_list_item_next(item);
}
- stp_list_destroy(dirlist);
- STPI_ASSERT(found, v);
- return found;
+ return 1;
}
static stp_mxml_node_t *
get_slots_xml(const stp_vars_t *v)
{
- stpi_escp2_printer_t *printdef = stp_escp2_get_printer(v);
+ stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v);
return printdef->slots;
}
static stp_list_t *
get_slots_cache(const stp_vars_t *v)
{
- stpi_escp2_printer_t *printdef = stp_escp2_get_printer(v);
+ stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v);
return printdef->slots_cache;
}
@@ -403,65 +349,65 @@ build_input_slot(const stp_vars_t *v, const char *name)
answer = stp_zalloc(sizeof(input_slot_t));
answer->name = stp_mxmlElementGetAttr(node, "name");
answer->text = gettext(stp_mxmlElementGetAttr(node, "text"));
- n1 = stp_mxmlFindElement(node, node, "CD", NULL, NULL, STP_MXML_DESCEND);
+ n1 = stp_xml_get_node(node, "CD", NULL);
if (n1)
answer->is_cd = 1;
- n1 = stp_mxmlFindElement(node, node, "RollFeed", NULL, NULL, STP_MXML_DESCEND);
+ n1 = stp_xml_get_node(node, "RollFeed", NULL);
if (n1)
{
answer->is_roll_feed = 1;
- if (stp_mxmlFindElement(n1, n1, "CutAll", NULL, NULL, STP_MXML_DESCEND))
+ if (stp_xml_get_node(n1, "CutAll", NULL))
answer->roll_feed_cut_flags |= ROLL_FEED_CUT_ALL;
- if (stp_mxmlFindElement(n1, n1, "CutLast", NULL, NULL, STP_MXML_DESCEND))
+ if (stp_xml_get_node(n1, "CutLast", NULL))
answer->roll_feed_cut_flags |= ROLL_FEED_CUT_LAST;
- if (stp_mxmlFindElement(n1, n1, "DontEject", NULL, NULL, STP_MXML_DESCEND))
+ if (stp_xml_get_node(n1, "DontEject", NULL))
answer->roll_feed_cut_flags |= ROLL_FEED_DONT_EJECT;
}
- n1 = stp_mxmlFindElement(node, node, "Duplex", NULL, NULL, STP_MXML_DESCEND);
+ n1 = stp_xml_get_node(node, "Duplex", NULL);
if (n1)
{
- if (stp_mxmlFindElement(n1, n1, "Tumble", NULL, NULL, STP_MXML_DESCEND))
+ if (stp_xml_get_node(n1, "Tumble", NULL))
answer->duplex |= DUPLEX_TUMBLE;
- if (stp_mxmlFindElement(n1, n1, "NoTumble", NULL, NULL, STP_MXML_DESCEND))
+ if (stp_xml_get_node(n1, "NoTumble", NULL))
answer->duplex |= DUPLEX_NO_TUMBLE;
}
- n1 = stp_mxmlFindElement(node, node, "InitSequence", NULL, NULL, STP_MXML_DESCEND);
+ n1 = stp_xml_get_node(node, "InitSequence", NULL);
if (n1 && n1->child && n1->child->type == STP_MXML_TEXT)
answer->init_sequence = stp_xmlstrtoraw(n1->child->value.text.string);
- n1 = stp_mxmlFindElement(node, node, "DeinitSequence", NULL, NULL, STP_MXML_DESCEND);
+ n1 = stp_xml_get_node(node, "DeinitSequence", NULL);
if (n1 && n1->child && n1->child->type == STP_MXML_TEXT)
answer->deinit_sequence = stp_xmlstrtoraw(n1->child->value.text.string);
- n1 = stp_mxmlFindElement(node, node, "ExtraHeight", NULL, NULL, STP_MXML_DESCEND);
+ n1 = stp_xml_get_node(node, "ExtraHeight", NULL);
if (n1 && n1->child && n1->child->type == STP_MXML_TEXT)
answer->extra_height = stp_xmlstrtoul(n1->child->value.text.string);
return answer;
}
int
-stp_escp2_printer_supports_rollfeed(const stp_vars_t *v)
+stpi_escp2_printer_supports_rollfeed(const stp_vars_t *v)
{
stp_mxml_node_t *node = get_slots_xml(v);
- if (stp_mxmlFindElement(node, node, "RollFeed", NULL, NULL, STP_MXML_DESCEND))
+ if (stp_xml_get_node(node, "RollFeed", NULL))
return 1;
else
return 0;
}
int
-stp_escp2_printer_supports_print_to_cd(const stp_vars_t *v)
+stpi_escp2_printer_supports_print_to_cd(const stp_vars_t *v)
{
stp_mxml_node_t *node = get_slots_xml(v);
- if (stp_mxmlFindElement(node, node, "CD", NULL, NULL, STP_MXML_DESCEND))
+ if (stp_xml_get_node(node, "CD", NULL))
return 1;
else
return 0;
}
int
-stp_escp2_printer_supports_duplex(const stp_vars_t *v)
+stpi_escp2_printer_supports_duplex(const stp_vars_t *v)
{
stp_mxml_node_t *node = get_slots_xml(v);
- if (stp_mxmlFindElement(node, node, "Duplex", NULL, NULL, STP_MXML_DESCEND))
+ if (stp_xml_get_node(node, "Duplex", NULL))
return 1;
else
return 0;
@@ -472,7 +418,7 @@ get_input_slot_named(const stp_vars_t *v, const char *name)
{
input_slot_t *answer = NULL;
int i;
- stpi_escp2_printer_t *printdef = stp_escp2_get_printer(v);
+ stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v);
const stp_string_list_t *p = printdef->input_slots;
stp_list_t *cache = get_slots_cache(v);
stp_list_item_t *li = stp_list_get_item_by_name(cache, name);
@@ -485,15 +431,9 @@ get_input_slot_named(const stp_vars_t *v, const char *name)
{
if (!strcmp(name, stp_string_list_param(p, i)->name))
{
-#ifdef HAVE_LOCALE_H
- char *locale = stp_strdup(setlocale(LC_ALL, NULL));
- setlocale(LC_ALL, "C");
-#endif
+ stp_xml_init();
answer = build_input_slot(v, name);
-#ifdef HAVE_LOCALE_H
- setlocale(LC_ALL, locale);
- stp_free(locale);
-#endif
+ stp_xml_exit();
break;
}
}
@@ -504,9 +444,9 @@ get_input_slot_named(const stp_vars_t *v, const char *name)
}
const input_slot_t *
-stp_escp2_get_input_slot(const stp_vars_t *v)
+stpi_escp2_get_input_slot(const stp_vars_t *v)
{
- stpi_escp2_printer_t *printdef = stp_escp2_get_printer(v);
+ stpi_escp2_printer_t *printdef = stpi_escp2_get_printer(v);
const stp_string_list_t *p = printdef->input_slots;
if (p)
{