loadfiles.c


     1	#include 
     2	#include "global.h"
     3	#include "trees.h"
     4	#include "ecoform.h"
     5	
     6	/********************************************************************
     7	 * loadSubFiles:
     8	 *    Opens the given branch, root, and leaf files and reads the
     9	 *    data into the linked lists that represent the branches, 
    10	 *    roots, and leaves of the tree.  TRUE is returned if no errors 
    11	 *    occur, FALSE otherwise.
    12	 ********************************************************************/
    13	
    14	bool loadSubFiles (brchFile,lvsFile,rtsFile,fSelect,fFirst,tree) 
    15	   string brchFile,lvsFile,rtsFile;
    16	   bool *fSelect, *fFirst;
    17	   treeData *tree;
    18	{
    19	   FILE *bfp,*lfp,*rfp;
    20	   bfp = fopen (brchFile, "r");
    21	   lfp = fopen (lvsFile, "r");
    22	   rfp = fopen (rtsFile, "r");
    23	   if (bfp == NULL) { 
    24	      fl_show_message ("Error: no branch data file found!","","");
    25	      return FALSE;
    26	   }
    27	   if (lfp == NULL){
    28	      fl_show_message ("Error: no leaf data file found!","","");
    29	      return FALSE;
    30	   }
    31	   if (rfp == NULL) { 
    32	      fl_show_message ("Error: no root data file found!","","");
    33	      return FALSE;
    34	   }
    35	    clearBrchList (&(tree->B));
    36	    clearLeafList (&(tree->L));
    37	    clearRootList (&(tree->R));
    38	    readBrchList (&(tree->B),bfp);
    39	    readLeafList (&(tree->L),lfp);
    40	    readRootList (&(tree->R),rfp);
    41	    *fSelect = TRUE;
    42	    *fFirst = TRUE;
    43	    return TRUE;
    44	}
    45	
    46	/********************************************************************
    47	 * getSubFileNames:
    48	 *    Creates strings that represent the pathes of the tree sub-
    49	 *    files by combining the files name and the directories they 
    50	 *    are found in into a single strings (bFile,lFile,rFile)
    51	 ********************************************************************/
    52	
    53	void getSubFileNames (dir,bFile,lFile,rFile,bStr,lStr,rStr)
    54	   strpointer dir,bFile,lFile,rFile;
    55	   string bStr,lStr,rStr;
    56	{
    57	   strcpy (bFile, dir);
    58	   strcpy (lFile, dir);
    59	   strcpy (rFile, dir);
    60	   strcat (bFile, "/");
    61	   strcat (lFile, "/");
    62	   strcat (rFile, "/");
    63	   strcat (bFile, bStr);
    64	   strcat (lFile, lStr);
    65	   strcat (rFile, rStr);
    66	}
    67	
    68	/********************************************************************
    69	 * loadTreeFiles:
    70	 *    Brings up the load file window and opens the file the user 
    71	 *    chose.  If the file name is valid, the curTree variables 
    72	 *    are assigned the values read in from the files.  
    73	 ********************************************************************/
    74	
    75	void loadTreeFiles (curTree,brchFile,lvsFile,rtsFile,fileSelected,fileFirst,fileBrow)
    76	   treeData *curTree;
    77	   string brchFile,lvsFile,rtsFile;
    78	   bool *fileSelected,*fileFirst;
    79	   FL_OBJECT *fileBrow;
    80	{
    81	   int i;
    82	   FILE *fp,*bfp,*lfp,*rfp;
    83	   brchSegPtr tempB;
    84	   lfStructPtr tempL;
    85	   rtSegPtr tempR;
    86	   float min, max, sun_x, sun_y, sun_z;
    87	   strpointer fname, strPtr, dir;
    88	   string temp, brch, lvs, rts;
    89	
    90	   /*** open load file window so user can select a file ***/
    91	   fname = fl_show_file_selector ("File To Load", "../datafiles", "*.eco", "");
    92	
    93	   if (fname != NULL) {
    94	      if ((fp = fopen (fname, "r")) != NULL) {
    95	         if ((fscanf (fp, "%s\n%s\n%s\n",brch,lvs,rts) != 3 ) ||
    96			(fscanf (fp, "%f %f %f\n",&sun_x,&sun_z,&sun_y) != 3)) {
    97	            fl_show_message ("Error: invalid data file!","","");
    98	            return;
    99	         }
   100		 /*** get the sun vector's coordinates ***/
   101		 curTree->sunVec_x = sun_x;
   102		 curTree->sunVec_y = sun_y;
   103		 curTree->sunVec_z = sun_z;
   104	
   105		 /*** get directory and create strings for sub-files ***/
   106		 dir = fl_get_directory ();
   107	         getSubFileNames (dir,brchFile,lvsFile,rtsFile,brch,lvs,rts);
   108	
   109		 /*** if any sub-file is not valid, return ***/ 
   110	         if (!loadSubFiles (brchFile,lvsFile,rtsFile,
   111			fileSelected,fileFirst,curTree)) {
   112	            return;
   113	         }
   114	
   115		 /*** read file data into the current tree's variables ***/
   116	         curTree->leavesChanged = TRUE;
   117	         curTree->rootsChanged = TRUE;  
   118		 curTree->branchChanged = TRUE;
   119	         if (curTree->leafType != NORMAL) {
   120	   	    setLeafAttributeRange (curTree->L,curTree->leafType,&min,&max);
   121	            curTree->minLfValue = min;
   122		    curTree->maxLfValue = max;
   123	            curTree->minLfValueOrig = min;
   124		    curTree->maxLfValueOrig = max;
   125	            sprintf (temp,"%f",curTree->minLfValue);
   126	            fl_set_input (lfmin, temp);
   127	            sprintf (temp,"%f",curTree->maxLfValue);
   128	            fl_set_input (lfmax, temp);
   129	         }
   130		 if ((curTree->rootType != NORMAL) && (curTree->rootType != DOUBLE)) {
   131	   	    setRootAttributeRange (curTree->R,curTree->rootType,&min,&max);
   132		    curTree->minRtValue = min;
   133		    curTree->maxRtValue = max;
   134		    curTree->minRtValueOrig = min;
   135		    curTree->maxRtValueOrig = max;
   136	            sprintf (temp,"%f",curTree->minRtValue);
   137	            fl_set_input (rtmin, temp);
   138	            sprintf (temp,"%f",curTree->maxRtValue);
   139	            fl_set_input (rtmax, temp);
   140	         }
   141		 fl_clear_browser (fileBrow);
   142	         fl_add_browser_line (fileBrow, fname);
   143	         fclose (fp);
   144	      } else {
   145	         fl_show_message ("Error: file not found!","","");
   146	      }
   147	   }
   148	}
   149	
   150	/********************  end of loadfiles.c **************************/
   151