| 1 | #include <stdio.h> |
|---|
| 2 | #include <stdlib.h> |
|---|
| 3 | #include <errno.h> |
|---|
| 4 | |
|---|
| 5 | #include "bcmindexer.h" |
|---|
| 6 | #include "bcmplayer.h" |
|---|
| 7 | |
|---|
| 8 | /** |
|---|
| 9 | This test creates a NAV file from an SCT file, incrementing a vchip state. |
|---|
| 10 | The NAV file is then read back using bcmplayer. The output should be |
|---|
| 11 | the same as running the printindex utility as followed: |
|---|
| 12 | |
|---|
| 13 | ./printindex -o index,vchip NAVFILE |
|---|
| 14 | |
|---|
| 15 | **/ |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * Create the NAV file from an SCT file, inserting vchip information. |
|---|
| 19 | **/ |
|---|
| 20 | void createindex(const char *sctfile, const char *navfile) |
|---|
| 21 | { |
|---|
| 22 | |
|---|
| 23 | FILE *sct = fopen(sctfile, "r"); |
|---|
| 24 | if (!sct) { |
|---|
| 25 | printf("Cannot open %s to read: %d\n", sctfile, errno); |
|---|
| 26 | exit(1); |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | FILE *nav = fopen(navfile, "w+"); |
|---|
| 30 | if (!nav) { |
|---|
| 31 | printf("Cannot open %s to read: %d\n", navfile, errno); |
|---|
| 32 | exit(1); |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | BNAV_Indexer_Handle bcmi; |
|---|
| 36 | BNAV_Indexer_Settings settings; |
|---|
| 37 | |
|---|
| 38 | BNAV_Indexer_GetDefaultSettings(&settings); |
|---|
| 39 | settings.writeCallback = (BNAV_WRITE_CB)fwrite; |
|---|
| 40 | settings.filePointer = nav; |
|---|
| 41 | |
|---|
| 42 | if (BNAV_Indexer_Open(&bcmi, &settings)) |
|---|
| 43 | exit(1); |
|---|
| 44 | |
|---|
| 45 | int vchip = 0; |
|---|
| 46 | while (!feof(sct)) { |
|---|
| 47 | #define BUFSIZE 4096 |
|---|
| 48 | char buf[BUFSIZE]; |
|---|
| 49 | int n = fread(buf, 1, BUFSIZE, sct); |
|---|
| 50 | if (n == -1) { |
|---|
| 51 | printf("fread error: %d\n", errno); |
|---|
| 52 | exit(1); |
|---|
| 53 | } |
|---|
| 54 | if (BNAV_Indexer_SetVChipState(bcmi, (vchip++ | 0x4040))) |
|---|
| 55 | printf("Error setting vchip\n"); |
|---|
| 56 | BNAV_Indexer_Feed(bcmi, (BSCT_Entry*)buf, n / sizeof(BSCT_Entry)); |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | BNAV_Indexer_Close(bcmi); |
|---|
| 60 | fclose(sct); |
|---|
| 61 | fclose(nav); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | /** |
|---|
| 65 | * Read the vchip information back out using the player. |
|---|
| 66 | **/ |
|---|
| 67 | void readindex(const char *navfile) |
|---|
| 68 | { |
|---|
| 69 | BNAV_Player_Handle handle; |
|---|
| 70 | BNAV_Player_Settings settings; |
|---|
| 71 | FILE *f = fopen(navfile, "r"); |
|---|
| 72 | |
|---|
| 73 | if (!f) |
|---|
| 74 | exit(1); |
|---|
| 75 | |
|---|
| 76 | BNAV_Player_GetDefaultSettings(&settings); |
|---|
| 77 | settings.readCb = (BP_READ_CB)fread; |
|---|
| 78 | settings.seekCb = (BP_SEEK_CB)fseek; |
|---|
| 79 | settings.tellCb = (BP_TELL_CB)ftell; |
|---|
| 80 | settings.filePointer = f; |
|---|
| 81 | if (BNAV_Player_Open(&handle, &settings)) |
|---|
| 82 | exit(1); |
|---|
| 83 | |
|---|
| 84 | for (int i=0;!BNAV_Player_SetCurrentIndex(handle, i);i++) { |
|---|
| 85 | BNAV_Player_Position pos; |
|---|
| 86 | BNAV_Player_GetPosition(handle, &pos); |
|---|
| 87 | printf("%d: 0x%04x\n", i, pos.vchipState); |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | BNAV_Player_Close(handle); |
|---|
| 91 | } |
|---|
| 92 | |
|---|
| 93 | int main(int argc, char **argv) { |
|---|
| 94 | if (argc < 3) { |
|---|
| 95 | printf("Usage: ./vchip_createindex_test SCTFILE NAVFILE\n"); |
|---|
| 96 | exit(1); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | const char *sctfile = argv[1]; |
|---|
| 100 | const char *navfile = argv[2]; |
|---|
| 101 | |
|---|
| 102 | createindex(sctfile, navfile); |
|---|
| 103 | readindex(navfile); |
|---|
| 104 | return 0; |
|---|
| 105 | } |
|---|