void pressed_file_configure() { spawn_dd tdata; spawn_row *rp; char txt[64], *s; int i, j; rp = tdata.strs = calloc(FACTION_ROWS_TOTAL, sizeof(spawn_row)); if (!rp) return; // Not enough memory for (i = 1; i <= FACTION_ROWS_TOTAL; i++ , rp++) { for (j = 0; j < 3; j++) { sprintf(txt, faction_ini[j], i); s = inifile_get(txt, ""); if (!j) strncpy0(rp->name, s, sizeof(rp->name)); else if (j == 1) gtkuncpy(rp->cmd, s, sizeof(rp->cmd)); else strncpy0(rp->dir, s, sizeof(rp->dir)); } } tdata.name = tdata.cmd = ""; tdata.dir[0] = '\0'; tdata.idx = -1; tdata.nidx = 0; tdata.cnt = FACTION_ROWS_TOTAL; tdata.lock = FALSE; run_create(spawn_code, &tdata, sizeof(tdata)); }
static const char* s_AdjustNetParams(const char* service, SConnNetInfo* net_info, EReqMethod req_method, const char* cgi_path, const char* cgi_args, const char* args, const char* static_header, EMIME_Type mime_t, EMIME_SubType mime_s, EMIME_Encoding mime_e, char* dynamic_header/*freed!*/) { const char *retval = 0; net_info->req_method = req_method; if (cgi_path) strncpy0(net_info->path, cgi_path, sizeof(net_info->path) - 1); if (args) strncpy0(net_info->args, args, sizeof(net_info->args) - 1); ConnNetInfo_DeleteAllArgs(net_info, cgi_args); if (ConnNetInfo_PrependArg(net_info, cgi_args, 0)) { size_t sh_len = static_header ? strlen(static_header) : 0; size_t dh_len = dynamic_header ? strlen(dynamic_header) : 0; char c_t[MAX_CONTENT_TYPE_LEN]; size_t ct_len, len; if (s_IsContentTypeDefined(service, net_info, mime_t, mime_s, mime_e) || !MIME_ComposeContentTypeEx(mime_t, mime_s, mime_e, c_t, sizeof(c_t))) { c_t[0] = '\0'; ct_len = 0; } else ct_len = strlen(c_t); if ((len = sh_len + dh_len + ct_len) != 0) { char* temp = (char*) malloc(len + 1/*EOL*/); if (temp) { strcpy(temp, static_header ? static_header : ""); strcpy(temp + sh_len, dynamic_header ? dynamic_header : ""); strcpy(temp + sh_len + dh_len, c_t); retval = temp; } } else retval = ""; } if (dynamic_header) free(dynamic_header); return retval; }
/*ARGSUSED*/ static const SLBSM_Sysinfo* s_GetSysinfo(const HOST_INFO hinfo, int/*bool*/ warn) { const SLBSM_Sysinfo* si = (const SLBSM_Sysinfo*)((const char*) hinfo + sizeof(*hinfo)); assert(hinfo); #if defined(_DEBUG) && !defined(NDEBUG) && defined(NETDAEMONS_VERSION_INT) if (si->data.version < NETDAEMONS_VERSION_INT && warn) { static int s_Warn = 0; if (s_Warn < 20) { char addr[64]; if (SOCK_ntoa(hinfo->addr, addr, sizeof(addr)) != 0) strncpy0(addr, "unknown", sizeof(addr) - 1); CORE_LOGF(s_Warn++ < 5 ? eLOG_Warning : eLOG_Trace, ("HINFO may be incorrect for obsolete daemon on %s" " (detected=%hu.%hu.%hu, expected=%s+)", addr, NETDAEMONS_MAJOR_OF(si->data.version), NETDAEMONS_MINOR_OF(si->data.version), NETDAEMONS_PATCH_OF(si->data.version), NETDAEMONS_VERSION_STR)); } } #endif /*_DEBUG && !NDEBUG && NETDAEMONS_VERSION_INT*/ return si; }
/* * 函数功能:读取授权机构代码 * 入口参数:无 * 出口参数:无 * 返 回 值:NORMAL--成功 * EXCEPTION--失败 */ int ProReadAuthOrgan(void) { char szBuffer[30]; char cRet; int nLen; if(strlen(TlvSendValue.szAuthOrgan)>0) { BUFCLR(szBuffer); memcpy(szBuffer,TlvSendValue.szAuthOrgan,11); } else { PubClearAll(); PubDisplay(2, "输入授权机构代码:"); BUFCLR(szAuthOrgan); do { BUFCLR(szBuffer); cRet = PubGetNStr(4, 0, szBuffer, 11); nLen = strlen(szBuffer); if (cRet == CANCELED||cRet == APPTIMEOUT) return EXCEPTION; } while (nLen != 11); } // strcpy(szAuthOrgan, szBuffer); strncpy0(szAuthOrgan, szBuffer, sizeof(szAuthOrgan)); return NORMAL; }
espeak_ERROR sync_espeak_Synth_Mark(unsigned int unique_identifier, const void *text, size_t size, const char *index_mark, unsigned int end_position, unsigned int flags, void* user_data) {//========================================================================= espeak_ERROR aStatus; InitText(flags); my_unique_identifier = unique_identifier; my_user_data = user_data; if(index_mark != NULL) { strncpy0(skip_marker, index_mark, sizeof(skip_marker)); skipping_text = 1; } end_character_position = end_position; aStatus = Synthesize(unique_identifier, text, flags | espeakSSML); SHOW_TIME("LEAVE sync_espeak_Synth_Mark"); return (aStatus); } // end of sync_espeak_Synth_Mark
/* !!! Expects that "txt" points to PATHBUF-sized buffer */ static void fpick_cleanse_path(char *txt) // Clean up null terminated path { char *src, *dest; #ifdef WIN32 // Unify path separators reseparate(txt); #endif // Expand home directory if ((txt[0] == '~') && (txt[1] == DIR_SEP)) { src = file_in_homedir(NULL, txt + 2, PATHBUF); strncpy0(txt, src, PATHBUF - 1); free(src); } // Remove multiple consecutive occurences of DIR_SEP if ((dest = src = strstr(txt, DIR_SEP_STR DIR_SEP_STR))) { while (*src) { if (*src == DIR_SEP) while (src[1] == DIR_SEP) src++; *dest++ = *src++; } *dest++ = '\0'; } }
void CDROM::startTrack(const char *trackName) { if (!_LICbuffer) return; byte *matchPtr = getTrackBuffer(trackName); if (!matchPtr) { warning("CDROM: Track \"%s\" not found", trackName); return; } strncpy0(_curTrack, trackName, 15); stopPlaying(); _curTrackBuffer = matchPtr; while (getTrackPos() >= 0) ; uint32 start = READ_LE_UINT32(matchPtr + 12); uint32 end = READ_LE_UINT32(matchPtr + 16); play(start, end); _startTime = g_system->getMillis(); _trackStop = _startTime + (end - start + 1 + 150) * 40 / 3; }
/* * 函数功能:读取授权机构代码 * 入口参数:无 * 出口参数:无 * 返 回 值:NORMAL--成功 * EXCEPTION--失败 */ int ProReadAuthOrgan(void) { char szBuffer[30]; char cRet; int nLen; PubClearAll(); PubDisplay(2, "输入授权机构代码:"); BUFCLR(szAuthOrgan); do { BUFCLR(szBuffer); cRet = PubGetNStr(4, 0, szBuffer, 11); nLen = strlen(szBuffer); if (cRet == CANCELED||cRet == APPTIMEOUT) return EXCEPTION; //add by baijz 20120625 if(nLen == 11) break; PubDisplay(2, "位数不足,请重输:"); PubDisplay(4, ""); //add end } while (nLen != 11); // strcpy(szAuthOrgan, szBuffer); strncpy0(szAuthOrgan, szBuffer, sizeof(szAuthOrgan)); return NORMAL; }
static int osip_boot_helper( int sel, int *args ) { char *ustr, *s, *p, *buf; switch( args[0] ) { case kBootHAscii2Unicode: /* unicode_dest, src, maxlen -- uni_strlen */ ustr = transl_mphys( args[1] ); s = transl_mphys( args[2] ); if( !ustr || !s ) return -1; return asc2uni( (unsigned char *)ustr, s, args[3] ); case kBootHUnicode2Ascii: /* dest, uni_str, uni_strlen, maxlen -- strlen */ s = transl_mphys( args[1] ); ustr = transl_mphys( args[2] ); if( !ustr || !s ) return -1; return uni2asc( s, (unsigned char *)ustr, args[3], args[4] ); case kBootHGetStrResInd: /* resname, buf, len, index, argnum */ p = transl_mphys( args[1] ); buf = transl_mphys( args[2] ); if( !p || !(s=get_str_res_ind(p,args[4],args[5])) ) return 0; if( args[2] && buf ) strncpy0( buf, s, args[3] ); return args[2]; case kBootHGetRAMSize: return ram.size; } return -1; }
/* Scan directory, populate widgets; return 1 if success, 0 if total failure, * -1 if failed with original dir and scanned a different one */ static int fpick_scan_directory(fpick_dd *dt, char *name, char *select) { DIR *dp; char *cp, *parent = NULL; char full_name[PATHBUF]; int len, fail, res = 1; strncpy0(full_name, name, PATHBUF - 1); len = strlen(full_name); /* Ensure the invariant */ if (!len || (full_name[len - 1] != DIR_SEP)) full_name[len++] = DIR_SEP , full_name[len] = 0; /* Step up the path till a searchable dir is found */ fail = 0; while (!(dp = opendir(full_name))) { res = -1; // Remember that original path was invalid full_name[len - 1] = 0; cp = strrchr(full_name, DIR_SEP); // Try to go one level up if (cp) len = cp - full_name + 1; // No luck - restart with current dir else if (!fail++) { getcwd(full_name, PATHBUF - 1); len = strlen(full_name); full_name[len++] = DIR_SEP; } // If current dir hasn't helped either, give up else return (0); full_name[len] = 0; } /* If we're going up the path and want to show from where */ if (!select) { if (!strncmp(dt->txt_directory, full_name, len) && dt->txt_directory[len]) { cp = strchr(dt->txt_directory + len, DIR_SEP); // Guaranteed parent = dt->txt_directory + len; select = parent = g_strndup(parent, cp - parent); } } /* If we've nothing to show */ else if (!select[0]) select = NULL; strncpy(dt->txt_directory, full_name, PATHBUF); fpick_directory_new(dt, full_name); // Register directory in combo scan_dir(dt, dp, select); g_free(parent); closedir(dp); filter_dir(dt, dt->txt_mask); cmd_reset(dt->list, dt); return (res); }
static void faction_changed(spawn_dd *dt, void **wdata, int what, void **where) { void *cause; spawn_row *rp; if (dt->lock) return; cause = cmd_read(where, dt); rp = dt->strs + dt->idx; if (cause == dt->dir) strncpy(rp->dir, dt->dir, sizeof(rp->dir)); else { strncpy0(rp->name, dt->name, sizeof(rp->name)); strncpy0(rp->cmd, dt->cmd, sizeof(rp->cmd)); cmd_setv(dt->list, (void *)dt->idx, LISTC_RESET_ROW); } }
void DrawEnvelopes() {//================ int ix_env; int y_base; int x; FILE *f_txt=NULL; unsigned char *env; char name[200]; wxBitmap bitmap(WD_ENV,HT_ENV*n_envelopes); // Create a memory DC wxMemoryDC dc; dc.SelectObject(bitmap); dc.SetBrush(*wxWHITE_BRUSH); dc.SetFont(*wxSWISS_FONT); dc.Clear(); sprintf(name,"%s%s",path_source,"envelopes.txt"); // f_txt = fopen(name,"w"); for(ix_env=0; ix_env<n_envelopes; ix_env++) { y_base = HT_ENV * ix_env; dc.SetPen(*wxLIGHT_GREY_PEN); dc.DrawLine(0,y_base+0,256,y_base+0); dc.DrawLine(0,y_base+64,256,y_base+64); dc.DrawLine(0,y_base+128,256,y_base+128); dc.DrawLine(128,y_base+0,128,y_base+128); dc.SetPen(*wxBLACK_PEN); strncpy0(name,envelope_paths[ix_env],sizeof(name)); dc.DrawText(wxString(name,wxConvLocal),1,y_base); env = envelope_dat[ix_env]; y_base = y_base+128; for(x=0; x<127; x++) { dc.DrawLine(x*2, y_base-env[x]/2, (x+1)*2, y_base-env[x+1]/2); } if(f_txt != NULL) { fprintf(f_txt,"%s\n",name); for(x=0; x<128; x++) { fprintf(f_txt," 0x%.2x,",env[x]); if((x & 0xf) == 0xf) fputc('\n',f_txt); } fputc('\n',f_txt); } } bitmap.SaveFile(path_phsource+_T("/envelopes.png"),wxBITMAP_TYPE_PNG); if(f_txt != NULL) fclose(f_txt); }
espeak_ERROR SetVoiceByName(const char *name) {//========================================= espeak_VOICE *v; int ix; espeak_VOICE voice_selector; char *variant_name; static char buf[60]; strncpy0(buf,name,sizeof(buf)); variant_name = ExtractVoiceVariantName(buf, 0, 1); for(ix=0; ; ix++) { // convert voice name to lower case (ascii) if((buf[ix] = tolower(buf[ix])) == 0) break; } memset(&voice_selector,0,sizeof(voice_selector)); voice_selector.name = (char *)name; // include variant name in voice stack ?? // first check for a voice with this filename // This may avoid the need to call espeak_ListVoices(). if(LoadVoice(buf,1) != NULL) { if(variant_name[0] != 0) { LoadVoice(variant_name,2); } DoVoiceChange(voice); voice_selector.languages = voice->language_name; SetVoiceStack(&voice_selector, variant_name); return(EE_OK); } if(n_voices_list == 0) espeak_ListVoices(NULL); // create the voices list if((v = SelectVoiceByName(voices_list,buf)) != NULL) { if(LoadVoice(v->identifier,0) != NULL) { if(variant_name[0] != 0) { LoadVoice(variant_name,2); } DoVoiceChange(voice); voice_selector.languages = voice->language_name; SetVoiceStack(&voice_selector, variant_name); return(EE_OK); } } return(EE_INTERNAL_ERROR); // voice name not found } // end of SetVoiceByName
static int any_HOOK(TBUFF **rp, const char *vp, HOOK * hook) { if (rp) { tb_scopy(rp, hook->proc); return TRUE; } else if (vp) { (void) strncpy0(hook->proc, vp, (size_t) NBUFN); return TRUE; } else { return FALSE; } }
espeak_VOICE *SelectVoiceByName(espeak_VOICE **voices, const char *name2) { int ix; int match_fname = -1; int match_fname2 = -1; int match_name = -1; const char *id; // this is the filename within espeak-data/voices char *variant_name; int last_part_len; char last_part[41]; char name[40]; if (voices == NULL) { if (n_voices_list == 0) espeak_ListVoices(NULL); // create the voices list voices = voices_list; } strncpy0(name, name2, sizeof(name)); if ((variant_name = strchr(name, '+')) != NULL) { *variant_name = 0; variant_name++; } sprintf(last_part, "%c%s", PATHSEP, name); last_part_len = strlen(last_part); for (ix = 0; voices[ix] != NULL; ix++) { if (strcasecmp(name, voices[ix]->name) == 0) { match_name = ix; // found matching voice name break; } else { id = voices[ix]->identifier; if (strcasecmp(name, id) == 0) match_fname = ix; // matching identifier, use this if no matching name else if (strcasecmp(last_part, &id[strlen(id)-last_part_len]) == 0) match_fname2 = ix; } } if (match_name < 0) { match_name = match_fname; // no matching name, try matching filename if (match_name < 0) match_name = match_fname2; // try matching just the last part of the filename } if (match_name < 0) return NULL; return voices[match_name]; }
/* See documentation in header file. */ int ini_parse(const char* filename, int (*handler)(void*, const char*, const char*, const char*), void* user) { FIL file; int error; if (f_open(&file, (TCHAR*) filename, FA_READ) != FR_OK) { strncpy0(ini_error, "f_open", 7); return -1; } error = ini_parse_file(&file, handler, user); f_close(&file); return error; }
static void s_MakeFrom(char* buf, size_t buf_size) { size_t buf_len, hostname_len; if (!CORE_GetUsername(buf, buf_size) || !*buf) strncpy0(buf, "anonymous", buf_size - 1); buf_len = strlen(buf); hostname_len = buf_size - buf_len; if (hostname_len-- > 1) { buf[buf_len++] = '@'; if ((!SOCK_gethostbyaddr(0, &buf[buf_len], hostname_len) || !strchr(&buf[buf_len], '.')) && SOCK_gethostname(&buf[buf_len], hostname_len) != 0) { buf[--buf_len] = '\0'; } } }
// reads a file from the SD card one line at a time int fat_read_line(struct fat_file_struct* fd, uint8_t* buffer, uint8_t buffer_len) { char internal[INI_MAX_LINE]; int bytesRead, bytesToCopy; int32_t offset; char* pchar; bytesRead = fat_read_file(fd, internal, INI_MAX_LINE); #if INI_DEBUG printf("read_line start: bytesRead=%d\r\n", bytesRead); #endif if(bytesRead > 0) { // 0 on EOF, bytes read for >0 pchar = strchr(internal, '\n'); // return a pointer to the first newline if(pchar) { // if newline found, compute byte number for matching char bytesToCopy = pchar - internal + 1; } else { // if newline not found, copy all the bytes read bytesToCopy = bytesRead; } if(bytesToCopy > buffer_len) { // warn user or exit? bytesToCopy = buffer_len; } // copy the bytes to the destination buffer strncpy0(buffer, internal, bytesToCopy); // seek backwards from current file pointer to end of copied text offset = bytesToCopy - bytesRead; //offset = bytesToCopy; #if INI_DEBUG printf( "offset=%d, bytesRead=%d, bytesToCopy=%d\r\n", (int)offset, bytesRead, bytesToCopy); #endif if(!fat_seek_file(fd, &offset, FAT_SEEK_CUR)) { //if(!fat_seek_file(fd, &offset, FAT_SEEK_SET)) { #if INI_DEBUG printf("fat_seek_file: failure\r\n"); #endif } #if INI_DEBUG printf("after seek: offset=%d\r\n", offset); #endif return bytesToCopy; } return bytesRead; }
espeak_ng_STATUS sync_espeak_Synth_Mark(unsigned int unique_identifier, const void *text, const char *index_mark, unsigned int end_position, unsigned int flags, void *user_data) { InitText(flags); my_unique_identifier = unique_identifier; my_user_data = user_data; if (index_mark != NULL) { strncpy0(skip_marker, index_mark, sizeof(skip_marker)); skipping_text = true; } end_character_position = end_position; return Synthesize(unique_identifier, text, flags | espeakSSML); }
voice_t *LoadVoiceVariant(const char *vname, int variant_num) { // Load a voice file. // Also apply a voice variant if specified by "variant", or by "+number" or "+name" in the "vname" voice_t *v; char *variant_name; char buf[60]; strncpy0(buf, vname, sizeof(buf)); variant_name = ExtractVoiceVariantName(buf, variant_num, 1); if ((v = LoadVoice(buf, 0)) == NULL) return NULL; if (variant_name[0] != 0) v = LoadVoice(variant_name, 2); return v; }
void Inter_v6::o6_openItk() { char fileName[32]; _vm->_game->_script->evalExpr(0); strncpy0(fileName, _vm->_game->_script->getResultStr(), 27); if (!strchr(fileName, '.')) strcat(fileName, ".ITK"); _vm->_dataIO->openDataFile(fileName, true); // WORKAROUND: The CD number detection in Urban Runner is quite daft // (it checks CD1.ITK - CD4.ITK and the first that's found determines // the CD number), while its NO_CD modus wants everything in CD1.ITK. // So we just open the other ITKs, too. if (_vm->_global->_noCd && !scumm_stricmp(fileName, "CD1.ITK")) { _vm->_dataIO->openDataFile("CD2.ITK", true); _vm->_dataIO->openDataFile("CD3.ITK", true); _vm->_dataIO->openDataFile("CD4.ITK", true); } }
static char *ExtractVoiceVariantName(char *vname, int variant_num) {//=============================================================== // Remove any voice variant suffix (name or number) from a voice name // Returns the voice variant name char *p; static char variant_name[20]; char variant_prefix[5]; variant_name[0] = 0; sprintf(variant_prefix,"!v%c",PATHSEP); if(vname != NULL) { if((p = strchr(vname,'+')) != NULL) { // The voice name has a +variant suffix *p++ = 0; // delete the suffix from the voice name if(isdigit(*p)) { variant_num = atoi(p); // variant number } else { // voice variant name, not number strcpy(variant_name,variant_prefix); strncpy0(&variant_name[3],p,sizeof(variant_name)-3); } } } if(variant_num > 0) { if(variant_num < 10) sprintf(variant_name,"%sm%d",variant_prefix, variant_num); // male else sprintf(variant_name,"%sf%d",variant_prefix, variant_num-10); // female } return(variant_name); } // end of ExtractVoiceVariantName
const char* DBLB_GetServerName(const char* lb_name, char* server_name_buf, size_t server_name_buflen, const char* const skip_servers[], char* errmsg_buf, size_t errmsg_buflen) { EDBLB_Status result; const char* retval = DBLB_GetServer(lb_name, fDBLB_None, 0/*preference*/, skip_servers, 0/*CP*/, server_name_buf, server_name_buflen, &result); if (errmsg_buf) { const char* errstr; assert(errmsg_buflen); errstr = DBLB_StatusStr(result); strncpy0(errmsg_buf, errstr ? errstr : "Unknown error", errmsg_buflen - 1); } return retval; }
void ProGetBaseDialFix(void) { char szValue[100]; char szGetBuf[30]; int i; uint nLen; // 银联商务要求每次都读主控应用的分机前缀 BUFCLR(szValue); if(EA_ucParamFileFindPara("Base56x0", "parafile", "Base0004", &nLen, szValue) == EM_SUCCESS) { BUFCLR(szGetBuf); memcpy(szGetBuf, FV.gbNacInfo.szDialFix, 4); if (szValue[0] != 0) { strncpy0(szGetBuf + 4, szValue, sizeof(szGetBuf)); for (i = 0; szGetBuf[i] != 0x0; i++) if (szGetBuf[i] == '.') szGetBuf[i] = ','; } strcpy(FV.gbNacInfo.szDialFix, szGetBuf); } }
static int/*bool*/ s_SockReadResponse(SOCK sock, int code, int alt_code, char* buf, size_t buf_size) { int c = s_SockRead(sock, buf, buf_size); if (c <= 0) { const char* message = 0; switch (c) { case SMTP_READERR: message = "Read error"; break; case SMTP_READTMO: message = "Read timed out"; break; case SMTP_RESPERR: message = "Error reading response prefix"; break; case SMTP_NOCODE: message = "No response code detected"; break; case SMTP_BADCODE: message = "Response code doesn't match in lines"; break; case SMTP_BADRESP: message = "Malformed response"; break; default: message = "Unknown error"; assert(0); break; } assert(message); strncpy0(buf, message, buf_size - 1); } else if (c == code || (alt_code && c == alt_code)) return 1/*success*/; return 0/*failure*/; }
int write_final_state(control *c, params *p, state *s) { /* Write the final state to the input param file so we can easily restart the model. This function copies the input param file with the exception of anything in the git hash and the state which it replaces with the updated stuff. */ char line[STRING_LENGTH]; char saved_line[STRING_LENGTH]; char section[STRING_LENGTH] = ""; char prev_name[STRING_LENGTH] = ""; char *start; char *end; char *name; char *value; int error = 0; int line_number = 0; int match = FALSE; while (fgets(line, sizeof(line), c->ifp) != NULL) { strcpy(saved_line, line); line_number++; start = lskip(rstrip(line)); if (*start == ';' || *start == '#') { /* Per Python ConfigParser, allow '#' comments at start of line */ } else if (*start == '[') { /* A "[section]" line */ end = find_char_or_comment(start + 1, ']'); if (*end == ']') { *end = '\0'; strncpy0(section, start + 1, sizeof(section)); *prev_name = '\0'; } else if (!error) { /* No ']' found on section line */ error = line_number; } } else if (*start && *start != ';') { /* Not a comment, must be a name[=:]value pair */ end = find_char_or_comment(start, '='); if (*end != '=') { end = find_char_or_comment(start, ':'); } if (*end == '=' || *end == ':') { *end = '\0'; name = rstrip(start); value = lskip(end + 1); end = find_char_or_comment(value, '\0'); if (*end == ';') *end = '\0'; rstrip(value); /* Valid name[=:]value pair found, call handler */ strncpy0(prev_name, name, sizeof(prev_name)); if (!ohandler(section, name, value, c, p, s, &match) && !error) error = line_number; } else if (!error) { /* No '=' or ':' found on name[=:]value line */ error = line_number; break; } } if (match == FALSE) fprintf(c->ofp, "%s", saved_line); else match = FALSE; /* reset match flag */ } return error; }
int ini_parse_string_impl(const std::string & s, int (*handler)(void*, const char*, const char*, const char*), void* user) { std::istringstream input(s); /* Uses a fair bit of stack (use heap instead if you need to) */ char section[MAX_INI_STRING] = ""; bool has_group = false; int lineno = 0; int error = 0; /* Scan through file line by line */ while (!at_end(input)) { std::string newline; get_line(input, newline); char * line = (char*)newline.c_str(); lineno++; char * start = line; char * end; // UTF-8 BOM if (lineno == 1 && (unsigned char)start[0] == 0xEF && (unsigned char)start[1] == 0xBB && (unsigned char)start[2] == 0xBF) { start += 3; } start = lskip(rstrip(start)); char c = *start; if (c == ';' || c == '#' || (c == '/' && start[1] == '/')) { /* Per Python ConfigParser, allow '#' comments at start of line */ } else if (c == '[') { /* A "[section]" line */ end = find_char_or_comment(start + 1, ']'); if (*end == ']') { *end = '\0'; strncpy0(section, start + 1, sizeof(section)); has_group = true; } else if (!error) { /* No ']' found on section line */ error = lineno; } } else if (c && c != ';' && has_group) { /* Not a comment, must be a name[=:]value pair */ end = find_char_or_comment(start, '='); if (*end != '=') { end = find_char_or_comment(start, ':'); } if (*end == '=' || *end == ':') { *end = '\0'; char * name = rstrip(start); char * value = lskip(end + 1); end = find_char_or_comment(value, '\0'); if (*end == ';') *end = '\0'; rstrip(value); /* Valid name[=:]value pair found, call handler */ if (!handler(user, section, name, value) && !error) error = lineno; } else if (!error) { /* No '=' or ':' found on name[=:]value line */ error = lineno; } } } return error; }
/* See documentation in header file. */ int ini_parse_file(FILE* file,const struct IniConfig* config, void* user) { /* Uses a fair bit of stack (use heap instead if you need to) */ #if INI_USE_STACK char line[INI_MAX_LINE]; #else char* line; #endif char section[MAX_SECTION] = ""; char prev_name[MAX_NAME] = ""; char* start; char* end; char* name; char* value; int lineno = 0; int error = 0; #if !INI_USE_STACK line = (char*)malloc(INI_MAX_LINE); if (!line) { return -2; } #endif /* Scan through file line by line */ while (fgets(line, INI_MAX_LINE, file) != NULL) { lineno++; start = line; #if INI_ALLOW_BOM if (lineno == 1 && (unsigned char)start[0] == 0xEF && (unsigned char)start[1] == 0xBB && (unsigned char)start[2] == 0xBF) { start += 3; } #endif start = lskip(rstrip(start)); if (*start == ';' || *start == '#') { start += 1; if (!config->commentHandler(user, start) && !error) error = lineno; /* Per Python ConfigParser, allow '#' comments at start of line */ } else if (config->supportMultiline && *prev_name && *start && start > line) { /* Non-black line with leading whitespace, treat as continuation of previous name's value (as per Python ConfigParser). */ if (!config->keyHandler(user, section, prev_name, start, 1) && !error) error = lineno; } else if (*start == '[') { /* A "[section]" line */ end = find_char_or_comment(start + 1, ']'); if (*end == ']') { *end = '\0'; strncpy0(section, start + 1, sizeof(section)); *prev_name = '\0'; if(!config->sectionHandler(user, section) && !error) error = lineno; } else if (!error) { /* No ']' found on section line */ error = lineno; } } else if (*start && *start != ';') { /* Not a comment, must be a name[=:]value pair */ end = find_char_or_comment(start, '='); if (*end != '=') { end = find_char_or_comment(start, ':'); } if (*end == '=' || *end == ':') { *end = '\0'; name = rstrip(start); value = lskip(end + 1); end = find_char_or_comment(value, '\0'); if (*end == ';') *end = '\0'; rstrip(value); /* Valid name[=:]value pair found, call handler */ strncpy0(prev_name, name, sizeof(prev_name)); if (!config->keyHandler(user, section, name, value, 0) && !error) error = lineno; } else if (!error) { /* No '=' or ':' found on name[=:]value line */ error = lineno; } } #if INI_STOP_ON_FIRST_ERROR if (error) break; #endif } #if !INI_USE_STACK free(line); #endif return error; }
// read and parse int load(const char* filename) { FILE *file = fopen(filename, "r"); if(!file) return false; char *line = new char[INI_MAX_LINE]; std::string prev_value, prev_name; char *section = new char [MAX_SECTION]; *section = '\0'; int bMultiline = 0; int lineno = 0, error=0; char *start, *end, *name, *value; while (fgets(line, INI_MAX_LINE, file) != NULL) { lineno++; start = line; start = lskip(start); // remove comment end = find_char_or_comment(start, '\0'); if( *end != '\0' ) // it points to a comment char *end = '\0'; rstrip(start, &end); if( start == end ) continue; if ( bMultiline ) { if( *(end-1) == '\\' && (end-1 == start || *(end-2) == ' ') ) { // continue reading next line // remove " \" *(--end) = '\0'; if( end > start && *(end-1) == ' ') *(--end) = '\0'; prev_value += start; } else{ prev_value += start; if( !handle_record(section, prev_name.c_str(), prev_value.c_str()) ) error = lineno; bMultiline = 0; prev_value = ""; prev_name = ""; } } else if( *start == '[' ) { // found section end = find_char_or_comment(start + 1, ']'); if (*end == ']') { *end = '\0'; strncpy0(section, start + 1, MAX_SECTION); } else{ /* No ']' found on section line */ error = lineno; } bMultiline = false; } else { /* Not a comment, must be a name[=:]value pair */ bMultiline = false; char *nend = find_char_or_comment(start, '='); if (*nend != '=') { nend = find_char_or_comment(start, ':'); } if (*nend == '=' || *nend == ':') { *nend = '\0'; value = lskip(nend + 1); name = rstrip(start, &nend); /* Valid name[=:]value pair found, call handler */ if( *(end-1) == '\\' && (end-1 == value || *(end-2) == ' ') ) { // continue reading next line // remove " \" *(--end) = '\0'; if( end > value && *(end-1) == ' ') *(--end) = '\0'; prev_value = value; prev_name = name; bMultiline = true; } if( !bMultiline ) if (!handle_record(section, name, value)) error = lineno; } else if (!error) { /* No '=' or ':' found on name[=:]value line */ error = lineno; } } if (error) break; } // while getline fclose(file); delete [] line; delete [] section; return error; } // read function
int main(int argc, char **argv) { static struct option long_options[] = { { "help", no_argument, 0, 'h' }, { "stdin", no_argument, 0, 0x100 }, { "compile-debug", optional_argument, 0, 0x101 }, { "compile", optional_argument, 0, 0x102 }, { "punct", optional_argument, 0, 0x103 }, { "voices", optional_argument, 0, 0x104 }, { "stdout", no_argument, 0, 0x105 }, { "split", optional_argument, 0, 0x106 }, { "path", required_argument, 0, 0x107 }, { "phonout", required_argument, 0, 0x108 }, { "pho", no_argument, 0, 0x109 }, { "ipa", optional_argument, 0, 0x10a }, { "version", no_argument, 0, 0x10b }, { "sep", optional_argument, 0, 0x10c }, { "tie", optional_argument, 0, 0x10d }, { "compile-mbrola", optional_argument, 0, 0x10e }, { "compile-intonations", no_argument, 0, 0x10f }, { "compile-phonemes", optional_argument, 0, 0x110 }, { 0, 0, 0, 0 } }; FILE *f_text = NULL; char *p_text = NULL; FILE *f_phonemes_out = stdout; char *data_path = NULL; // use default path for espeak-ng-data int option_index = 0; int c; int ix; char *optarg2; int value; int flag_stdin = 0; int flag_compile = 0; int filesize = 0; int synth_flags = espeakCHARS_AUTO | espeakPHONEMES | espeakENDPAUSE; int volume = -1; int speed = -1; int pitch = -1; int wordgap = -1; int option_capitals = -1; int option_punctuation = -1; int phonemes_separator = 0; int phoneme_options = 0; int option_linelength = 0; int option_waveout = 0; espeak_VOICE voice_select; char filename[200]; char voicename[40]; char devicename[200]; #define N_PUNCTLIST 100 wchar_t option_punctlist[N_PUNCTLIST]; voicename[0] = 0; wavefile[0] = 0; filename[0] = 0; devicename[0] = 0; option_punctlist[0] = 0; while (true) { c = getopt_long(argc, argv, "a:b:d:f:g:hk:l:mp:qs:v:w:xXz", long_options, &option_index); // Detect the end of the options. if (c == -1) break; optarg2 = optarg; switch (c) { case 'b': // input character encoding, 8bit, 16bit, UTF8 if ((sscanf(optarg2, "%d", &value) == 1) && (value <= 4)) synth_flags |= value; else synth_flags |= espeakCHARS_8BIT; break; case 'd': strncpy0(devicename, optarg2, sizeof(devicename)); break; case 'h': printf("\n"); PrintVersion(); printf("%s", help_text); return 0; case 'k': option_capitals = atoi(optarg2); break; case 'x': phoneme_options |= espeakPHONEMES_SHOW; break; case 'X': phoneme_options |= espeakPHONEMES_TRACE; break; case 'm': synth_flags |= espeakSSML; break; case 'p': pitch = atoi(optarg2); break; case 'q': quiet = true; break; case 'f': strncpy0(filename, optarg2, sizeof(filename)); break; case 'l': option_linelength = atoi(optarg2); break; case 'a': volume = atoi(optarg2); break; case 's': speed = atoi(optarg2); break; case 'g': wordgap = atoi(optarg2); break; case 'v': strncpy0(voicename, optarg2, sizeof(voicename)); break; case 'w': option_waveout = 1; strncpy0(wavefile, optarg2, sizeof(filename)); break; case 'z': // remove pause from the end of a sentence synth_flags &= ~espeakENDPAUSE; break; case 0x100: // --stdin flag_stdin = 1; break; case 0x105: // --stdout option_waveout = 1; strcpy(wavefile, "stdout"); break; case 0x101: // --compile-debug case 0x102: // --compile if (optarg2 != NULL && *optarg2) { strncpy0(voicename, optarg2, sizeof(voicename)); flag_compile = c; quiet = true; break; } else { fprintf(stderr, "Voice name to '%s' not specified.\n", c == 0x101 ? "--compile-debug" : "--compile"); exit(EXIT_FAILURE); } case 0x103: // --punct option_punctuation = 1; if (optarg2 != NULL) { ix = 0; while ((ix < N_PUNCTLIST) && ((option_punctlist[ix] = optarg2[ix]) != 0)) ix++; option_punctlist[N_PUNCTLIST-1] = 0; option_punctuation = 2; } break; case 0x104: // --voices espeak_Initialize(AUDIO_OUTPUT_SYNCHRONOUS, 0, data_path, 0); DisplayVoices(stdout, optarg2); exit(0); case 0x106: // -- split if (optarg2 == NULL) samples_split_seconds = 30 * 60; // default 30 minutes else samples_split_seconds = atoi(optarg2) * 60; break; case 0x107: // --path data_path = optarg2; break; case 0x108: // --phonout if ((f_phonemes_out = fopen(optarg2, "w")) == NULL) fprintf(stderr, "Can't write to: %s\n", optarg2); break; case 0x109: // --pho phoneme_options |= espeakPHONEMES_MBROLA; break; case 0x10a: // --ipa phoneme_options |= espeakPHONEMES_IPA; if (optarg2 != NULL) { // deprecated and obsolete switch (atoi(optarg2)) { case 1: phonemes_separator = '_'; break; case 2: phonemes_separator = 0x0361; phoneme_options |= espeakPHONEMES_TIE; break; case 3: phonemes_separator = 0x200d; // ZWJ phoneme_options |= espeakPHONEMES_TIE; break; } } break; case 0x10b: // --version PrintVersion(); exit(0); case 0x10c: // --sep phoneme_options |= espeakPHONEMES_SHOW; if (optarg2 == 0) phonemes_separator = ' '; else utf8_in(&phonemes_separator, optarg2); if (phonemes_separator == 'z') phonemes_separator = 0x200c; // ZWNJ break; case 0x10d: // --tie phoneme_options |= (espeakPHONEMES_SHOW | espeakPHONEMES_TIE); if (optarg2 == 0) phonemes_separator = 0x0361; // default: combining-double-inverted-breve else utf8_in(&phonemes_separator, optarg2); if (phonemes_separator == 'z') phonemes_separator = 0x200d; // ZWJ break; case 0x10e: // --compile-mbrola { espeak_ng_InitializePath(data_path); espeak_ng_ERROR_CONTEXT context = NULL; espeak_ng_STATUS result = espeak_ng_CompileMbrolaVoice(optarg2, stdout, &context); if (result != ENS_OK) { espeak_ng_PrintStatusCodeMessage(result, stderr, context); espeak_ng_ClearErrorContext(&context); return EXIT_FAILURE; } return EXIT_SUCCESS; } case 0x10f: // --compile-intonations { espeak_ng_InitializePath(data_path); espeak_ng_ERROR_CONTEXT context = NULL; espeak_ng_STATUS result = espeak_ng_CompileIntonation(stdout, &context); if (result != ENS_OK) { espeak_ng_PrintStatusCodeMessage(result, stderr, context); espeak_ng_ClearErrorContext(&context); return EXIT_FAILURE; } return EXIT_SUCCESS; } case 0x110: // --compile-phonemes { espeak_ng_InitializePath(data_path); espeak_ng_ERROR_CONTEXT context = NULL; espeak_ng_STATUS result; if (optarg2) { result = espeak_ng_CompilePhonemeDataPath(22050, optarg2, NULL, stdout, &context); } else { result = espeak_ng_CompilePhonemeData(22050, stdout, &context); } if (result != ENS_OK) { espeak_ng_PrintStatusCodeMessage(result, stderr, context); espeak_ng_ClearErrorContext(&context); return EXIT_FAILURE; } return EXIT_SUCCESS; } default: exit(0); } } espeak_ng_InitializePath(data_path); espeak_ng_ERROR_CONTEXT context = NULL; espeak_ng_STATUS result = espeak_ng_Initialize(&context); if (result != ENS_OK) { espeak_ng_PrintStatusCodeMessage(result, stderr, context); espeak_ng_ClearErrorContext(&context); exit(1); } if (option_waveout || quiet) { // writing to a file (or no output), we can use synchronous mode result = espeak_ng_InitializeOutput(ENOUTPUT_MODE_SYNCHRONOUS, 0, devicename[0] ? devicename : NULL); samplerate = espeak_ng_GetSampleRate(); samples_split = samplerate * samples_split_seconds; espeak_SetSynthCallback(SynthCallback); if (samples_split) { char *extn; extn = strrchr(wavefile, '.'); if ((extn != NULL) && ((wavefile + strlen(wavefile) - extn) <= 4)) { strcpy(filetype, extn); *extn = 0; } } } else { // play the sound output result = espeak_ng_InitializeOutput(PLAYBACK_MODE, 0, devicename[0] ? devicename : NULL); samplerate = espeak_ng_GetSampleRate(); } if (result != ENS_OK) { espeak_ng_PrintStatusCodeMessage(result, stderr, NULL); exit(EXIT_FAILURE); } if (voicename[0] == 0) strcpy(voicename, ESPEAKNG_DEFAULT_VOICE); result = espeak_ng_SetVoiceByName(voicename); if (result != ENS_OK) { memset(&voice_select, 0, sizeof(voice_select)); voice_select.languages = voicename; result = espeak_ng_SetVoiceByProperties(&voice_select); if (result != ENS_OK) { espeak_ng_PrintStatusCodeMessage(result, stderr, NULL); exit(EXIT_FAILURE); } } if (flag_compile) { // This must be done after the voice is set espeak_ng_ERROR_CONTEXT context = NULL; espeak_ng_STATUS result = espeak_ng_CompileDictionary("", NULL, stderr, flag_compile & 0x1, &context); if (result != ENS_OK) { espeak_ng_PrintStatusCodeMessage(result, stderr, context); espeak_ng_ClearErrorContext(&context); return EXIT_FAILURE; } return EXIT_SUCCESS; } // set any non-default values of parameters. This must be done after espeak_Initialize() if (speed > 0) espeak_SetParameter(espeakRATE, speed, 0); if (volume >= 0) espeak_SetParameter(espeakVOLUME, volume, 0); if (pitch >= 0) espeak_SetParameter(espeakPITCH, pitch, 0); if (option_capitals >= 0) espeak_SetParameter(espeakCAPITALS, option_capitals, 0); if (option_punctuation >= 0) espeak_SetParameter(espeakPUNCTUATION, option_punctuation, 0); if (wordgap >= 0) espeak_SetParameter(espeakWORDGAP, wordgap, 0); if (option_linelength > 0) espeak_SetParameter(espeakLINELENGTH, option_linelength, 0); if (option_punctuation == 2) espeak_SetPunctuationList(option_punctlist); espeak_SetPhonemeTrace(phoneme_options | (phonemes_separator << 8), f_phonemes_out); if (filename[0] == 0) { if ((optind < argc) && (flag_stdin == 0)) { // there's a non-option parameter, and no -f or --stdin // use it as text p_text = argv[optind]; } else { f_text = stdin; if (flag_stdin == 0) flag_stdin = 2; } } else { struct stat st; if (stat(filename, &st) != 0) { fprintf(stderr, "Failed to stat() file '%s'\n", filename); exit(EXIT_FAILURE); } filesize = GetFileLength(filename); f_text = fopen(filename, "r"); if (f_text == NULL) { fprintf(stderr, "Failed to read file '%s'\n", filename); exit(EXIT_FAILURE); } if (S_ISFIFO(st.st_mode)) { flag_stdin = 2; } } if (p_text != NULL) { int size; size = strlen(p_text); espeak_Synth(p_text, size+1, 0, POS_CHARACTER, 0, synth_flags, NULL, NULL); } else if (flag_stdin) { size_t max = 1000; if ((p_text = (char *)malloc(max)) == NULL) { espeak_ng_PrintStatusCodeMessage(ENOMEM, stderr, NULL); exit(EXIT_FAILURE); } if (flag_stdin == 2) { // line by line input on stdin or from FIFO while (fgets(p_text, max, f_text) != NULL) { p_text[max-1] = 0; espeak_Synth(p_text, max, 0, POS_CHARACTER, 0, synth_flags, NULL, NULL); // Allow subprocesses to use the audio data through pipes. fflush(stdout); } if (f_text != stdin) { fclose(f_text); } } else { // bulk input on stdin ix = 0; while (true) { if ((c = fgetc(stdin)) == EOF) break; p_text[ix++] = (char)c; if (ix >= (max-1)) { char *new_text = NULL; if (max <= SIZE_MAX - 1000) { max += 1000; new_text = (char *)realloc(p_text, max); } if (new_text == NULL) { free(p_text); espeak_ng_PrintStatusCodeMessage(ENOMEM, stderr, NULL); exit(EXIT_FAILURE); } p_text = new_text; } } if (ix > 0) { p_text[ix-1] = 0; espeak_Synth(p_text, ix+1, 0, POS_CHARACTER, 0, synth_flags, NULL, NULL); } } free(p_text); } else if (f_text != NULL) { if ((p_text = (char *)malloc(filesize+1)) == NULL) { espeak_ng_PrintStatusCodeMessage(ENOMEM, stderr, NULL); exit(EXIT_FAILURE); } fread(p_text, 1, filesize, f_text); p_text[filesize] = 0; espeak_Synth(p_text, filesize+1, 0, POS_CHARACTER, 0, synth_flags, NULL, NULL); fclose(f_text); free(p_text); } result = espeak_ng_Synchronize(); if (result != ENS_OK) { espeak_ng_PrintStatusCodeMessage(result, stderr, NULL); exit(EXIT_FAILURE); } if (f_phonemes_out != stdout) fclose(f_phonemes_out); CloseWavFile(); espeak_ng_Terminate(); return 0; }