Please help...

marconi_07's Avatar, Join Date: Aug 2009
Light Poster
Hi,

I have the following C code, which does the following :-

Takes the brt file name from character 1 of the input filename (ALLSMFAS.ref) and creates a new file (A.brt) as follows :-

ALLSMFAS.ref
-----------------
AD,B,0424,0415,89375,,A,2,,80,,
AD,B,0424,0417,89375,,A,1,,82,,
AD,B,0416,0410,89375,,B,2,,-7,2,
BN,B,0135,0094,65625,,D,,,0,1,H
BN,B,0338,0337,65824,,B,,,-18,1,
BN,B,0334,0339,65824,,C,,,65,,
CA,T,,SREC,47221,,C,,R,0,,
CA,T,,X171,47221,,A,6,,60,,
CA,T,,X173,47221,,A,5,,60,,
CC,B,1783,1574,48231,,C,1,,60,,
CC,B,1783,1576,48231,,C,2,,60,,
CC,B,1783,1578,48231,,C,3,,60,,

A.brt
------
B0424041589375 B0424041789375 B0416041089375 B0135009465625 B0338033765824 B0334033965824
T SREC47221 T X17147221 T X17347221 B1783157448231 B1783157648231 B1783157848231


Code:
#include "p2geog.h"  
#define max_smf_lines   1000 * td_max
 
// Reformat the structure of smf_file_layout 
struct smf_file_layout
{
 char      line_type[2];
 char      step_type;
 char      from_berth[4];
 char      to_berth[4];
 char      stanox[5];
 char      event_type;  
 char      thru_line;
 char      platform_id;
 int       event_time_offset;
 char      forward_line;
 char      forward_route;
 unsigned long    sort_key;
};
 
struct tds_from_smf
{
 char      td_id[2];
 int       sfl_ind;
 int       sfl_rec_count;
};
struct smf_file_layout   sfl[max_smf_lines];
struct tds_from_smf    tds_ive_seen[td_max];
int        sfl_count;
extern HWND      main_handle;
extern char      berth_step_dir[],
        td_maps_dir[];
extern BOOL      gen_brt_files;
extern struct td_header_tree tdh[td_max];
extern struct td_map_tree  tdm[td_map_max];
extern struct adjacent_maps  *am[];
extern int      map_count,
        adj_map_count,
        td_header_count;
extern FILE      *file_handle;
 
void generate_brt_files(void)
{
 
 char      f_name[MAX_PATH],
        this_td[3];
//        buffer[200];
 FILE      *ref_file;
 WIN32_FIND_DATA    wfd;
 HANDLE      find_handle;
 int       seen_td_count = 0,
        cnt;
//        ptr,
//        knt;
 
 if (gen_brt_files)
 {
  if (!berth_step_dir[0])
  {
   MessageBox(main_handle,
        "No location has been set for the berth stepping files.",
        "Error,,,",
        MB_ICONERROR | MB_OK);
   return;
  }
 
  memset(&wfd,
      0,
      sizeof(WIN32_FIND_DATA));
 
  // Change the input file type format. 
  wsprintf(f_name,
     "%sallsmf*.ref",
     berth_step_dir);
  if ((find_handle = FindFirstFile(f_name,
           &wfd)) == INVALID_HANDLE_VALUE)
  {
   MessageBox(main_handle,
        "No SMART berth stepping files found.",
        "Error...",
        MB_ICONERROR | MB_OK);
   return;
  }
  sfl_count = 0;
  memset(tds_ive_seen,
      0,
      sizeof(tds_ive_seen));
  memset(sfl,
      0,
      sizeof(sfl));
  while (1)
  {
   wsprintf(f_name,
      "%s%s",
      berth_step_dir,
      wfd.cFileName);
   if (ref_file = fopen(f_name,
         "rb"))
   {
 
       // Take the brt file name from character 1 of the input filename.
    memcpy(this_td,
        wfd.cFileName,
        1);
    this_td[2] = 0;
 
    for (cnt = 0; cnt < seen_td_count; cnt++)
    {
     if (!memicmp(&this_td,
         &tds_ive_seen[cnt].td_id,
         2))
      break;
    }
    if (cnt >= seen_td_count)
    {
 
     // only process the file if we haven't seen this TD already
     memcpy(&tds_ive_seen[seen_td_count].td_id,
         this_td,
         2);
     tds_ive_seen[seen_td_count].sfl_ind = sfl_count;
     read_smf_file(ref_file);
     tds_ive_seen[seen_td_count].sfl_rec_count = sfl_count - 
tds_ive_seen[seen_td_count].sfl_ind;
     if (gen_brt_files)
      write_brt_file(seen_td_count);
     seen_td_count++;
    }
    fclose(ref_file);
   }
   if (!FindNextFile(find_handle,
         &wfd))
    break;
  }
  FindClose(find_handle);
 } // end if (gen_brt_files)
 else
 {
  gen_brt_files = FALSE;
  wsprintf(f_name,
     "%std_ind.tmp",
     td_maps_dir);
  save_file(f_name);
  do_td_index();
  wsprintf(f_name,
     "%std_ind.new",
     td_maps_dir);
  file_handle = fopen(f_name,
       "rb");
  process_td_index();
  draw_tree();
 }
}
 
BOOL read_smf_file(FILE   *file)
{
 char      buffer[100],
        *ptr,
        *mem;
 unsigned int    knt;
 
 while (!feof(file))
 {
  memset(&buffer,
      0,
      sizeof(buffer));
  fgets(buffer,
     sizeof(buffer),
     file);
 
  memset(&sfl[sfl_count],
      ' ',
      sizeof(struct smf_file_layout));
 
  // Remove the stanox_int 
   sfl[sfl_count].event_time_offset =
   sfl[sfl_count].sort_key = 0;
  for (knt = 0; knt < strlen(buffer); knt++)
  {
 
   if (buffer[knt + 1] == 13 ||
    buffer[knt + 1] == 10)
    break;
   if ((buffer[knt] == ',') &&
    (buffer[knt + 1] == ','))
   {
    memmove(&buffer[knt + 2],
      &buffer[knt + 1],
      strlen(buffer) - knt);
    buffer[knt + 1] = ' ';
   }
  }
  ptr = buffer;
  if ((mem = strtok(ptr,
        ",")) && (mem[0] > ' '))
   memcpy(sfl[sfl_count].line_type,
       mem,
       2);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(&sfl[sfl_count].step_type,
       mem,
       1);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(&sfl[sfl_count].from_berth,
       mem,
       4);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(sfl[sfl_count].to_berth,
       mem,
       4);
 
                // Remove the trust_event, rttm_event, stanox_int,
  // tiploc and direction
 
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
  {
   memcpy(sfl[sfl_count].stanox,
       mem,
       5);
  }
 
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(&sfl[sfl_count].event_type,
       mem,
       1);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(&sfl[sfl_count].thru_line,
       mem,
       1);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(&sfl[sfl_count].platform_id,
       mem,
       1);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   sfl[sfl_count].event_time_offset = atoi(mem);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(&sfl[sfl_count].forward_line,
       mem,
       1);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(&sfl[sfl_count].forward_route,
       mem,
       1);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   sfl[sfl_count].sort_key = atoi(mem);
  sfl_count++;
 }
 return TRUE;
}
 
BOOL write_brt_file(int   tis_ind)
{
 FILE      *brt_file;
 char      f_name[MAX_PATH],
        buffer[100];
 int       cnt,
        last_rec;
 
 wsprintf(f_name,
    "%s%.2s.brt",
    berth_step_dir,
    tds_ive_seen[tis_ind].td_id);
 if (brt_file = fopen(f_name,
       "wb"))
 {
  last_rec = tds_ive_seen[tis_ind].sfl_rec_count + tds_ive_seen[tis_ind].sfl_ind;
  for (cnt = tds_ive_seen[tis_ind].sfl_ind; cnt < last_rec; cnt++)
  {
   wsprintf(buffer,
      "%c%.4s%.4s%.5s     ",
      sfl[cnt].step_type,
      sfl[cnt].from_berth,
      sfl[cnt].to_berth,
      sfl[cnt].stanox);
   fwrite(buffer,
       19,
       1,
       brt_file);
  }
  fclose(brt_file);
 }
 else
 {
  MessageBox(main_handle,
       "Unable to open file for writing",
       f_name,
       MB_ICONERROR | MB_OK);
  return FALSE;
 }
 return TRUE;
}

But now, I need to create output files from the column 1 of the input file and each time the value in column 1 changes,
a new file is created.

So for input file :-

ALLSMFAS.ref
----------------
AD,B,0424,0415,89375,,A,2,,80,,
AD,B,0424,0417,89375,,A,1,,82,,
AD,B,0416,0410,89375,,B,2,,-7,2,
BN,B,0135,0094,65625,,D,,,0,1,H
BN,B,0338,0337,65824,,B,,,-18,1,
BN,B,0334,0339,65824,,C,,,65,,
CA,T,,SREC,47221,,C,,R,0,,
CA,T,,X171,47221,,A,6,,60,,
CA,T,,X173,47221,,A,5,,60,,
CC,B,1783,1574,48231,,C,1,,60,,
CC,B,1783,1576,48231,,C,2,,60,,
CC,B,1783,1578,48231,,C,3,,60,,
:
:
the following needs to be the output files.


AD.brt
------
B0424041589375 B0424041789375 B0416041089375

BN.brt
------
B0135009465625 B0338033765824 B0334033965824

CA.brt
------
T SREC47221 T X17147221 T X17347221

CC.brt
------
B1783157448231 B1783157648231 B1783157848231
and so on ....

In the above code, I am stuck up at the following place, where I believe the logic for reading the first column of the file and for each change in the value, a new file is created has to be put.

Code:
// Take the brt file name from character 1 of the input filename.
    memcpy(this_td,
        wfd.cFileName,
        1);
    this_td[2] = 0;
Request you, if you could please help me with the appropriate code for this.

Thanks a lot.
Marconi.
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Struggling to follow your code. I like to use state machines for this kind of thing; it makes code a lot clearer (well for me anyway).
Anyway, what you're going to have to do is to open the input file and read it line by line, at each line deciding if you're going to use the currently opened BRT file or to create a new one, then derive the BRT filename from the line just read in from the input file.