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

Function selaCreateFromFile

deps/leptonica/src/sel1.c:1778–1846  ·  view source on GitHub ↗

----------------------------------------------------------------------* * Building custom hit-miss sels from a simple file format * *----------------------------------------------------------------------*/ ! * \brief selaCreateFromFile() * * \param[in] filename * \return sela, or NULL on error * * * Notes: * (1) The file contains a sequence of Sel descriptio

Source from the content-addressed store, hash-verified

1776 * </pre>
1777 */
1778SELA *
1779selaCreateFromFile(const char *filename)
1780{
1781char *filestr, *line;
1782l_int32 i, n, first, last, nsel, insel;
1783size_t nbytes;
1784NUMA *nafirst, *nalast;
1785SARRAY *sa;
1786SEL *sel;
1787SELA *sela;
1788
1789 PROCNAME("selaCreateFromFile");
1790
1791 if (!filename)
1792 return (SELA *)ERROR_PTR("filename not defined", procName, NULL);
1793
1794 filestr = (char *)l_binaryRead(filename, &nbytes);
1795 sa = sarrayCreateLinesFromString(filestr, 1);
1796 LEPT_FREE(filestr);
1797 n = sarrayGetCount(sa);
1798 sela = selaCreate(0);
1799
1800 /* Find the start and end lines for each Sel.
1801 * We allow the "blank" lines to be null strings or
1802 * to have standard whitespace (' ','\t',\'n') or be '#'. */
1803 nafirst = numaCreate(0);
1804 nalast = numaCreate(0);
1805 insel = FALSE;
1806 for (i = 0; i < n; i++) {
1807 line = sarrayGetString(sa, i, L_NOCOPY);
1808 if (!insel &&
1809 (line[0] != '\0' && line[0] != ' ' &&
1810 line[0] != '\t' && line[0] != '\n' && line[0] != '#')) {
1811 numaAddNumber(nafirst, i);
1812 insel = TRUE;
1813 continue;
1814 }
1815 if (insel &&
1816 (line[0] == '\0' || line[0] == ' ' ||
1817 line[0] == '\t' || line[0] == '\n' || line[0] == '#')) {
1818 numaAddNumber(nalast, i - 1);
1819 insel = FALSE;
1820 continue;
1821 }
1822 }
1823 if (insel) /* fell off the end of the file */
1824 numaAddNumber(nalast, n - 1);
1825
1826 /* Extract sels */
1827 nsel = numaGetCount(nafirst);
1828 for (i = 0; i < nsel; i++) {
1829 numaGetIValue(nafirst, i, &first);
1830 numaGetIValue(nalast, i, &last);
1831 if ((sel = selCreateFromSArray(sa, first, last)) == NULL) {
1832 fprintf(stderr, "Error reading sel from %d to %d\n", first, last);
1833 selaDestroy(&sela);
1834 sarrayDestroy(&sa);
1835 numaDestroy(&nafirst);

Callers

nothing calls this directly

Calls 14

l_binaryReadFunction · 0.85
sarrayGetCountFunction · 0.85
selaCreateFunction · 0.85
numaCreateFunction · 0.85
sarrayGetStringFunction · 0.85
numaAddNumberFunction · 0.85
numaGetCountFunction · 0.85
numaGetIValueFunction · 0.85
selCreateFromSArrayFunction · 0.85
selaDestroyFunction · 0.85
sarrayDestroyFunction · 0.85

Tested by

no test coverage detected