Permalink
Browse files

Add more public API for the TOC

  • Loading branch information...
1 parent 028c9bb commit 0f628eddd2e80e0d328ca91e3e9ee409a32df124 @neror neror committed Nov 8, 2012
Showing with 79 additions and 8 deletions.
  1. +30 −1 EPUB3.c
  2. +6 −1 EPUB3.h
  3. +0 −3 EPUB3_private.h
  4. +43 −3 TestEPUB3Processor/check_EPUB3.c
View
31 EPUB3.c
@@ -47,14 +47,42 @@ EXPORT EPUB3Error EPUB3GetPathsOfSequentialResources(EPUB3Ref epub, const char *
return error;
}
-EXPORT int32_t EPUB3GetCountOfTocRootItems(EPUB3Ref epub)
+EXPORT int32_t EPUB3CountOfTocRootItems(EPUB3Ref epub)
{
assert(epub != NULL);
assert(epub->toc != NULL);
return epub->toc->rootItemCount;
}
+EXPORT EPUB3Error EPUB3GetTocRootItems(EPUB3Ref epub, EPUB3TocItemRef *tocItems)
+{
+ assert(epub != NULL);
+ assert(epub->toc != NULL);
+
+ EPUB3Error error = kEPUB3Success;
+
+ if(epub->toc->rootItemCount > 0) {
+ int32_t count = 0;
+ EPUB3TocItemChildListItemPtr itemPtr = epub->toc->rootItemsHead;
+ while(itemPtr != NULL) {
+ tocItems[count] = itemPtr->item;
+ count++;
+ itemPtr = itemPtr->next;
+ }
+
+ }
+
+ return error;
+}
+
+EXPORT EPUB3Bool EPUB3TocItemHasParent(EPUB3TocItemRef tocItem)
+{
+ assert(tocItem != NULL);
+
+ return tocItem->parent == NULL ? kEPUB3_NO : kEPUB3_YES;
+}
+
#pragma mark - Base Object
void EPUB3ObjectRelease(void *object)
@@ -355,6 +383,7 @@ EPUB3TocItemRef EPUB3TocItemCreate()
memory = EPUB3ObjectInitWithTypeID(memory, kEPUB3TocItemTypeID);
memory->idref = NULL;
memory->manifestItem = NULL;
+ memory->parent = NULL;
memory->childCount = 0;
memory->childrenHead = NULL;
memory->childrenTail = NULL;
View
@@ -21,7 +21,10 @@ typedef enum _EPUB3Error {
kEPUB3XMLXDocumentInvalidError = 1010,
} EPUB3Error;
+typedef enum { kEPUB3_NO = 0 , kEPUB3_YES = 1 } EPUB3Bool;
+
typedef struct EPUB3 * EPUB3Ref;
+typedef struct EPUB3TocItem * EPUB3TocItemRef;
EPUB3Ref EPUB3CreateWithArchiveAtPath(const char * path, EPUB3Error *error);
void EPUB3Retain(EPUB3Ref epub);
@@ -36,7 +39,9 @@ EPUB3Error EPUB3GetPathsOfSequentialResources(EPUB3Ref epub, const char ** resou
EPUB3Error EPUB3ExtractArchiveToPath(EPUB3Ref epub, const char * path);
EPUB3Error EPUB3CopyRootFilePathFromContainer(EPUB3Ref epub, char ** rootPath);
-int32_t EPUB3GetCountOfTocRootItems(EPUB3Ref epub);
+int32_t EPUB3CountOfTocRootItems(EPUB3Ref epub);
+EPUB3Error EPUB3GetTocRootItems(EPUB3Ref epub, EPUB3TocItemRef *tocItems);
+EPUB3Bool EPUB3TocItemHasParent(EPUB3TocItemRef tocItem);
#if defined(__cplusplus)
} //EXTERN "C"
View
@@ -20,8 +20,6 @@
#define EXPORT __attribute__((visibility("default")))
-typedef enum { kEPUB3_NO = 0 , kEPUB3_YES = 1 } EPUB3Bool;
-
#pragma mark - Object Pointer Types
typedef struct EPUB3Metadata * EPUB3MetadataRef;
@@ -30,7 +28,6 @@ typedef struct EPUB3Manifest * EPUB3ManifestRef;
typedef struct EPUB3Spine * EPUB3SpineRef;
typedef struct EPUB3SpineItem * EPUB3SpineItemRef;
typedef struct EPUB3Toc * EPUB3TocRef;
-typedef struct EPUB3TocItem * EPUB3TocItemRef;
const char * kEPUB3TypeID;
const char * kEPUB3MetadataTypeID;
@@ -166,8 +166,8 @@ START_TEST(test_epub3_toc)
}
END_TEST
-#pragma mark test_epub3_toc_root_list
-START_TEST(test_epub3_toc_root_list)
+#pragma mark test_epub3_toc_root_list_addition
+START_TEST(test_epub3_toc_root_list_addition)
{
EPUB3TocRef toc = EPUB3TocCreate();
fail_unless(toc->rootItemCount == 0);
@@ -206,6 +206,44 @@ START_TEST(test_epub3_toc_root_list)
}
END_TEST
+#pragma mark test_epub3_toc_root_list_get
+START_TEST(test_epub3_toc_root_list_get)
+{
+ EPUB3Ref blankEpub = EPUB3Create();
+ EPUB3TocRef toc = EPUB3TocCreate();
+ blankEpub->toc = toc;
+
+ int32_t itemCount = 20;
+ EPUB3TocItemRef items[itemCount];
+
+ const char * idref = "my_idref";
+ for(int i = 0; i < itemCount; i++) {
+ EPUB3TocItemRef item = EPUB3TocItemCreate();
+ item->idref = strdup(idref);
+ EPUB3TocAddRootItem(toc, item);
+ items[i] = item;
+ }
+
+ int32_t count = EPUB3CountOfTocRootItems(blankEpub);
+ ck_assert_int_eq(itemCount, count);
+
+ EPUB3TocItemRef rootList[count];
+
+ EPUB3Error error = EPUB3GetTocRootItems(blankEpub, rootList);
+ fail_unless(error == kEPUB3Success);
+
+ for (int i = 0; i < count; i++) {
+ fail_if(rootList[i] == NULL);
+ fail_unless(EPUB3TocItemHasParent(rootList[i]) == kEPUB3_NO);
+ ck_assert_str_eq(idref, rootList[i]->idref);
+ fail_unless(items[i] == rootList[i]);
+ EPUB3TocItemRelease(items[i]);
+ }
+
+ EPUB3TocRelease(toc);
+}
+END_TEST
+
#pragma mark test_epub3_toc_tree
START_TEST(test_epub3_toc_tree)
{
@@ -228,6 +266,7 @@ START_TEST(test_epub3_toc_tree)
firstChild = item;
}
fail_unless(rootItem->childrenTail->item == item);
+ fail_unless(EPUB3TocItemHasParent(item) == kEPUB3_YES);
fail_unless(item->parent == rootItem);
EPUB3TocItemRelease(item);
}
@@ -462,8 +501,9 @@ TEST_EXPORT TCase * check_EPUB3_make_tcase(void)
tcase_add_test(test_case, test_metadata_object);
tcase_add_test(test_case, test_epub3_manifest_hash);
tcase_add_test(test_case, test_epub3_toc);
- tcase_add_test(test_case, test_epub3_toc_root_list);
+ tcase_add_test(test_case, test_epub3_toc_root_list_addition);
tcase_add_test(test_case, test_epub3_toc_tree);
+ tcase_add_test(test_case, test_epub3_toc_root_list_get);
tcase_add_test(test_case, test_epub3_spine);
tcase_add_test(test_case, test_epub3_spine_list);
tcase_add_test(test_case, test_epub3_copy_cover_image);

0 comments on commit 0f628ed

Please sign in to comment.