//
// Name: INTERSTELLAR CONSOLE
// Author: Misato Observatory, Wakayama, Japan
// License: GPL (General Public License)
//

#include <stdio.h>

//
//  Reads first line in file_txt in current directory, 
//  and returns status and the line as string to buf_ret[].
//
static inline int get_first_line( const char *file_txt, char *buf_ret, int len_buf_ret )
{
    int flag_found;
    FILE *fp;

    flag_found = 0;
    fp = fopen(file_txt, "r");
    if ( fp != NULL ) {
      char line[len_buf_ret];
      while ( fgets(line, len_buf_ret, fp) != NULL ) {
        int i,j,last_delim;
        for ( i=0 ; i < len_buf_ret ; i++ ) {
          if ( line[i] == '\0' || ' ' < line[i] ) break;
        }
        if ( line[i] != '#' ) {
          j=0;
          last_delim=0;
          for ( ; i < len_buf_ret ; i++ ) {
            if ( line[i] < ' ' ) {
              if ( 0 < last_delim ) {
                buf_ret[last_delim] = '\0';
              } else {
                buf_ret[j] = '\0';
              }
              break;
            } else {
              buf_ret[j] = line[i];
              if ( ' ' < line[i] ) {
                last_delim = j + 1;
              }
              j++;
            }
          }
          if ( 0 < j ) {
            flag_found = 1;
            break;
          }
        }
      }
      fclose(fp);
    }

    return flag_found;
}

