----------------------------------------------------------------------* * 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
| 1776 | * </pre> |
| 1777 | */ |
| 1778 | SELA * |
| 1779 | selaCreateFromFile(const char *filename) |
| 1780 | { |
| 1781 | char *filestr, *line; |
| 1782 | l_int32 i, n, first, last, nsel, insel; |
| 1783 | size_t nbytes; |
| 1784 | NUMA *nafirst, *nalast; |
| 1785 | SARRAY *sa; |
| 1786 | SEL *sel; |
| 1787 | SELA *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); |
nothing calls this directly
no test coverage detected