Commit 83fa78e2a82835e966473a0c03c03de0b6a5aefa

Authored by m-holger
1 parent 03fcdc63

In QPDFJob::json_schema refactor key-check logic with reusable function.

Replaces repetitive key-check conditions with a reusable lambda function `want_key`, improving code readability and maintainability. Simplifies logic across multiple conditional sections in `QPDFJob.cc`.
Showing 1 changed file with 21 additions and 19 deletions
libqpdf/QPDFJob.cc
@@ -1397,18 +1397,20 @@ QPDFJob::json_schema(int json_version, std::set<std::string>* keys) @@ -1397,18 +1397,20 @@ QPDFJob::json_schema(int json_version, std::set<std::string>* keys)
1397 "decodelevel": "decode level used to determine stream filterability" 1397 "decodelevel": "decode level used to determine stream filterability"
1398 })")); 1398 })"));
1399 1399
1400 - bool all_keys = ((keys == nullptr) || keys->empty()); 1400 + bool all_keys = !keys || keys->empty();
  1401 +
  1402 + auto want_key = [&](std::string const& key) -> bool { return all_keys || keys->contains(key); };
1401 1403
1402 // The list of selectable top-level keys id duplicated in the following places: job.yml, 1404 // The list of selectable top-level keys id duplicated in the following places: job.yml,
1403 // QPDFJob::json_schema, and QPDFJob::doJSON. 1405 // QPDFJob::json_schema, and QPDFJob::doJSON.
1404 if (json_version == 1) { 1406 if (json_version == 1) {
1405 - if (all_keys || keys->count("objects")) {  
1406 - schema.addDictionaryMember("objects", JSON::parse(R"({ 1407 + if (want_key("objects")) {
  1408 + (void)schema.addDictionaryMember("objects", JSON::parse(R"({
1407 "<n n R|trailer>": "json representation of object" 1409 "<n n R|trailer>": "json representation of object"
1408 })")); 1410 })"));
1409 } 1411 }
1410 - if (all_keys || keys->count("objectinfo")) {  
1411 - JSON objectinfo = schema.addDictionaryMember("objectinfo", JSON::parse(R"({ 1412 + if (want_key("objectinfo")) {
  1413 + (void)schema.addDictionaryMember("objectinfo", JSON::parse(R"({
1412 "<object-id>": { 1414 "<object-id>": {
1413 "stream": { 1415 "stream": {
1414 "filter": "if stream, its filters, otherwise null", 1416 "filter": "if stream, its filters, otherwise null",
@@ -1419,8 +1421,8 @@ QPDFJob::json_schema(int json_version, std::set&lt;std::string&gt;* keys) @@ -1419,8 +1421,8 @@ QPDFJob::json_schema(int json_version, std::set&lt;std::string&gt;* keys)
1419 })")); 1421 })"));
1420 } 1422 }
1421 } else { 1423 } else {
1422 - if (all_keys || keys->count("qpdf")) {  
1423 - schema.addDictionaryMember("qpdf", JSON::parse(R"([{ 1424 + if (want_key("qpdf")) {
  1425 + (void)schema.addDictionaryMember("qpdf", JSON::parse(R"([{
1424 "jsonversion": "numeric JSON version", 1426 "jsonversion": "numeric JSON version",
1425 "pdfversion": "PDF version as x.y", 1427 "pdfversion": "PDF version as x.y",
1426 "pushedinheritedpageresources": "whether inherited attributes were pushed to the page level", 1428 "pushedinheritedpageresources": "whether inherited attributes were pushed to the page level",
@@ -1432,8 +1434,8 @@ QPDFJob::json_schema(int json_version, std::set&lt;std::string&gt;* keys) @@ -1432,8 +1434,8 @@ QPDFJob::json_schema(int json_version, std::set&lt;std::string&gt;* keys)
1432 }])")); 1434 }])"));
1433 } 1435 }
1434 } 1436 }
1435 - if (all_keys || keys->count("pages")) {  
1436 - JSON page = schema.addDictionaryMember("pages", JSON::parse(R"([ 1437 + if (want_key("pages")) {
  1438 + (void)schema.addDictionaryMember("pages", JSON::parse(R"([
1437 { 1439 {
1438 "contents": [ 1440 "contents": [
1439 "reference to each content stream" 1441 "reference to each content stream"
@@ -1468,16 +1470,16 @@ QPDFJob::json_schema(int json_version, std::set&lt;std::string&gt;* keys) @@ -1468,16 +1470,16 @@ QPDFJob::json_schema(int json_version, std::set&lt;std::string&gt;* keys)
1468 } 1470 }
1469 ])")); 1471 ])"));
1470 } 1472 }
1471 - if (all_keys || keys->count("pagelabels")) {  
1472 - JSON labels = schema.addDictionaryMember("pagelabels", JSON::parse(R"([ 1473 + if (want_key("pagelabels")) {
  1474 + (void)schema.addDictionaryMember("pagelabels", JSON::parse(R"([
1473 { 1475 {
1474 "index": "starting page position starting from zero", 1476 "index": "starting page position starting from zero",
1475 "label": "page label dictionary" 1477 "label": "page label dictionary"
1476 } 1478 }
1477 ])")); 1479 ])"));
1478 } 1480 }
1479 - if (all_keys || keys->count("outlines")) {  
1480 - JSON outlines = schema.addDictionaryMember("outlines", JSON::parse(R"([ 1481 + if (want_key("outlines")) {
  1482 + (void)schema.addDictionaryMember("outlines", JSON::parse(R"([
1481 { 1483 {
1482 "dest": "outline destination dictionary", 1484 "dest": "outline destination dictionary",
1483 "destpageposfrom1": "position of destination page in document numbered from 1; null if not known", 1485 "destpageposfrom1": "position of destination page in document numbered from 1; null if not known",
@@ -1488,8 +1490,8 @@ QPDFJob::json_schema(int json_version, std::set&lt;std::string&gt;* keys) @@ -1488,8 +1490,8 @@ QPDFJob::json_schema(int json_version, std::set&lt;std::string&gt;* keys)
1488 } 1490 }
1489 ])")); 1491 ])"));
1490 } 1492 }
1491 - if (all_keys || keys->count("acroform")) {  
1492 - JSON acroform = schema.addDictionaryMember("acroform", JSON::parse(R"({ 1493 + if (want_key("acroform")) {
  1494 + (void)schema.addDictionaryMember("acroform", JSON::parse(R"({
1493 "fields": [ 1495 "fields": [
1494 { 1496 {
1495 "alternativename": "alternative name of field -- this is the one usually shown to users", 1497 "alternativename": "alternative name of field -- this is the one usually shown to users",
@@ -1522,8 +1524,8 @@ QPDFJob::json_schema(int json_version, std::set&lt;std::string&gt;* keys) @@ -1522,8 +1524,8 @@ QPDFJob::json_schema(int json_version, std::set&lt;std::string&gt;* keys)
1522 } 1524 }
1523 std::string MODIFY_ANNOTATIONS = 1525 std::string MODIFY_ANNOTATIONS =
1524 (json_version == 1 ? "moddifyannotations" : "modifyannotations"); 1526 (json_version == 1 ? "moddifyannotations" : "modifyannotations");
1525 - if (all_keys || keys->count("encrypt")) {  
1526 - JSON encrypt = schema.addDictionaryMember("encrypt", JSON::parse(R"({ 1527 + if (want_key("encrypt")) {
  1528 + (void)schema.addDictionaryMember("encrypt", JSON::parse(R"({
1527 "capabilities": { 1529 "capabilities": {
1528 "accessibility": "allow extraction for accessibility?", 1530 "accessibility": "allow extraction for accessibility?",
1529 "extract": "allow extraction?", 1531 "extract": "allow extraction?",
@@ -1552,8 +1554,8 @@ QPDFJob::json_schema(int json_version, std::set&lt;std::string&gt;* keys) @@ -1552,8 +1554,8 @@ QPDFJob::json_schema(int json_version, std::set&lt;std::string&gt;* keys)
1552 "userpasswordmatched": "whether supplied password matched user password; always false for non-encrypted files" 1554 "userpasswordmatched": "whether supplied password matched user password; always false for non-encrypted files"
1553 })")); 1555 })"));
1554 } 1556 }
1555 - if (all_keys || keys->count("attachments")) {  
1556 - JSON attachments = schema.addDictionaryMember("attachments", JSON::parse(R"({ 1557 + if (want_key("attachments")) {
  1558 + (void)schema.addDictionaryMember("attachments", JSON::parse(R"({
1557 "<attachment-key>": { 1559 "<attachment-key>": {
1558 "filespec": "object containing the file spec", 1560 "filespec": "object containing the file spec",
1559 "preferredcontents": "most preferred embedded file stream", 1561 "preferredcontents": "most preferred embedded file stream",