void cquotestring(Bytebuffer* databuf, char quote) { char* escaped = escapify(bbContents(databuf),'"',bbLength(databuf)); bbClear(databuf); bbAppend(databuf,quote); bbCat(databuf,escaped); bbAppend(databuf,quote); }
static char * find_in_share(const char * filename) { char * escaped_dir = escapify(get_asf_share_dir()); char * ret = (char *) MALLOC(sizeof(char) * (strlen(escaped_dir) + strlen(filename) + 2)); sprintf(ret, "%s/%s", escaped_dir, filename); free(escaped_dir); return ret; }
/** * @internal Build _NCProperties attribute value. * * Convert a NCPROPINFO instance to a single string. * * @param info Properties info. * @param propdatap Pointer that gets properties string. * * @return ::NC_NOERR No error. * @return ::NC_EINVAL failed. * @author Dennis Heimbigner */ int NC4_buildpropinfo(struct NCPROPINFO* info, char** propdatap) { int stat = NC_NOERR; int i; NCbytes* buffer = NULL; char sversion[64]; LOG((3, "%s", __func__)); if(info == NULL || info->version == 0 || propdatap == NULL) {stat = NC_EINVAL; goto done;} *propdatap = NULL; buffer = ncbytesnew(); if(!buffer) {stat = NC_ENOMEM; goto done;} /* start with version */ ncbytescat(buffer,NCPVERSION); ncbytesappend(buffer,'='); snprintf(sversion,sizeof(sversion),"%d",info->version); ncbytescat(buffer,sversion); for(i=0;i<nclistlength(info->properties);i+=2) { char* value, *name; name = nclistget(info->properties,i); if(name == NULL) continue; value = nclistget(info->properties,i+1); ncbytesappend(buffer,NCPROPSSEP2); /* terminate last entry */ escapify(buffer,name); ncbytesappend(buffer,'='); escapify(buffer,value); } /* Force null termination */ ncbytesnull(buffer); *propdatap = ncbytesextract(buffer); done: if(buffer != NULL) ncbytesfree(buffer); return stat; }
// try to find a program we can use to view the generated csv files const char * detect_csv_assoc() { static char *csv_app = NULL; if (!csv_app) { char *csv_file = find_in_share("asf_view_cfg.csv"); #ifdef win32 // On Windows, use the file association table to find out what we // can do with csv files. char path[1024]; int ret = (int)FindExecutable((LPCTSTR)csv_file, (LPCTSTR)get_asf_share_dir(), (LPTSTR)path); if (ret > 32 && strlen(path) > 0) { csv_app = escapify(path); printf("Path to CSV Application: %s\n", csv_app); } else { if (ret==SE_ERR_FNF) printf("File not found: %s\n", csv_file); else if (ret==SE_ERR_NOASSOC) printf("No association for: %s\n", csv_file); else if (ret==SE_ERR_OOM) printf("Out of resources.\n"); else printf("Unknown error! (return value: %d)\n", ret); csv_app = STRDUP("notepad.exe"); printf("CSV Application not found -- using notepad.\n"); } FREE(csv_file); #else // On Linux, get the app from the configuration file FILE *cfg = fopen(csv_file, "r"); if (cfg) { char tmp[1024]; while (fgets(tmp, 1024, cfg) != NULL) { if (strncmp_case(tmp, "CSV,", 4) == 0) { csv_app = trim_whitespace(tmp+4); printf("CSV Application from config file: %s\n", csv_app); } } if (!csv_app) csv_app = STRDUP(""); fclose(cfg); } else { printf("Failed to open %s: %s\n", csv_file, strerror(errno)); csv_app = STRDUP(""); printf("CSV Application not found.\n"); } #endif } return csv_app; }
static void mdv_thread (GString *file, gpointer user_data) { #ifdef win32 gchar * mdv = find_in_bin("mdv.exe"); #else gchar * mdv = find_in_bin("mdv"); #endif char buf[1024]; char *escaped_str = escapify(file->str); sprintf(buf, "\"%s\" \"%s\"", mdv, escaped_str); free(escaped_str); asfSystem(buf); g_string_free(file, TRUE); }
int open_google_earth() { char *kml_filename = appendExt(curr->filename, ".kml"); char *basename = get_basename(curr->filename); char *dirname = get_dirname(curr->filename); char *arg; if (strlen(dirname)==0) { char *tmpdir = g_get_current_dir(); dirname = escapify(tmpdir); arg = MALLOC(sizeof(char)*(strlen(dirname)+strlen(kml_filename)+20)); sprintf(arg, "%s/%s", dirname, kml_filename); //free(tmpdir); } else { arg = STRDUP(kml_filename); } char *png_file = appendExt(arg, ".png"); printf("png file: %s\n", png_file); printf("Temporary kml file: %s\n", arg); FILE *kml_file = fopen(arg, "w"); if (!kml_file) { asfPrintWarning("Couldn't open kml file!\n"); return FALSE; } dbf_header_t *dbf; int nAttr, nCoords; double *lat, *lon, center_lat, center_lon; char configFile[255], *name; meta_parameters *meta; sprintf(configFile, "%s/convert2vector.config", get_asf_share_dir()); c2v_config *cfg = read_c2v_config(configFile); kml_header(kml_file); meta = meta2vector(curr->filename, &dbf, &nAttr, &lat, &lon, &nCoords); //meta_parameters *meta = curr->meta; if (meta && meta->general && meta_is_valid_double(meta->general->center_latitude) && meta_is_valid_double(meta->general->center_longitude)) { pixbuf2png(pixbuf_small, png_file); //kml_entry_with_overlay(kml_file, meta, basename, png_file, dirname); name = get_basename(kml_filename); center_lat = meta->general->center_latitude; center_lon = meta->general->center_longitude; write_kml_placemark(kml_file, name, center_lat, center_lon, png_file, dbf, nAttr, lat, lon, nCoords, cfg); FREE(lat); FREE(lon); FREE(dbf); } else { asfPrintWarning( "Failed, metadata doesn't contain valid lat/lon info.\n"); return FALSE; } kml_footer(kml_file); fclose(kml_file); gchar *ge; printf("kml: %s\n", kml_filename); printf("dir: %s\n", dirname); #ifdef win32 char path[1024]; FindExecutable((LPCTSTR)kml_filename, (LPCTSTR)dirname, (LPTSTR)path); ge = escapify(path); printf("Path to google earth: %s\n", ge); asfSystem("\"%s\" \"%s\"", ge, arg); #else ge = find_in_path("googleearth"); if (!ge) { message_box("Couldn't find googleearth! Is it installed?"); return FALSE; } int pid = fork(); if (pid == 0) { asfSystem("\"%s\" \"%s\"", ge, arg); //unlink(kml_filename); exit(EXIT_SUCCESS); } #endif free(kml_filename); free(basename); free(dirname); free(arg); return TRUE; }
char* escapifyname(char* s0) { return escapify(s0,'"',strlen(s0)); }
static int j_constant(Generator* generator, Symbol* sym, NCConstant* con, Bytebuffer* buf,...) { Bytebuffer* codetmp = bbNew(); char* special = NULL; switch (con->nctype) { case NC_CHAR: if(con->value.charv == '\'') bbprintf(codetmp,"'\\''"); else bbprintf(codetmp,"'%c'",con->value.charv); break; case NC_BYTE: bbprintf(codetmp,"%hhd",con->value.int8v); break; case NC_SHORT: bbprintf(codetmp,"%hd",con->value.int16v); break; case NC_INT: bbprintf(codetmp,"%d",con->value.int32v); break; case NC_FLOAT: /* Special case for nan */ if(isnan(con->value.floatv)) bbprintf(codetmp,"Float.NaN"); else bbprintf(codetmp,"%f",con->value.floatv); break; case NC_DOUBLE: /* Special case for nan */ if(isnan(con->value.doublev)) bbprintf(codetmp,"Double.NaN"); else bbprintf(codetmp,"%lf",con->value.doublev); break; case NC_UBYTE: bbprintf(codetmp,"%hhu",con->value.uint8v); break; case NC_USHORT: bbprintf(codetmp,"%hu",con->value.uint16v); break; case NC_UINT: bbprintf(codetmp,"%uU",con->value.uint32v); break; case NC_INT64: bbprintf(codetmp,"%lldLL",con->value.int64v); break; case NC_UINT64: bbprintf(codetmp,"%lluLLU",con->value.uint64v); break; case NC_STRING: { /* handle separately */ char* escaped = escapify(con->value.stringv.stringv, '"',con->value.stringv.len); special = poolalloc(1+2+strlen(escaped)); strcpy(special,"\""); strcat(special,escaped); strcat(special,"\""); } break; default: PANIC1("ncstype: bad type code: %d",con->nctype); } if(special == NULL) bbCatbuf(buf,codetmp); else bbCat(buf,special); bbFree(codetmp); return 1; }
static int c_constant(Generator* generator, NCConstant* con, Bytebuffer* buf,...) { Bytebuffer* codetmp = bbNew(); char* special = NULL; switch (con->nctype) { case NC_CHAR: if(con->value.charv == '\'') bbprintf(codetmp,"'\\''"); else bbprintf(codetmp,"'%s'",cescapifychar(con->value.charv,'\'')); break; case NC_BYTE: bbprintf(codetmp,"%hhd",con->value.int8v); break; case NC_SHORT: bbprintf(codetmp,"%hd",con->value.int16v); break; case NC_INT: bbprintf(codetmp,"%d",con->value.int32v); break; case NC_FLOAT: /* Special case for nanf */ if(isnan(con->value.floatv)) bbprintf(codetmp,"nanf"); else bbprintf(codetmp,"%f",con->value.floatv); break; case NC_DOUBLE: /* Special case for nan */ if(isnan(con->value.doublev)) bbprintf(codetmp,"nan"); else bbprintf(codetmp,"%lf",con->value.doublev); break; case NC_UBYTE: bbprintf(codetmp,"%hhu",con->value.uint8v); break; case NC_USHORT: bbprintf(codetmp,"%hu",con->value.uint16v); break; case NC_UINT: bbprintf(codetmp,"%uU",con->value.uint32v); break; case NC_INT64: bbprintf(codetmp,"%lldLL",con->value.int64v); break; case NC_UINT64: bbprintf(codetmp,"%lluLLU",con->value.uint64v); break; case NC_ECONST: bbprintf(codetmp,"%s",cname(con->value.enumv)); break; case NC_NIL: case NC_STRING: { /* handle separately */ if(con->value.stringv.len == 0 && con->value.stringv.stringv == NULL) { bbprintf(codetmp,"NULL"); } else { char* escaped = escapify(con->value.stringv.stringv, '"',con->value.stringv.len); special = poolalloc(1+2+strlen(escaped)); strcpy(special,"\""); strcat(special,escaped); strcat(special,"\""); } } break; case NC_OPAQUE: { char* p; int bslen; bslen=(4*con->value.opaquev.len); special = poolalloc(bslen+2+1); strcpy(special,"\""); p = con->value.opaquev.stringv; while(*p) { strcat(special,"\\x"); strncat(special,p,2); p += 2; } strcat(special,"\""); } break; default: PANIC1("ncstype: bad type code: %d",con->nctype); } if(special == NULL) bbCatbuf(buf,codetmp); else bbCat(buf,special); bbFree(codetmp); return 1; }
int open_google_earth() { char *kml_filename = appendExt(curr->filename, ".kml"); char *basename = get_basename(curr->filename); char *dirname = get_dirname(curr->filename); char *arg; if (strlen(dirname)==0) { char *tmpdir = g_get_current_dir(); dirname = escapify(tmpdir); arg = MALLOC(sizeof(char)*(strlen(dirname)+strlen(kml_filename)+20)); sprintf(arg, "%s/%s", dirname, kml_filename); //free(tmpdir); } else { arg = STRDUP(kml_filename); } char *png_file = appendExt(arg, ".png"); printf("png file: %s\n", png_file); printf("Temporary kml file: %s\n", arg); FILE *kml_file = fopen(arg, "w"); if (!kml_file) { asfPrintWarning("Couldn't open kml file!\n"); return FALSE; } kml_header(kml_file); meta_parameters *meta = curr->meta; if (meta && meta->general && meta_is_valid_double(meta->general->center_latitude) && meta_is_valid_double(meta->general->center_longitude)) { pixbuf2png(pixbuf_small, png_file); kml_entry_with_overlay(kml_file, meta, basename, png_file, dirname); } else { asfPrintWarning( "Failed, metadata doesn't contain valid lat/lon info.\n"); return FALSE; } kml_footer(kml_file); fclose(kml_file); gchar *ge; printf("kml: %s\n", kml_filename); printf("dir: %s\n", dirname); #ifdef win32 char path[1024]; FindExecutable((LPCTSTR)kml_filename, (LPCTSTR)dirname, (LPTSTR)path); ge = escapify(path); printf("Path to google earth: %s\n", ge); asfSystem("\"%s\" \"%s\"", ge, arg); #else ge = find_in_path("googleearth"); if (!ge) { message_box("Couldn't find googleearth! Is it installed?"); return FALSE; } int pid = fork(); if (pid == 0) { asfSystem("\"%s\" \"%s\"", ge, arg); //unlink(kml_filename); exit(EXIT_SUCCESS); } #endif free(kml_filename); free(basename); free(dirname); free(arg); return TRUE; }
SIGNAL_CALLBACK void on_help_button_clicked(GtkWidget *widget) { #ifdef win32 char hh[1024]; // First method, get location of help viewer from the standard registry location get_string_from_registry_ex("SOFTWARE\\Classes\\chm.file\\shell\\open\\command", "", hh); if (strlen(hh) > 0) { char *p = strstr(hh, "%1"); if (p) *p = '\0'; char * escaped_share_dir = escapify(get_asf_share_dir()); strcat(hh, escaped_share_dir); strcat(hh, "/mapready.chm"); FREE(escaped_share_dir); } // Failed to find location of hh.exe through registry... try the system directory char *sr = getenv("SYSTEMROOT"); if (strlen(sr) > 0) { int i, j = 0; for (i = 0; i < strlen(sr); ++i) { switch(sr[i]) { case '\\': hh[j] = '\\'; hh[j+1] = '\\'; ++j; break; default: hh[j] = sr[i]; break; } ++j; } hh[j] = '\0'; strcat(hh, "/hh.exe "); char * escaped_share_dir = escapify(get_asf_share_dir()); strcat(hh, escaped_share_dir); strcat(hh, "/mapready.chm"); FREE(escaped_share_dir); } if (strlen(hh)>0) { static GThreadPool *ttp = NULL; GError *err = NULL; if (!ttp) { if (!g_thread_supported ()) g_thread_init (NULL); ttp = g_thread_pool_new ((GFunc)help_viewer_thread, NULL, 4, TRUE, &err); g_assert(!err); } g_thread_pool_push (ttp, g_string_new (hh), &err); g_assert(!err); } else { // Failed, give up. message_box("Couldn't find the help viewer!"); } #else GtkWidget *help_dialog; GtkWidget *help_text; GtkTextBuffer * text_buffer; FILE * help_file; gchar * help_filename; help_dialog = get_widget_checked("help_dialog"); help_text = get_widget_checked("help_text"); text_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(help_text)); gtk_text_buffer_set_text(text_buffer, "", -1); help_filename = "mapready.txt"; //help_file = fopen(help_filename, "rt"); help_file = fopen_share_file(help_filename, "rt"); if (help_file) { int line_count = 0; gchar * buffer = (gchar *) g_malloc(sizeof(gchar) * max_line_len); while (!feof(help_file)) { gchar *p = fgets(buffer, max_line_len, help_file); if (p) { if (strlen(p)) line_count++; GtkTextIter end; gchar * q = strstr(buffer, "$VERSION"); if (q) { gchar * r = g_strdup(q + 8); /* 8 = length of '$VERSION' */ strcpy(q, MAPREADY_VERSION_STRING); strcat(buffer, r); g_free(r); } gtk_text_buffer_get_end_iter(text_buffer, &end); gtk_text_buffer_insert(text_buffer, &end, buffer, -1); } } if (!line_count) { sprintf(buffer, "\n\n ERROR: Empty help file (mapready.txt) in share folder\n(%s)\n", get_asf_share_dir()); GtkTextIter end; gtk_text_buffer_get_end_iter(text_buffer, &end); gtk_text_buffer_insert(text_buffer, &end, buffer, -1); } fclose(help_file); g_free(buffer); } else { // No help file found gchar *buffer = (gchar *) g_malloc(sizeof(gchar) * max_line_len); strcpy(buffer, "\n\n ERROR: Cannot find help file (mapready.txt) in share folder.\n"); GtkTextIter end; gtk_text_buffer_get_end_iter(text_buffer, &end); gtk_text_buffer_insert(text_buffer, &end, buffer, -1); g_free(buffer); } gtk_widget_show(help_dialog); #endif }
/* Result is a pool string or a constant => do not free*/ char* cdata_const(Constant* ci) { Bytebuffer* codetmp = bbNew(); char* result; switch (ci->nctype) { case NC_CHAR: { char tmp[64]; tmp[0] = '\0'; escapifychar(ci->value.charv,tmp,'\''); bbCat(codetmp,"'"); bbCat(codetmp,tmp); bbCat(codetmp,"'"); } break; case NC_BYTE: bbprintf(codetmp,"%hhd",ci->value.int8v); break; case NC_SHORT: bbprintf(codetmp,"%hd",ci->value.int16v); break; case NC_INT: bbprintf(codetmp,"%d",ci->value.int32v); break; case NC_FLOAT: bbprintf(codetmp,"%f",ci->value.floatv); break; case NC_DOUBLE: bbprintf(codetmp,"%lf",ci->value.doublev); break; case NC_UBYTE: bbprintf(codetmp,"%hhu",ci->value.uint8v); break; case NC_USHORT: bbprintf(codetmp,"%hu",ci->value.uint16v); break; case NC_UINT: bbprintf(codetmp,"%uU",ci->value.uint32v); break; case NC_INT64: bbprintf(codetmp,"%lldLL",ci->value.int64v); break; case NC_UINT64: bbprintf(codetmp,"%lluLLU",ci->value.uint64v); break; case NC_ECONST: bbprintf(codetmp,"%s",cname(ci->value.enumv)); break; case NC_STRING: { /* handle separately */ char* escaped = escapify(ci->value.stringv.stringv, '"',ci->value.stringv.len); result = poolalloc(1+2+strlen(escaped)); strcpy(result,"\""); strcat(result,escaped); strcat(result,"\""); goto done; } break; case NC_OPAQUE: { char* p; int bslen; bslen=(4*ci->value.opaquev.len); result = poolalloc(bslen+2+1); strcpy(result,"\""); p = ci->value.opaquev.stringv; while(*p) { strcat(result,"\\x"); strncat(result,p,2); p += 2; } strcat(result,"\""); goto done; } break; default: PANIC1("ncstype: bad type code: %d",ci->nctype); } result = pooldup(bbContents(codetmp)); /*except for NC_STRING and NC_OPAQUE*/ bbFree(codetmp); done: return result; }