Exemplo n.º 1
0
char *
find_in_bin(const char * filename)
{
    char * ret = (char *) malloc(sizeof(char) *
        (strlen(get_asf_bin_dir_win()) + strlen(filename) + 5));
    sprintf(ret, "%s%c%s", get_asf_bin_dir_win(), DIR_SEPARATOR, filename);
    return ret;
}
Exemplo n.º 2
0
void process()
{
  clear_results_message();

  child_params_t *cp = MALLOC(sizeof(child_params_t));
  strcpy(cp->in_file, get_string_from_entry("input_file_entry"));

  char *odir = get_string_from_entry("output_directory_entry");
  char *ofile = get_string_from_entry("output_file_entry");

  if (strlen(odir) > 0) {
#ifdef win32
    if (odir[strlen(odir)-1]=='/' || odir[strlen(odir)-1]=='\\')
#else
    if (odir[strlen(odir)-1]=='/')
#endif
      sprintf(cp->out_file, "%s%s", odir, ofile);
    else
      sprintf(cp->out_file, "%s/%s", odir, ofile);
  }
  else {
    strcpy(cp->out_file, ofile);
  }

  if (strlen(cp->in_file)==0) {
    message_box("No input file specified!");
    free(cp);
    return;
  }
  else if (strlen(cp->out_file)==0) {
    message_box("No output file selected!");
    free(cp);
    return;
  }
  else if (!fileExists(cp->in_file)) {
    message_box("Input file \"%s\" not found!", cp->in_file);
    free(cp);
    return;
  }

  int input_format = get_combo_box_item("input_format_combobox");
  int output_format = get_combo_box_item("output_format_combobox");

  if (input_format==INPUT_AUTO) {
    select_defaults_by_file_type(cp->in_file, FALSE);
    input_format = get_combo_box_item("input_format_combobox");
    if (input_format==INPUT_AUTO) {
      message_box("Can't figure out which type of data this is.\n"
                  "Please select the input format manually.");
      free(cp);
      return;
    }
  }

  strcpy(cp->in_format, input_format_to_str(input_format));
  strcpy(cp->out_format, output_format_to_str(output_format));
  char *logFile = appendExt(cp->out_file, ".log");

#ifdef win32
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    memset(&si, 0, sizeof(si));
    memset(&pi, 0, sizeof(pi));
    si.cb = sizeof(si);
    
    char *cmd = MALLOC(sizeof(char)*
        (strlen(cp->in_file) + strlen(cp->out_file) + strlen(get_asf_bin_dir_win()) + 512));
    sprintf(cmd,
        "\"%s/convert2vector.exe\" "
        "-log \"%s\" "
        "-input-format %s -output-format %s \"%s\" \"%s\"",
        get_asf_bin_dir_win(),
        logFile,
        cp->in_format, cp->out_format, cp->in_file, cp->out_file);

    // clear out any previously existing log file
    fLog = fopen(logFile, "a");
    FCLOSE(fLog);

    if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    {
        DWORD dw = GetLastError();
        //printf( "CreateProcess failed (%ld)\n", dw );

        LPVOID lpMsgBuf;
        FormatMessage(
          FORMAT_MESSAGE_ALLOCATE_BUFFER |
          FORMAT_MESSAGE_FROM_SYSTEM |
          FORMAT_MESSAGE_IGNORE_INSERTS,
          NULL,
          dw,
          MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
          (LPTSTR)&lpMsgBuf,
          0,
          NULL);

        printf("CreateProcess() failed with error %ld: %s\n",
            dw, (char*)lpMsgBuf);
        printf("Failed command: %s\n", cmd);
    }

    DWORD dwWaitResult;

    // now wait for process to finish
    do {
        while (gtk_events_pending())
            gtk_main_iteration();
        dwWaitResult = WaitForSingleObject(pi.hProcess, 50);
    }
    while (dwWaitResult == WAIT_TIMEOUT);
#else
  asfFork(child_func, (void*)cp, parent_func, NULL);
#endif

  char message[1024];
  strcpy(message, "-");

  int success=FALSE;
  FILE *lfp = fopen(logFile, "r");
  if (!lfp) {
    sprintf(message, "Error Opening Log File '%s': %s\n",
            logFile, strerror(errno));
  }
  else {
    char *output = NULL;
    char line[1024];
    while (fgets(line, 1024, lfp)) {
      if (output) {
        output = realloc(output, sizeof(char)*(strlen(output)+strlen(line)+1));
        strcat(output, line);
      }
      else {
        output = malloc(sizeof(char)*(strlen(line)+1));
        strcpy(output, line);
      }
    }
    fclose(lfp);
    //unlink(logFile);

    char *p = output, *q;
    while (p) {
      q = strchr(p+1, '\n');
      if (q) {
        *q = '\0';
        if (strstr(p, "Error")!=NULL || strstr(p, "ERROR")!=NULL) {
          *q = '\n';
          int i,n = 0;
          do {
            p = q + 1;
            q = strchr(p, '\n') - 1;
            while (isspace(*q)) --q;
            if (q - p > 2) {
              // First 220 characters of the error string, unless line ends
              // first.  Don't cut off in the middle of a word, though
              strcpy(message, "Error: ");
              strncat(message, p, 220);
              while (isalnum(message[strlen(message)-1]))
                message[strlen(message)-1] = '\0';
              for (n=0; n<strlen(message); ++n)
                if (message[n] == '\n' || message[n] == '*') message[n] = ' ';
              int eating=FALSE;
              for (n=0,i=0; n<strlen(message); ++n) {
                if (isspace(message[n])) {
                  if (!eating) {
                    eating=TRUE;
                    message[i++] = message[n];
                  }
                } else {
                  eating=FALSE;
                  message[i++] = message[n];
                }
              }
              message[i]='\0';
              char *eoe = strstr(message, "End of error");
              if (eoe)
                *eoe = '\0';
              else if (strlen(message)>200)
                strcat(message, " ...");
              break;
            }
          }
          while (*p != '*' && ++n<5); // * flags the end of the error message
        }

        if (strstr(p,"Successful completion!") != NULL) {
          strcpy(message, "Processed successfully!");
          success=TRUE;
          break;
        }

        *q = '\n';
      }
      p=q;
    }

    if (strlen(message)==0) {
      // Did not find an error message, or the success message!
      // So, assume there was an error, but we don't know why
      strcpy(message, "Processing failed!");
    }

    FREE(output);
  }

  int open_output = get_checked("open_output_checkbutton");
  if (!success) open_output = FALSE;

  put_string_to_label("result_label", message);
  asfPrintStatus(message);
  asfPrintStatus("\n\nDone.\n\n");

  if (open_output) {
    switch (output_format) {
      case OUTPUT_KML:
        open_in_google_earth(cp->out_file);
        break;
      case OUTPUT_ALOS_CSV:
        open_in_excel(cp->out_file);
      default:
        // do nothing, output type has no natural associated app
        break;
    }
  }

  free(cp);
}
Exemplo n.º 3
0
static char *
do_convert(int pid, GtkTreeIter *iter, char *cfg_file, int save_dem,
           int keep_files, char **intermediates_file)
{
    FILE *output;
    char *logFile = appendExt(cfg_file, ".log");

    gtk_list_store_set(list_store, iter, COL_STATUS, "Processing...", -1);

#ifdef win32
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    memset(&si, 0, sizeof(si));
    memset(&pi, 0, sizeof(pi));
    si.cb = sizeof(si);

    char *cmd = MALLOC(sizeof(char)*
                       (strlen(cfg_file) + strlen(logFile) +
                        strlen(get_asf_bin_dir_win()) + 256));
    sprintf(cmd, "\"%s/asf_mapready.exe\" %s-log \"%s\" \"%s\"",
        get_asf_bin_dir_win(),
        save_dem ? "--save-dem " : "",
        logFile, cfg_file);

    fLog = fopen(logFile, "a");
    log_summary_text(fLog);
    FCLOSE(fLog);

    //printf("Running command> %s\n", cmd);
    if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
    {
        DWORD dw = GetLastError();
        //printf( "CreateProcess failed (%ld)\n", dw );

        LPVOID lpMsgBuf;
        FormatMessage(
          FORMAT_MESSAGE_ALLOCATE_BUFFER |
          FORMAT_MESSAGE_FROM_SYSTEM |
          FORMAT_MESSAGE_IGNORE_INSERTS,
          NULL,
          dw,
          MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
              (LPTSTR)&lpMsgBuf,
          0,
          NULL);

        printf("CreateProcess() failed with error %ld: %s\n",
            dw, (char*)lpMsgBuf);
        printf("Failed command: %s\n", cmd);
    }

    char *statFile = appendExt(cfg_file, ".status");
    DWORD dwWaitResult;
    int counter = 1;

    // now wait for process to finish
    do {
        while (gtk_events_pending())
            gtk_main_iteration();

        if (++counter % 200 == 0) {
            // check status file
            char buf[256];
            FILE *fStat = fopen(statFile, "r");
            if (fStat)
            {
                fgets(buf, sizeof(buf), fStat);
                fclose(fStat);

                gtk_list_store_set(list_store, iter, COL_STATUS, buf, -1);
            }
        }

        dwWaitResult = WaitForSingleObject(pi.hProcess, 50);
    }
    while (dwWaitResult == WAIT_TIMEOUT);

    remove_file(statFile);
    free(statFile);
    // Don't do this, CreateProcess() takes it
    //free(cmd);
#else
    extern int logflag;
    extern FILE *fLog;

    pid = fork();
    if (pid == 0)
    {
        /* child */
        logflag = TRUE;
        fLog = fopen(logFile, "a");

        asfPrintStatus("Running MapReady with configuration file: %s\n",
                       cfg_file);

        log_summary_text(fLog);

        asf_convert_ext(FALSE, cfg_file, save_dem);

        FCLOSE(fLog);
        exit(EXIT_SUCCESS);
    }
    else
    {
        /* parent */
        int counter = 1;
        char *statFile = appendExt(cfg_file, ".status");
        while (waitpid(pid, NULL, WNOHANG) == 0)
        {
            while (gtk_events_pending())
                gtk_main_iteration();

            g_usleep(50);

            if (++counter % 200 == 0) {
                /* check status file */
                char buf[256];
                FILE *fStat = fopen(statFile, "r");
                if (fStat)
                {
                    if (fgets(buf, sizeof(buf), fStat))
                        fclose(fStat);
                    else
                        strcpy(buf,"");

                    gtk_list_store_set(list_store, iter, COL_STATUS, buf, -1);

                    if (strcmp(buf, "Done")==0 || strcmp(buf, "Error")==0) {
                         // kludge:
                         // Status file says "Done" but we're still here.
                         // This could happen because it *just* finished
                         // during the most recent g_usleep(), but a much more
                         // likely reason is that the waitpid() detection
                         // failed.  (I say "much more likely" because waitpid
                         // is checked 200x more often than the status file)

                         // UPDATE!
                         // We figured out why this is occurring on Linux,
                         // it is a bug in the GtkFileChooser.  Since the
                         // chooser is much nicer than the older FileSelector,
                         // we'll keep this kludge... seems to be no other
                         // side effects... hopefully...

                         // Expanded the kludge to update the status file
                         // with "Error", so that even if the process exits
                         // with an error we'll still be ok.  This only
                         // leaves the core-dump case as a loose end.

#ifdef linux
                         // On Linux (which is actually the only platform
                         // I have seen this problem), we can try to kill
                         // the zombie process.
                         kill(pid, 9);
#endif

                         // We'll break out of the loop now, and
                         // possibly leave a zombie process around, if this
                         // were to occur somewhere other than Linux
                         break;
                     }
                }
            }
        }

        remove_file(statFile);
        free(statFile);
    }
#endif

    gchar *the_output = NULL;
    output = fopen(logFile, "r");

    // see if we got a file containing a list of useful intermediate files
    *intermediates_file = appendExt(cfg_file, ".files");
    if (!fileExists(*intermediates_file)) {
      free(*intermediates_file);
      *intermediates_file = NULL;
    }

    if (!output)
    {
        the_output = (gchar *)g_malloc(512);
        sprintf(the_output, "Error Opening Log File: %s\n", strerror(errno));
    }
    else
    {
        gchar buffer[4096];
        gchar *p = fgets(buffer, sizeof(buffer), output);
        while (!feof(output))
        {
            if (p && !g_str_has_prefix(p, "Processing "))
            {
                if (the_output)
                {
                    the_output = (gchar *)g_realloc(the_output, sizeof(gchar) *
                        (strlen(the_output) + strlen(buffer) + 1));

                    strcat(the_output, buffer);
                }
                else
                {
                    the_output = (gchar *)
                        g_malloc(sizeof(gchar) * (strlen(buffer) + 1));

                    strcpy(the_output, buffer);
                }
            }
            p = fgets(buffer, sizeof(buffer), output);
        }
        fclose(output);

        if (!keep_files)
            remove_file(logFile);
    }

    if (!the_output)
    {
        /* Log file existed, but had immediate EOF */
        /* This is most likely caused by a "Disk Full" situation... */
        /*  ... or a segmentation fault! */
        the_output = g_strdup("Error Opening Log File: Disk Full?\n");
    }

    free(logFile);
    return the_output;
}