int write_local_name(bdaddr_t *bdaddr, const char *name) { char filename[PATH_MAX + 1], str[249]; int i; memset(str, 0, sizeof(str)); for (i = 0; i < 248 && name[i]; i++) if ((unsigned char) name[i] < 32 || name[i] == 127) str[i] = '.'; else str[i] = name[i]; create_filename(filename, PATH_MAX, bdaddr, "config"); create_file(filename, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); return textfile_put(filename, "name", str); }
bool savegame::save_game_automatic(CVideo& video, bool ask_for_overwrite, const std::string& filename) { bool overwrite = true; if (filename == "") create_filename(); else filename_ = filename; if (ask_for_overwrite){ overwrite = check_overwrite(video); if (!overwrite) return save_game_interactive(video, "", true); } return save_game(&video); }
extern int binary_ladder_save(t_binary_ladder_types type, unsigned int paracount, t_cb_get_from_ladder _cb_get_from_ladder) { int results[10]; int rank = 1; const char * ladder_name; const char * filename; int checksum, count; FILE * fp; if ((!(ladder_name = binary_ladder_type_to_filename(type))) || (!(filename = create_filename(prefs_get_ladderdir(),ladder_name,"_LADDER")))) { eventlog(eventlog_level_error,__FUNCTION__,"NULL filename - aborting"); return -1; } if (!(fp = fopen(filename,"wb"))) { eventlog(eventlog_level_error,__FUNCTION__,"could not open file \"%s\" for writing (fopen: %s)",filename,pstrerror(errno)); dispose_filename(filename); return -1; } results[0] = magick; fwrite(results,sizeof(int),1,fp); //write the magick int as header checksum = 0; while ((*_cb_get_from_ladder)(type,rank,results)!=-1) { fwrite(results,sizeof(int),paracount,fp); for (count=0;count<paracount;count++) checksum+=results[count]; rank++; } //calculate a checksum over saved data results[0] = checksum; fwrite(results,sizeof(int),1,fp); // add checksum at the end fclose(fp); dispose_filename(filename); return 0; }
static char *read_proximity_config(bdaddr_t *sba, bdaddr_t *dba, const char *alert) { char filename[PATH_MAX + 1], addr[18], key[38]; char *str, *strnew; create_filename(filename, PATH_MAX, sba, "proximity"); ba2str(dba, addr); snprintf(key, sizeof(key), "%17s#%s", addr, alert); str = textfile_caseget(filename, key); if (str == NULL) return NULL; strnew = g_strdup(str); free(str); return strnew; }
int read_local_name(bdaddr_t *bdaddr, char *name) { char filename[PATH_MAX + 1], *str; int len; create_filename(filename, PATH_MAX, bdaddr, "config"); str = textfile_get(filename, "name"); if (!str) return -ENOENT; len = strlen(str); if (len > HCI_MAX_NAME_LENGTH) str[HCI_MAX_NAME_LENGTH] = '\0'; strcpy(name, str); free(str); return 0; }
parm_result put_string (int name, char *string) { char *filename; FILE *fd; int result = PARM_OK; /* First check input parameters */ if (name == 0 || name >= PAR_END_MARKER || string == NULL) { return -PARM_PARAMETER_ERROR; } /* Make room for our filename */ filename = malloc(MAX_PARAM_FILE_NAME_LENGTH); if (!filename) { printf("Could not allocate memory for file name !\n"); return -PARM_MEMORY; } /* Create filename */ create_filename(type_string, name, filename); /* open parameter file, for writing (overwriting previous value) */ fd = fopen(filename, "w"); if (fd == 0) { printf("Could not open parameter file !\n"); result = -PARM_NOT_FOUND; goto error; } /* Write value to file */ fputs(string, fd); error: if (fd) fclose(fd); if (filename) free(filename); return result; }
parm_result get_string (int name, char *string, int max_len) { char *filename; FILE *fd; /* First check input parameters */ if (name == 0 || name >= PAR_END_MARKER || string == NULL) { return -PARM_PARAMETER_ERROR; } /* Make room for our filename */ filename = malloc(MAX_PARAM_FILE_NAME_LENGTH); if (!filename) { printf("Could not allocate memory for file name !\n"); return -PARM_MEMORY; } /* Create filename */ create_filename(type_string, name, filename); /* open parameter file */ fd = fopen(filename, "r"); if (fd == 0) { printf("Could not find parameter '%d' !\n", name); /* Make sure the empty string is terminated */ *string = 0x00; } else { if (fgets(string, max_len, fd) == NULL) { printf("Could not read parameter '%d' data !\n", name); /* Make sure the empty string is terminated */ *string = 0x00; } else { fclose(fd); } } free(filename); return PARM_OK; }
parm_result get_integer (int name, int *value) { char *filename; FILE *fd; /* First check input parameters */ if (name == 0 || name >= PAR_END_MARKER || value == NULL) { return -PARM_PARAMETER_ERROR; } /* Make room for our filename */ filename = malloc(MAX_PARAM_FILE_NAME_LENGTH); if (!filename) { printf("Could not allocate memory for file name !\n"); return -PARM_MEMORY; } /* Create filename */ create_filename(type_int, name, filename); /* open parameter file */ fd = fopen(filename, "r"); if (fd == 0) { printf("Could not find parameter '%d' !\n", name); free(filename); return -PARM_NOT_FOUND; } if (fscanf(fd, "%d", value) < 0) { printf("Could not read parameter '%d' data !\n", name); fclose(fd); free(filename); return -PARM_FAILED; } fclose(fd); free(filename); return PARM_OK; }
bool DeduplicationService::verifyKey(const std::string &key) { auto I = cache.find(key); auto E = cache.end(); if (I != E) { return I->second; } std::string file = create_filename("lock", servicePath, key); int fd = open(file.c_str(), O_CREAT | O_EXCL, 0644); bool result = (fd > 0); if (result) { close(fd); #ifdef DEBUG // Re-open to write the key being tagged. int fd = open(file.c_str(), O_WRONLY, 0644); dprintf(fd, "%s\n", key.c_str()); close(fd); #endif } cache[key] = result; return result; }
bool Tokenizer<N, P>::open(const Param ¶m) { close(); const std::string prefix = param.template get<std::string>("dicdir"); CHECK_FALSE(unkdic_.open(create_filename (prefix, UNK_DIC_FILE).c_str())) << unkdic_.what(); CHECK_FALSE(property_.open(param)) << property_.what(); Dictionary *sysdic = new Dictionary; CHECK_FALSE(sysdic->open (create_filename(prefix, SYS_DIC_FILE).c_str())) << sysdic->what(); CHECK_FALSE(sysdic->type() == 0) << "not a system dictionary: " << prefix; property_.set_charset(sysdic->charset()); dic_.push_back(sysdic); const std::string userdic = param.template get<std::string>("userdic"); if (!userdic.empty()) { scoped_fixed_array<char, BUF_SIZE> buf; scoped_fixed_array<char *, BUF_SIZE> dicfile; std::strncpy(buf.get(), userdic.c_str(), buf.size()); const size_t n = tokenizeCSV(buf.get(), dicfile.get(), dicfile.size()); for (size_t i = 0; i < n; ++i) { Dictionary *d = new Dictionary; CHECK_FALSE(d->open(dicfile[i])) << d->what(); CHECK_FALSE(d->type() == 1) << "not a user dictionary: " << dicfile[i]; CHECK_FALSE(sysdic->isCompatible(*d)) << "incompatible dictionary: " << dicfile[i]; dic_.push_back(d); } } dictionary_info_ = 0; dictionary_info_freelist_.free(); for (int i = static_cast<int>(dic_.size() - 1); i >= 0; --i) { DictionaryInfo *d = dictionary_info_freelist_.alloc(); d->next = dictionary_info_; d->filename = dic_[i]->filename(); d->charset = dic_[i]->charset(); d->size = dic_[i]->size(); d->lsize = dic_[i]->lsize(); d->rsize = dic_[i]->rsize(); d->type = dic_[i]->type(); d->version = dic_[i]->version(); dictionary_info_ = d; } unk_tokens_.clear(); for (size_t i = 0; i < property_.size(); ++i) { const char *key = property_.name(i); const Dictionary::result_type n = unkdic_.exactMatchSearch(key); CHECK_FALSE(n.value != -1) << "cannot find UNK category: " << key; const Token *token = unkdic_.token(n); size_t size = unkdic_.token_size(n); unk_tokens_.push_back(std::make_pair(token, size)); } space_ = property_.getCharInfo(0x20); // ad-hoc bos_feature_.reset_string(param.template get<std::string>("bos-feature")); const std::string tmp = param.template get<std::string>("unk-feature"); unk_feature_.reset(0); if (!tmp.empty()) { unk_feature_.reset_string(tmp); } CHECK_FALSE(*bos_feature_ != '\0') << "bos-feature is undefined in dicrc"; max_grouping_size_ = param.template get<size_t>("max-grouping-size"); if (max_grouping_size_ == 0) { max_grouping_size_ = DEFAULT_MAX_GROUPING_SIZE; } return true; }
void export_gpx(TTBIN_FILE *ttbin, FILE *file) { uint32_t i; char timestr[32]; if (!ttbin->gps_records) return; fputs("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n" "<gpx xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" " xsi:schemaLocation=\"http://www.topografix.com/GPX/1/1" " http://www.topografix.com/GPX/1/1/gpx.xsd" " http://www.garmin.com/xmlschemas/GpxExtensions/v3" " http://www.garmin.com/xmlschemas/GpxExtensionsv3.xsd" " http://www.garmin.com/xmlschemas/TrackPointExtension/v1" " http://www.garmin.com/xmlschemas/TrackPointExtensionv1.xsd\"" " xmlns:gpxx=\"http://www.garmin.com/xmlschemas/GpxExtensions/v3\"" " xmlns:gpxtpx=\"http://www.garmin.com/xmlschemas/TrackPointExtension/v1\"" " version=\"1.1\" creator=\"TomTom\" xmlns=\"http://www.topografix.com/GPX/1/1\">\r\n" " <metadata>\r\n <name>", file); fputs(create_filename(ttbin, "gpx"), file); fputs("</name>\r\n </metadata>\r\n" " <trk>\r\n <name>", file); switch(ttbin->activity) { case ACTIVITY_RUNNING: fputs("RUNNING", file); break; case ACTIVITY_CYCLING: fputs("CYCLING", file); break; case ACTIVITY_SWIMMING: fputs("POOL SWIM", file); break; case ACTIVITY_TREADMILL: fputs("TREADMILL", file); break; case ACTIVITY_FREESTYLE: fputs("FREESTYLE", file); break; default: fputs("UNKNOWN", file); break; } fputs("</name>\r\n <trkseg>\r\n", file); for (i = 0; i < ttbin->gps_record_count; ++i) { if ((ttbin->gps_records[i].timestamp != 0) && !((ttbin->gps_records[i].latitude == 0) && (ttbin->gps_records[i].longitude == 0))) { strftime(timestr, sizeof(timestr), "%FT%X.000Z", gmtime(&ttbin->gps_records[i].timestamp)); fprintf(file, " <trkpt lon=\"%.6f\" lat=\"%.6f\">\r\n", ttbin->gps_records[i].longitude, ttbin->gps_records[i].latitude); fprintf(file, " <ele>%d</ele>\r\n", (int)ttbin->gps_records[i].elevation); fputs( " <time>", file); fputs(timestr, file); fputs("</time>\r\n", file); if ((i < ttbin->heart_rate_record_count) && (ttbin->heart_rate_records[i].heart_rate > 0)) { fputs(" <extensions>\r\n" " <gpxtpx:TrackPointExtension>\r\n", file); fprintf(file, " <gpxtpx:hr>%d</gpxtpx:hr>\r\n", ttbin->heart_rate_records[i].heart_rate); fputs(" </gpxtpx:TrackPointExtension>\r\n" " </extensions>\r\n", file); } fputs( " </trkpt>\r\n", file); } } fputs( " </trkseg>\r\n </trk>\r\n</gpx>\r\n", file); }
bool load_dictionary_resource(Param *param) { std::string rcfile = param->get<std::string>("rcfile"); #ifdef HAVE_GETENV if (rcfile.empty()) { const char *homedir = getenv("HOME"); if (homedir) { std::string s = MeCab::create_filename(std::string(homedir), ".mecabrc"); std::ifstream ifs(s.c_str()); if (ifs) rcfile = s; } } if (rcfile.empty()) { const char *rcenv = getenv("MECABRC"); if (rcenv) rcfile = rcenv; } #endif #if defined (HAVE_GETENV) && defined(_WIN32) && !defined(__CYGWIN__) if (rcfile.empty()) { char buf[BUF_SIZE]; DWORD len = GetEnvironmentVariable("MECABRC", buf, sizeof(buf)); if (len < sizeof(buf) && len > 0) { rcfile = buf; } } #endif #if defined(_WIN32) && !defined(__CYGWIN__) HKEY hKey; char v[BUF_SIZE]; DWORD vt; DWORD size = sizeof(v); if (rcfile.empty()) { RegOpenKeyEx(HKEY_LOCAL_MACHINE, "software\\mecab", 0, KEY_READ, &hKey); RegQueryValueEx(hKey, "mecabrc", 0, &vt, reinterpret_cast<BYTE *>(v), &size); RegCloseKey(hKey); if (vt == REG_SZ) rcfile = v; } if (rcfile.empty()) { RegOpenKeyEx(HKEY_CURRENT_USER, "software\\mecab", 0, KEY_READ, &hKey); RegQueryValueEx(hKey, "mecabrc", 0, &vt, reinterpret_cast<BYTE *>(v), &size); RegCloseKey(hKey); if (vt == REG_SZ) rcfile = v; } /* for Open JTalk if (rcfile.empty()) { vt = GetModuleFileName(DllInstance, v, size); if (vt != 0) { char drive[_MAX_DRIVE]; char dir[_MAX_DIR]; _splitpath(v, drive, dir, NULL, NULL); std::string s = std::string(drive) + std::string(dir) + std::string("mecabrc"); std::ifstream ifs(s.c_str()); if (ifs) rcfile = s; } } */ #endif /* for Open JTalk if (rcfile.empty()) rcfile = MECAB_DEFAULT_RC; if (!param->load(rcfile.c_str())) return false; */ std::string dicdir = param->get<std::string>("dicdir"); if (dicdir.empty()) dicdir = "."; // current remove_filename(&rcfile); replace_string(&dicdir, "$(rcpath)", rcfile); param->set<std::string>("dicdir", dicdir, true); dicdir = create_filename(dicdir, DICRC); if (!param->load(dicdir.c_str())) return false; return true; }
extern t_binary_ladder_load_result binary_ladder_load(t_binary_ladder_types type, unsigned int paracount, t_cb_add_to_ladder _cb_add_to_ladder) { int values[10]; const char * ladder_name; const char * filename; int checksum, count; FILE * fp; //TODO: load from file and if this fails return binary_ladder_load_failed // then make sure ladder gets loaded somehow else (form accounts) // compare checksum - and if it differs return load_invalid // then make sure ladder gets flushed and then loaded from accounts // on success don't load from accounts if ((!(ladder_name = binary_ladder_type_to_filename(type))) || (!(filename = create_filename(prefs_get_ladderdir(),ladder_name,"_LADDER")))) { eventlog(eventlog_level_error,__FUNCTION__,"NULL filename - aborting"); return load_failed; } if (!(fp = fopen(filename,"rb"))) { eventlog(eventlog_level_info,__FUNCTION__,"could not open ladder file \"%s\" - maybe ladder still empty",filename,pstrerror(errno)); dispose_filename(filename); return load_failed; } if ((fread(values,sizeof(int),1,fp)!=1) || (values[0]!=magick)) { eventlog(eventlog_level_error,__FUNCTION__,"ladder file not starting with the magick int"); dispose_filename(filename); fclose(fp); return load_failed; } checksum = 0; while (fread(values,sizeof(int),paracount,fp)==paracount) { (*_cb_add_to_ladder)(type,values); for (count=0;count<paracount;count++) checksum+=values[count]; } fread(values,sizeof(int),1,fp); if (feof(fp)==0) { eventlog(eventlog_level_error,__FUNCTION__,"got data past end.. fall back to old loading mode"); return illegal_checksum; } if (values[0]!=checksum) { eventlog(eventlog_level_error,__FUNCTION__,"ladder file has invalid checksum... fall back to old loading mode"); return illegal_checksum; } fclose(fp); eventlog(eventlog_level_info,__FUNCTION__,"successfully loaded %s",filename); dispose_filename(filename); return load_success; }
bool CharProperty::open(const Param ¶m) { const std::string prefix = param.get<std::string>("dicdir"); const std::string filename = create_filename(prefix, CHAR_PROPERTY_FILE); return open(filename.c_str()); }
int main(int argc, char *argv[]) { uint32_t formats = 0; int pipe_mode = 0; int set_laps = 0; int download_elevation = 1; char *lap_definitions = 0; FILE *input_file = 0; TTBIN_FILE *ttbin = 0; unsigned i; int opt = 0; int option_index = 0; /* create the options lists */ #define OPTION_COUNT (OFFLINE_FORMAT_COUNT + 5) struct option long_options[OPTION_COUNT] = { { "help", no_argument, 0, 'h' }, { "all", no_argument, 0, 'a' }, { "laps", required_argument, 0, 'l' }, { "no-elevation", no_argument, 0, 'E' }, }; char short_options[OPTION_COUNT + 1] = "hl:aE"; opt = 4; for (i = 0; i < OFFLINE_FORMAT_COUNT; ++i) { if (OFFLINE_FORMATS[i].producer) { long_options[opt].name = OFFLINE_FORMATS[i].name; long_options[opt].has_arg = no_argument; long_options[opt].flag = 0; long_options[opt].val = OFFLINE_FORMATS[i].name[0]; short_options[opt++ + 1] = OFFLINE_FORMATS[i].name[0]; } } while (opt < OPTION_COUNT) { memset(&long_options[opt], 0, sizeof(struct option)); short_options[opt++ + 1] = 0; } /* check the command line options */ while ((opt = getopt_long(argc, argv, short_options, long_options, &option_index)) != -1) { switch (opt) { case 'h': /* help */ help(argv); return 0; case 'l': /* set lap list */ set_laps = 1; lap_definitions = optarg; break; case 'a': /* all supported formats */ formats = 0xffffffff; break; case 'E': /* no elevation */ download_elevation = 0; break; default: for (i = 0; i < OFFLINE_FORMAT_COUNT; ++i) { if (opt == OFFLINE_FORMATS[i].name[0]) { formats |= OFFLINE_FORMATS[i].mask; break; } } break; } } /* check that we actually have to do something */ if (!formats) { help(argv); return 0; } pipe_mode = (optind >= argc); /* make sure we've only got one output format specified if we're operating as a pipe */ if (pipe_mode && (formats & (formats - 1))) { fprintf(stderr, "Only one output format can be specified in pipe mode\n"); return 4; } /* open the input file */ if (!pipe_mode) { input_file = fopen(argv[optind], "r"); if (!input_file) { fprintf(stderr, "Unable to open input file: %s\n", argv[optind]); return 3; } } else input_file = stdin; /* read the ttbin data file */ ttbin = read_ttbin_file(input_file); if (input_file != stdin) fclose(input_file); if (!ttbin) { fprintf(stderr, "Unable to read and parse TTBIN file\n"); return 5; } /* if we have gps data, download the elevation data */ if (ttbin->gps_records.count && download_elevation) download_elevation_data(ttbin); /* set the list of laps if we have been asked to */ if (set_laps) do_replace_lap_list(ttbin, lap_definitions); /* write the output files */ for (i = 0; i < OFFLINE_FORMAT_COUNT; ++i) { if ((formats & OFFLINE_FORMATS[i].mask) && OFFLINE_FORMATS[i].producer) { if ((OFFLINE_FORMATS[i].gps_ok && ttbin->gps_records.count) || (OFFLINE_FORMATS[i].treadmill_ok && ttbin->activity==ACTIVITY_TREADMILL) || (OFFLINE_FORMATS[i].pool_swim_ok && ttbin->activity==ACTIVITY_SWIMMING)) { FILE *output_file = stdout; if (!pipe_mode) { const char *filename = create_filename(ttbin, OFFLINE_FORMATS[i].name); output_file = fopen(filename, "w"); if (!output_file) fprintf(stderr, "Unable to create output file: %s\n", filename); } if (output_file) { (*OFFLINE_FORMATS[i].producer)(ttbin, output_file); if (output_file != stdout) fclose(output_file); } } else fprintf(stderr, "Unable to process output format: %s\n", OFFLINE_FORMATS[i].name); } } free_ttbin(ttbin); return 0; }
bool load_dictionary_resource(Param *param) { //debug std::cout << "[" << __FILE__ << ":" << __LINE__ << "]: " << "load_dictionary_resource(Param *param)" << std::endl; /// std::string rcfile = param->get<std::string>("rcfile"); #ifdef HAVE_GETENV if (rcfile.empty()) { //debug std::cout << "[" << __FILE__ << ":" << __LINE__ << "]: " << "rcfile.empty()" << std::endl; /// const char *homedir = getenv("HOME"); if (homedir) { const std::string s = MeCab::create_filename(std::string(homedir), ".mecabrc"); std::ifstream ifs(WPATH(s.c_str())); if (ifs) { rcfile = s; } } } if (rcfile.empty()) { const char *rcenv = getenv("MECABRC"); if (rcenv) { rcfile = rcenv; } } #endif #if defined (HAVE_GETENV) && defined(_WIN32) && !defined(__CYGWIN__) if (rcfile.empty()) { scoped_fixed_array<wchar_t, BUF_SIZE> buf; const DWORD len = ::GetEnvironmentVariableW(L"MECABRC", buf.get(), buf.size()); if (len < buf.size() && len > 0) { rcfile = WideToUtf8(buf.get()); } } #endif #if defined(_WIN32) && !defined(__CYGWIN__) HKEY hKey; scoped_fixed_array<wchar_t, BUF_SIZE> v; DWORD vt; DWORD size = v.size() * sizeof(v[0]); if (rcfile.empty()) { ::RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"software\\mecab", 0, KEY_READ, &hKey); ::RegQueryValueExW(hKey, L"mecabrc", 0, &vt, reinterpret_cast<BYTE *>(v.get()), &size); ::RegCloseKey(hKey); if (vt == REG_SZ) { rcfile = WideToUtf8(v.get()); } } if (rcfile.empty()) { ::RegOpenKeyExW(HKEY_CURRENT_USER, L"software\\mecab", 0, KEY_READ, &hKey); ::RegQueryValueExW(hKey, L"mecabrc", 0, &vt, reinterpret_cast<BYTE *>(v.get()), &size); ::RegCloseKey(hKey); if (vt == REG_SZ) { rcfile = WideToUtf8(v.get()); } } if (rcfile.empty()) { vt = ::GetModuleFileNameW(DllInstance, v.get(), size); if (vt != 0) { scoped_fixed_array<wchar_t, _MAX_DRIVE> drive; scoped_fixed_array<wchar_t, _MAX_DRIVE> dir; _wsplitpath(v.get(), drive.get(), dir.get(), NULL, NULL); const std::wstring path = std::wstring(drive.get()) + std::wstring(dir.get()) + L"mecabrc"; if (::GetFileAttributesW(path.c_str()) != -1) { rcfile = WideToUtf8(path); } } } #endif if (rcfile.empty()) { rcfile = MECAB_DEFAULT_RC; } if (!param->load(rcfile.c_str())) { return false; } std::string dicdir = param->get<std::string>("dicdir"); if (dicdir.empty()) { dicdir = "."; // current } remove_filename(&rcfile); replace_string(&dicdir, "$(rcpath)", rcfile); param->set<std::string>("dicdir", dicdir, true); dicdir = create_filename(dicdir, DICRC); if (!param->load(dicdir.c_str())) { return false; } return true; }
void TranslationService::recordCopiedFile(const std::string &copiedPath, const std::string &realPath) { std::string file = create_filename("copy", servicePath, copiedPath); int fd = open(file.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0644); dprintf(fd, "%s\n", realPath.c_str()); close(fd); }
std::string create_filespec (const std::string& directory, const std::string& basename, const std::string& extension) { return create_filespec(directory, create_filename(basename, extension)); }