summaryrefslogtreecommitdiffstats
path: root/src/plist.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/plist.c')
-rw-r--r--src/plist.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/src/plist.c b/src/plist.c
index 8d57416..01e44df 100644
--- a/src/plist.c
+++ b/src/plist.c
@@ -1573,3 +1573,95 @@ PLIST_API void plist_sort(plist_t plist)
1573 } while (swapped); 1573 } while (swapped);
1574 } 1574 }
1575} 1575}
1576
1577PLIST_API plist_err_t plist_write_to_string(plist_t plist, char **output, uint32_t* length, plist_format_t format, plist_write_options_t options)
1578{
1579 plist_err_t err = PLIST_ERR_UNKNOWN;
1580 switch (format) {
1581 case PLIST_FORMAT_XML:
1582 err = plist_to_xml(plist, output, length);
1583 break;
1584 case PLIST_FORMAT_JSON:
1585 err = plist_to_json(plist, output, length, ((options & PLIST_OPT_COMPACT) == 0));
1586 break;
1587 case PLIST_FORMAT_OSTEP:
1588 err = plist_to_openstep(plist, output, length, ((options & PLIST_OPT_COMPACT) == 0));
1589 break;
1590 case PLIST_FORMAT_PRINT:
1591 err = plist_write_to_string_default(plist, output, length, options);
1592 break;
1593 case PLIST_FORMAT_LIMD:
1594 err = plist_write_to_string_limd(plist, output, length, options);
1595 break;
1596 case PLIST_FORMAT_PLUTIL:
1597 err = plist_write_to_string_plutil(plist, output, length, options);
1598 break;
1599 default:
1600 // unsupported output format
1601 err = PLIST_ERR_FORMAT;
1602 break;
1603 }
1604 return err;
1605}
1606
1607PLIST_API plist_err_t plist_write_to_stream(plist_t plist, FILE *stream, plist_format_t format, plist_write_options_t options)
1608{
1609 if (!plist || !stream) {
1610 return PLIST_ERR_INVALID_ARG;
1611 }
1612 plist_err_t err = PLIST_ERR_UNKNOWN;
1613 char *output = NULL;
1614 uint32_t length = 0;
1615 switch (format) {
1616 case PLIST_FORMAT_BINARY:
1617 err = plist_to_bin(plist, &output, &length);
1618 break;
1619 case PLIST_FORMAT_XML:
1620 err = plist_to_xml(plist, &output, &length);
1621 break;
1622 case PLIST_FORMAT_JSON:
1623 err = plist_to_json(plist, &output, &length, ((options & PLIST_OPT_COMPACT) == 0));
1624 break;
1625 case PLIST_FORMAT_OSTEP:
1626 err = plist_to_openstep(plist, &output, &length, ((options & PLIST_OPT_COMPACT) == 0));
1627 break;
1628 case PLIST_FORMAT_PRINT:
1629 err = plist_write_to_stream_default(plist, stream, options);
1630 break;
1631 case PLIST_FORMAT_LIMD:
1632 err = plist_write_to_stream_limd(plist, stream, options);
1633 break;
1634 case PLIST_FORMAT_PLUTIL:
1635 err = plist_write_to_stream_plutil(plist, stream, options);
1636 break;
1637 default:
1638 // unsupported output format
1639 err = PLIST_ERR_FORMAT;
1640 break;
1641 }
1642 if (output && err == PLIST_ERR_SUCCESS) {
1643 if (fwrite(output, 1, length, stream) < length) {
1644 err = PLIST_ERR_IO;
1645 }
1646 }
1647 return err;
1648}
1649
1650PLIST_API plist_err_t plist_write_to_file(plist_t plist, const char* filename, plist_format_t format, plist_write_options_t options)
1651{
1652 if (!plist || !filename) {
1653 return PLIST_ERR_INVALID_ARG;
1654 }
1655 FILE* f = fopen(filename, "wb");
1656 if (!f) {
1657 return PLIST_ERR_IO;
1658 }
1659 plist_err_t err = plist_write_to_stream(plist, f, format, options);
1660 fclose(f);
1661 return err;
1662}
1663
1664PLIST_API void plist_print(plist_t plist)
1665{
1666 plist_write_to_stream(plist, stdout, PLIST_FORMAT_PRINT, PLIST_OPT_PARTIAL_DATA);
1667}