src/sel1.c
SELA *
selaCreateFromFile(const char  *filename)
{
char    *filestr, *line;
l_int32  i, n, first, last, nsel, insel;
size_t   nbytes;
NUMA    *nafirst, *nalast;
SARRAY  *sa;
SEL     *sel;
SELA    *sela;

    PROCNAME("selaCreateFromFile");

    if (!filename)
        return (SELA *)ERROR_PTR("filename not defined", procName, NULL);
    
    filestr = (char *)l_binaryRead(filename, &nbytes);
    sa = sarrayCreateLinesFromString(filestr, 1);
    FREE(filestr);
    n = sarrayGetCount(sa);
    sela = selaCreate(0);

        /* Find the start and end lines for each Sel.
         * We allow the "blank" lines to be null strings or
         * to have standard whitespace (' ','\t',\'n') or be '#'. */
    nafirst = numaCreate(0);
    nalast = numaCreate(0);
    insel = FALSE;
    for (i = 0; i < n; i++) {
        line = sarrayGetString(sa, i, L_NOCOPY);
        if (!insel &&
            (line[0] != '\0' && line[0] != ' ' &&
             line[0] != '\t' && line[0] != '\n' && line[0] != '#')) {
            numaAddNumber(nafirst, i);
            insel = TRUE;
	    continue;
        }	    
	if (insel &&
            (line[0] == '\0' || line[0] == ' ' ||
             line[0] == '\t' || line[0] == '\n' || line[0] == '#')) {
            numaAddNumber(nalast, i - 1);
            insel = FALSE;
            continue;
        }	    
    }
    if (insel)  /* fell off the end of the file */
        numaAddNumber(nalast, n - 1);

        /* Extract sels */
    nsel = numaGetCount(nafirst);
    for (i = 0; i < nsel; i++) {
        numaGetIValue(nafirst, i, &first);
        numaGetIValue(nalast, i, &last);
        if ((sel = selCreateFromSArray(sa, first, last)) == NULL) {
            fprintf(stderr, "Error reading sel from %d to %d\n", first, last);
            selaDestroy(&sela);
            sarrayDestroy(&sa);
            numaDestroy(&nafirst);
            numaDestroy(&nalast);
            return (SELA *)ERROR_PTR("bad sela file", procName, NULL);
        }
	selaAddSel(sela, sel, NULL, 0);
    }

    numaDestroy(&nafirst);
    numaDestroy(&nalast);
    sarrayDestroy(&sa);
    return sela;
}
/*!
 *  selaCreateFromFile()
 *
 *      Input:  filename
 *      Return: sela, or null on error
 *
 *  Notes:
 *      (1) The file contains a sequence of Sel descriptions.
 *      (2) Each Sel is formatted as follows:
 *           - Any number of comment lines starting with '#' are ignored
 *           - The next line contains the selname
 *           - The next lines contain the Sel data.  They must be
 *             formatted similarly to the string format in
 *             selCreateFromString(), with each line beginning and
 *             ending with a double-quote, and showing the 2D layout.
 *           - Each Sel ends when a blank line, a comment line, or
 *             the end of file is reached.
 *      (3) See selCreateFromString() for a description of the string
 *          format for the Sel data.  As an example, here are the lines
 *          of is a valid file for a single Sel.  In the file, all lines 
 *          are left-justified:
 *                    # diagonal sel
 *                    sel_5diag
 *                    "x    "
 *                    " x   "
 *                    "  X  "
 *                    "   x "
 *                    "    x"
 */
selaCreateFromFile()
Input:  filename
Return: sela, or null on error

(1) The file contains a sequence of Sel descriptions.
(2) Each Sel is formatted as follows:
- Any number of comment lines starting with '#' are ignored
- The next line contains the selname
- The next lines contain the Sel data.  They must be
formatted similarly to the string format in
selCreateFromString(), with each line beginning and
ending with a double-quote, and showing the 2D layout.
- Each Sel ends when a blank line, a comment line, or
the end of file is reached.
(3) See selCreateFromString() for a description of the string
format for the Sel data.  As an example, here are the lines
of is a valid file for a single Sel.
selaCreateFromFile () Input : filename Return : sela or null on error ( 1 ) The file contains a sequence of Sel descriptions . ( 2 ) Each Sel is formatted as follows : - Any number of comment lines starting with ' # ' are ignored - The next line contains the selname - The next lines contain the Sel data . They must be formatted similarly to the string format in selCreateFromString () with each line beginning and ending with a double - quote and showing the 2D layout . - Each Sel ends when a blank line a comment line or the end of file is reached . ( 3 ) See selCreateFromString () for a description of the string format for the Sel data . As an example here are the lines of is a valid file for a single Sel .
SELA *
selaCreateFromFile(const char  *filename)
{
char    *filestr, *line;
l_int32  i, n, first, last, nsel, insel;
size_t   nbytes;
NUMA    *nafirst, *nalast;
SARRAY  *sa;
SEL     *sel;
SELA    *sela;
    PROCNAME("selaCreateFromFile");
    if (!filename)
        return (SELA *)ERROR_PTR("filename not defined", procName, NULL);
    filestr = (char *)l_binaryRead(filename, &nbytes);
    sa = sarrayCreateLinesFromString(filestr, 1);
    FREE(filestr);
    n = sarrayGetCount(sa);
    sela = selaCreate(0);
    nafirst = numaCreate(0);
    nalast = numaCreate(0);
    insel = FALSE;
    for (i = 0; i < n; i++) {
        line = sarrayGetString(sa, i, L_NOCOPY);
        if (!insel &&
            (line[0] != '\0' && line[0] != ' ' &&
             line[0] != '\t' && line[0] != '\n' && line[0] != '#')) {
            numaAddNumber(nafirst, i);
            insel = TRUE;
	    continue;
        }	    
	if (insel &&
            (line[0] == '\0' || line[0] == ' ' ||
             line[0] == '\t' || line[0] == '\n' || line[0] == '#')) {
            numaAddNumber(nalast, i - 1);
            insel = FALSE;
            continue;
        }	    
    }
    if (insel)  
        numaAddNumber(nalast, n - 1);
    nsel = numaGetCount(nafirst);
    for (i = 0; i < nsel; i++) {
        numaGetIValue(nafirst, i, &first);
        numaGetIValue(nalast, i, &last);
        if ((sel = selCreateFromSArray(sa, first, last)) == NULL) {
            fprintf(stderr, "Error reading sel from %d to %d\n", first, last);
            selaDestroy(&sela);
            sarrayDestroy(&sa);
            numaDestroy(&nafirst);
            numaDestroy(&nalast);
            return (SELA *)ERROR_PTR("bad sela file", procName, NULL);
        }
	selaAddSel(sela, sel, NULL, 0);
    }
    numaDestroy(&nafirst);
    numaDestroy(&nalast);
    sarrayDestroy(&sa);
    return sela;
}
SELA * selaCreateFromFile ( const char * filename ) { char * filestr , * line ; l_int32 i , n , first , last , nsel , insel ; size_t nbytes ; NUMA * nafirst , * nalast ; SARRAY * sa ; SEL * sel ; SELA * sela ; PROCNAME ( " " ) ; if ( ! filename ) return ( SELA * ) ERROR_PTR ( " " , procName , NULL ) ; filestr = ( char * ) l_binaryRead ( filename , & nbytes ) ; sa = sarrayCreateLinesFromString ( filestr , 1 ) ; FREE ( filestr ) ; n = sarrayGetCount ( sa ) ; sela = selaCreate ( 0 ) ; nafirst = numaCreate ( 0 ) ; nalast = numaCreate ( 0 ) ; insel = FALSE ; for ( i = 0 ; i < n ; i ++ ) { line = sarrayGetString ( sa , i , L_NOCOPY ) ; if ( ! insel && ( line [ 0 ] != ' \0 ' && line [ 0 ] != ' ' && line [ 0 ] != ' \t ' && line [ 0 ] != ' \n ' && line [ 0 ] != ' ' ) ) { numaAddNumber ( nafirst , i ) ; insel = TRUE ; continue ; } if ( insel && ( line [ 0 ] == ' \0 ' || line [ 0 ] == ' ' || line [ 0 ] == ' \t ' || line [ 0 ] == ' \n ' || line [ 0 ] == ' ' ) ) { numaAddNumber ( nalast , i - 1 ) ; insel = FALSE ; continue ; } } if ( insel ) numaAddNumber ( nalast , n - 1 ) ; nsel = numaGetCount ( nafirst ) ; for ( i = 0 ; i < nsel ; i ++ ) { numaGetIValue ( nafirst , i , & first ) ; numaGetIValue ( nalast , i , & last ) ; if ( ( sel = selCreateFromSArray ( sa , first , last ) ) == NULL ) { fprintf ( stderr , " \n " , first , last ) ; selaDestroy ( & sela ) ; sarrayDestroy ( & sa ) ; numaDestroy ( & nafirst ) ; numaDestroy ( & nalast ) ; return ( SELA * ) ERROR_PTR ( " " , procName , NULL ) ; } selaAddSel ( sela , sel , NULL , 0 ) ; } numaDestroy ( & nafirst ) ; numaDestroy ( & nalast ) ; sarrayDestroy ( & sa ) ; return sela ; }
selaCreateFromFile()
      Input:  filename
      Return: sela, or null on error
selaCreateFromFile () Input : filename Return : sela or null on error
/* Find the start and end lines for each Sel.
         * We allow the "blank" lines to be null strings or
         * to have standard whitespace (' ','\t',\'n') or be '#'. */ /* fell off the end of the file */ /* Extract sels */