! * \brief l_getIndexFromFile() * * \param[in] filename * \param[out] pindex found index * \return 0 if found, 1 on error. */
| 641 | * \return 0 if found, 1 on error. |
| 642 | */ |
| 643 | static l_int32 |
| 644 | l_getIndexFromFile(const char *filename, |
| 645 | l_int32 *pindex) |
| 646 | { |
| 647 | char buf[256]; |
| 648 | char *word; |
| 649 | FILE *fp; |
| 650 | l_int32 notfound, format; |
| 651 | SARRAY *sa; |
| 652 | |
| 653 | PROCNAME("l_getIndexFromFile"); |
| 654 | |
| 655 | if (!pindex) |
| 656 | return ERROR_INT("&index not defined", procName, 1); |
| 657 | *pindex = 0; |
| 658 | if (!filename) |
| 659 | return ERROR_INT("filename not defined", procName, 1); |
| 660 | |
| 661 | /* Open the stream, read lines until you find one with more |
| 662 | * than a newline, and grab the first word. */ |
| 663 | if ((fp = fopenReadStream(filename)) == NULL) |
| 664 | return ERROR_INT("stream not opened", procName, 1); |
| 665 | do { |
| 666 | if ((fgets(buf, sizeof(buf), fp)) == NULL) { |
| 667 | fclose(fp); |
| 668 | return ERROR_INT("fgets read fail", procName, 1); |
| 669 | } |
| 670 | } while (buf[0] == '\n'); |
| 671 | fclose(fp); |
| 672 | sa = sarrayCreateWordsFromString(buf); |
| 673 | word = sarrayGetString(sa, 0, L_NOCOPY); |
| 674 | |
| 675 | /* Find the index associated with the word. If it is not |
| 676 | * found, test to see if the file is a compressed pix. */ |
| 677 | notfound = l_getIndexFromStructname(word, pindex); |
| 678 | sarrayDestroy(&sa); |
| 679 | if (notfound) { /* maybe a Pix */ |
| 680 | if (findFileFormat(filename, &format) == 0) { |
| 681 | l_getIndexFromStructname("Pix", pindex); |
| 682 | } else { |
| 683 | return ERROR_INT("no file type identified", procName, 1); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | return 0; |
| 688 | } |
| 689 | |
| 690 | |
| 691 | /*! |
no test coverage detected