MCPcopy Create free account
hub / github.com/creatale/node-dv / l_getIndexFromFile

Function l_getIndexFromFile

deps/leptonica/src/stringcode.c:643–688  ·  view source on GitHub ↗

! * \brief l_getIndexFromFile() * * \param[in] filename * \param[out] pindex found index * \return 0 if found, 1 on error. */

Source from the content-addressed store, hash-verified

641 * \return 0 if found, 1 on error.
642 */
643static l_int32
644l_getIndexFromFile(const char *filename,
645 l_int32 *pindex)
646{
647char buf[256];
648char *word;
649FILE *fp;
650l_int32 notfound, format;
651SARRAY *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/*!

Callers 2

strcodeCreateFromFileFunction · 0.85
l_getStructStrFromFileFunction · 0.85

Calls 6

fopenReadStreamFunction · 0.85
sarrayGetStringFunction · 0.85
l_getIndexFromStructnameFunction · 0.85
sarrayDestroyFunction · 0.85
findFileFormatFunction · 0.85

Tested by

no test coverage detected