/** Initialize the playlists */ void InitializeMusic() { uint j = 0; for (uint i = 0; i < NUM_SONGS_AVAILABLE; i++) { if (StrEmpty(GetSongName(i))) continue; _playlist_all[j++] = i + 1; } /* Terminate the list */ _playlist_all[j] = 0; /* Now make the 'styled' playlists */ for (uint k = 0; k < NUM_SONG_CLASSES; k++) { j = 0; for (uint i = 0; i < NUM_SONGS_CLASS; i++) { int id = k * NUM_SONGS_CLASS + i + 1; if (StrEmpty(GetSongName(id))) continue; _playlists[k + 1][j++] = id + 1; } /* Terminate the list */ _playlists[k + 1][j] = 0; } ValidatePlaylist(_settings_client.music.custom_1, lastof(_settings_client.music.custom_1)); ValidatePlaylist(_settings_client.music.custom_2, lastof(_settings_client.music.custom_2)); if (BaseMusic::GetUsedSet()->num_available < _music_wnd_cursong) { /* If there are less songs than the currently played song, * just pause and reset to no song. */ _music_wnd_cursong = 0; _song_is_active = false; } }
NetworkRecvStatus ServerNetworkAdminSocketHandler::Receive_ADMIN_JOIN(Packet *p) { if (this->status != ADMIN_STATUS_INACTIVE) return this->SendError(NETWORK_ERROR_NOT_EXPECTED); char password[NETWORK_PASSWORD_LENGTH]; p->Recv_string(password, sizeof(password)); if (StrEmpty(_settings_client.network.admin_password) || strcmp(password, _settings_client.network.admin_password) != 0) { /* Password is invalid */ return this->SendError(NETWORK_ERROR_WRONG_PASSWORD); } p->Recv_string(this->admin_name, sizeof(this->admin_name)); p->Recv_string(this->admin_version, sizeof(this->admin_version)); if (StrEmpty(this->admin_name) || StrEmpty(this->admin_version)) { /* no name or version supplied */ return this->SendError(NETWORK_ERROR_ILLEGAL_PACKET); } this->status = ADMIN_STATUS_ACTIVE; DEBUG(net, 1, "[admin] '%s' (%s) has connected", this->admin_name, this->admin_version); return this->SendProtocol(); }
/** * Find the requested blitter and return his class. * @param name the blitter to select. * @post Sets the blitter so GetCurrentBlitter() returns it too. */ static Blitter *SelectBlitter(const char *name) { #if defined(DEDICATED) const char *default_blitter = "null"; #else const char *default_blitter = "8bpp-optimized"; #if defined(WITH_COCOA) /* Some people reported lack of fullscreen support in 8 bpp mode. * While we prefer 8 bpp since it's faster, we will still have to test for support. */ if (!QZ_CanDisplay8bpp()) { /* The main display can't go to 8 bpp fullscreen mode. * We will have to switch to 32 bpp by default. */ default_blitter = "32bpp-anim"; } #endif /* defined(WITH_COCOA) */ #endif /* defined(DEDICATED) */ if (GetBlitters().size() == 0) return NULL; const char *bname = (StrEmpty(name)) ? default_blitter : name; Blitters::iterator it = GetBlitters().begin(); for (; it != GetBlitters().end(); it++) { BlitterFactoryBase *b = (*it).second; if (strcasecmp(bname, b->name) == 0) { Blitter *newb = b->CreateInstance(); delete *GetActiveBlitter(); *GetActiveBlitter() = newb; DEBUG(driver, 1, "Successfully %s blitter '%s'", StrEmpty(name) ? "probed" : "loaded", bname); return newb; } } return NULL; }
void InitFreeType() { ResetFontSizes(); if (StrEmpty(_freetype.small_font) && StrEmpty(_freetype.medium_font) && StrEmpty(_freetype.large_font)) { DEBUG(freetype, 1, "No font faces specified, using sprite fonts instead"); return; } if (FT_Init_FreeType(&_library) != FT_Err_Ok) { ShowInfoF("Unable to initialize FreeType, using sprite fonts instead"); return; } DEBUG(freetype, 2, "Initialized"); /* Load each font */ LoadFreeTypeFont(_freetype.small_font, &_face_small, "small"); LoadFreeTypeFont(_freetype.medium_font, &_face_medium, "medium"); LoadFreeTypeFont(_freetype.large_font, &_face_large, "large"); /* Set each font size */ if (_face_small != NULL) { SetFontGeometry(_face_small, FS_SMALL, _freetype.small_size); } if (_face_medium != NULL) { SetFontGeometry(_face_medium, FS_NORMAL, _freetype.medium_size); } if (_face_large != NULL) { SetFontGeometry(_face_large, FS_LARGE, _freetype.large_size); } }
void CDurableFile::Init(CDurableFileController* pcController, char* szFileName, char* szRewriteName) { mbAddedToController = FALSE; mpcController = pcController; InitBasic(); if (!StrEmpty(szFileName)) { mcPrimaryDiskFile.Init(szFileName); mcLogFile.Init(&mcPrimaryDiskFile); mcPrimaryFile.Init(&mcLogFile); mszFileName.Init(mcPrimaryDiskFile.mszFileName); } else { gcLogger.Error2(__METHOD__, " Primary DurableFile file name is NULL.", NULL); } if (!StrEmpty(szRewriteName)) { mcRewriteDiskFile.Init(szRewriteName); mcRewriteFile.Init(&mcRewriteDiskFile); mszRewriteName.Init(mcRewriteDiskFile.mszFileName); } else { memset(&mszRewriteName, 0, sizeof(CChars)); memset(&mcRewriteFile, 0, sizeof(CFileBasic)); } }
/** * Place a sign at the given coordinates. Ownership of sign has * no effect whatsoever except for the colour the sign gets for easy recognition, * but everybody is able to rename/remove it. * @param tile tile to place sign at * @param flags type of operation * @param p1 unused * @param p2 unused * @param text unused * @return the cost of this operation or an error */ CommandCost CmdPlaceSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { /* Try to locate a new sign */ if (!Sign::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_SIGNS); /* Check sign text length if any */ if (!StrEmpty(text) && Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR; /* When we execute, really make the sign */ if (flags & DC_EXEC) { Sign *si = new Sign(_current_company); int x = TileX(tile) * TILE_SIZE; int y = TileY(tile) * TILE_SIZE; si->x = x; si->y = y; si->z = GetSlopeZ(x, y); if (!StrEmpty(text)) { si->name = strdup(text); } si->UpdateVirtCoord(); InvalidateWindowData(WC_SIGN_LIST, 0, 0); _new_sign_id = si->index; } return CommandCost(); }
/* virtual */ void ParseFile() { this->StringReader::ParseFile(); if (StrEmpty(_lang.name) || StrEmpty(_lang.own_name) || StrEmpty(_lang.isocode)) { error("Language must include ##name, ##ownname and ##isocode"); } }
void main() { int i; char c, *p = "God bye!", *q = "God luck!"; HString t, s, r; InitString(t); InitString(s); InitString(r); StrAssign(t, p); printf("串t为"); StrPrint(t); printf("串长为%d,串空否?%d(1:空 0:否)\n", StrLength(t), StrEmpty(t)); StrAssign(s, q); printf("串s为"); StrPrint(s); i = StrCompare(s, t); if (i < 0) c = '<'; else if (i == 0) c = '='; else c = '>'; printf("串s%c串t\n", c); Concat(r, t, s); printf("串t连接串s产生的串r为"); StrPrint(r); StrAssign(s, "oo"); printf("串s为"); StrPrint(s); StrAssign(t, "o"); printf("串t为"); StrPrint(t); Replace(r, t, s); printf("把串r中和串t相同的子串用串s代替后,串r为"); StrPrint(r); ClearString(s); printf("串s清空后,串长为%d,空否?%d(1:空 0:否)\n", StrLength(s), StrEmpty(s)); SubString(s, r, 6, 4); printf("串s为从串r的第6个字符起的4个字符,长度为%d,串s为", s.length); StrPrint(s); StrCopy(t, r); printf("由串r复制得串t,串t为"); StrPrint(t); StrInsert(t, 6, s); printf("在串t的第6个字符前插入串s后,串t为"); StrPrint(t); StrDelete(t, 1, 5); printf("从串t的第1个字符起删除5个字符后,串t为"); StrPrint(t); printf("%d是从串t的第1个字符起,和串s相同的第1个子串的位置\n", Index(t, s, 1)); printf("%d是从串t的第2个字符起,和串s相同的第1个子串的位置\n", Index(t, s, 2)); DestroyString(t); }
void test() { char ch[2][255]; int i=0,pos=1; SString S[2],T; for(i=0;i<2;i++) { printf("ÊäÈë×Ö·û´®%d:",i+1); scanf("%s",ch[i]); StrAssign(S[i],ch[i]); } for(i=0;i<2;i++) { printf("Êä³ö×Ö·û´®%d:",i+1); StrPrint(S[i]); printf("×Ö·ûÊÇ·ñΪ¿Õ%d(1-·Ç 0-¿Õ),×Ö·û´®³¤¶È:%d\n",!StrEmpty(S[i]),StrLength(S[i])); } printf("---Á¬½Ó´®--\n"); Concat(T,S[0],S[1]); printf("Á¬½Óºó:"); StrPrint(T); printf("---Çó×Ó´®---\n"); printf("ÊäÈë×Ö´®:"); scanf("%s",ch[0]); printf("ÊäÈë²éÕÒλÖÃ:"); scanf("%d",&pos); StrAssign(S[0],ch[0]); i=Index(T,S[0],pos); printf("²éÕÒµ½µÄλÖÃ:%d\n",i); printf("---Ìæ´ú---\n"); printf("ÊäÈëÌæ´úµÄ×Ó´®:"); scanf("%s",ch[1]); printf("Ìæ»»%sºó:",ch[0]); StrAssign(S[1],ch[1]); Replace(T,S[0],S[1]); StrPrint(T); printf("±È½Ï×Ö·û´®:S%d%cS%d\n",1,StrCompare(S[0],S[1])>0?'>':StrCompare(S[0],S[1])<0?'<':'=',2); StrCopy(S[0],S[1]); printf("---¸´Öƺó---\n"); for(i=0;i<2;i++) { printf("Êä³ö×Ö·û´®%d:",i+1); StrPrint(S[i]); printf("×Ö·ûÊÇ·ñΪ¿Õ%d(1-·Ç 0-¿Õ),×Ö·û´®³¤¶È:%d\n",!StrEmpty(S[i]),StrLength(S[i])); } printf("±È½Ï×Ö·û´®:S%d%cS%d\n",1,StrCompare(S[0],S[1])>0?'>':StrCompare(S[0],S[1])<0?'<':'=',2); }
/** * A client has requested the names of some NewGRFs. * * Replying this can be tricky as we have a limit of SEND_MTU bytes * in the reply packet and we can send up to 100 bytes per NewGRF * (GRF ID, MD5sum and NETWORK_GRF_NAME_LENGTH bytes for the name). * As SEND_MTU is _much_ less than 100 * NETWORK_MAX_GRF_COUNT, it * could be that a packet overflows. To stop this we only reply * with the first N NewGRFs so that if the first N + 1 NewGRFs * would be sent, the packet overflows. * in_reply and in_reply_count are used to keep a list of GRFs to * send in the reply. */ DEF_UDP_RECEIVE_COMMAND(Server, PACKET_UDP_CLIENT_GET_NEWGRFS) { uint8 num_grfs; uint i; const GRFConfig *in_reply[NETWORK_MAX_GRF_COUNT]; uint8 in_reply_count = 0; size_t packet_len = 0; DEBUG(net, 6, "[udp] newgrf data request from %s", client_addr->GetAddressAsString()); num_grfs = p->Recv_uint8 (); if (num_grfs > NETWORK_MAX_GRF_COUNT) return; for (i = 0; i < num_grfs; i++) { GRFConfig c; const GRFConfig *f; this->Recv_GRFIdentifier(p, &c); /* Find the matching GRF file */ f = FindGRFConfig(c.grfid, c.md5sum); if (f == NULL) continue; // The GRF is unknown to this server /* If the reply might exceed the size of the packet, only reply * the current list and do not send the other data. * The name could be an empty string, if so take the filename. */ packet_len += sizeof(c.grfid) + sizeof(c.md5sum) + min(strlen((f->name != NULL && !StrEmpty(f->name)) ? f->name : f->filename) + 1, (size_t)NETWORK_GRF_NAME_LENGTH); if (packet_len > SEND_MTU - 4) { // 4 is 3 byte header + grf count in reply break; } in_reply[in_reply_count] = f; in_reply_count++; } if (in_reply_count == 0) return; Packet packet(PACKET_UDP_SERVER_NEWGRFS); packet.Send_uint8(in_reply_count); for (i = 0; i < in_reply_count; i++) { char name[NETWORK_GRF_NAME_LENGTH]; /* The name could be an empty string, if so take the filename */ strecpy(name, (in_reply[i]->name != NULL && !StrEmpty(in_reply[i]->name)) ? in_reply[i]->name : in_reply[i]->filename, lastof(name)); this->Send_GRFIdentifier(&packet, in_reply[i]); packet.Send_string(name); } this->SendPacket(&packet, client_addr); }
void main() { int i; char c,*p="God bye!",*q="God luck!"; HString t,s,r; InitString(&t); /* HString类型必需初始化 */ InitString(&s); InitString(&r); StrAssign(&t,p); printf("串t为: "); StrPrint(t); printf("串长为%d 串空否?%d(1:空 0:否)\n",StrLength(t),StrEmpty(t)); StrAssign(&s,q); printf("串s为: "); StrPrint(s); i=StrCompare(s,t); if(i<0) c='<'; else if(i==0) c='='; else c='>'; printf("串s%c串t\n",c); Concat(&r,t,s); printf("串t联接串s产生的串r为: "); StrPrint(r); StrAssign(&s,"oo"); printf("串s为: "); StrPrint(s); StrAssign(&t,"o"); printf("串t为: "); StrPrint(t); Replace(&r,t,s); printf("把串r中和串t相同的子串用串s代替后,串r为:\n"); StrPrint(r); ClearString(&s); printf("串s清空后,串长为%d 空否?%d(1:空 0:否)\n",StrLength(s),StrEmpty(s)); SubString(&s,r,6,4); printf("串s为从串r的第6个字符起的4个字符,长度为%d 串s为: ",s.length); StrPrint(s); StrCopy(&t,r); printf("复制串t为串r,串t为: "); StrPrint(t); StrInsert(&t,6,s); printf("在串t的第6个字符前插入串s后,串t为: "); StrPrint(t); StrDelete(&t,1,5); printf("从串t的第1个字符起删除5个字符后,串t为: "); StrPrint(t); printf("%d是从串t的第1个字符起,和串s相同的第1个子串的位置\n",Index(t,s,1)); printf("%d是从串t的第2个字符起,和串s相同的第1个子串的位置\n",Index(t,s,2)); }
/** * (Re)initialize the freetype related things, i.e. load the non-sprite fonts. * @param monospace Whether to initialise the monospace or regular fonts. */ void InitFreeType(bool monospace) { ResetFontSizes(monospace); ResetGlyphCache(monospace); if (monospace) { UnloadFace(&_face_mono); } else { UnloadFace(&_face_small); UnloadFace(&_face_medium); UnloadFace(&_face_large); } if (StrEmpty(_freetype.small_font) && StrEmpty(_freetype.medium_font) && StrEmpty(_freetype.large_font) && StrEmpty(_freetype.mono_font)) { DEBUG(freetype, 1, "No font faces specified, using sprite fonts instead"); return; } if (_library == NULL) { if (FT_Init_FreeType(&_library) != FT_Err_Ok) { ShowInfoF("Unable to initialize FreeType, using sprite fonts instead"); return; } DEBUG(freetype, 2, "Initialized"); } /* Load each font */ if (monospace) { LoadFreeTypeFont(_freetype.mono_font , &_face_mono, "mono"); if (_face_mono != NULL) { SetFontGeometry(_face_mono, FS_MONO, _freetype.mono_size); } } else { LoadFreeTypeFont(_freetype.small_font, &_face_small, "small"); LoadFreeTypeFont(_freetype.medium_font, &_face_medium, "medium"); LoadFreeTypeFont(_freetype.large_font, &_face_large, "large"); /* Set each font size */ if (_face_small != NULL) { SetFontGeometry(_face_small, FS_SMALL, _freetype.small_size); } if (_face_medium != NULL) { SetFontGeometry(_face_medium, FS_NORMAL, _freetype.medium_size); } if (_face_large != NULL) { SetFontGeometry(_face_large, FS_LARGE, _freetype.large_size); } } }
/** * Retrieve keyboard layout from language string or (if set) config file. * Also check for invalid characters. */ void GetKeyboardLayout() { char keyboard[2][OSK_KEYBOARD_ENTRIES * 4 + 1]; char errormark[2][OSK_KEYBOARD_ENTRIES + 1]; // used for marking invalid chars bool has_error = false; // true when an invalid char is detected if (StrEmpty(_keyboard_opt[0])) { GetString(keyboard[0], STR_OSK_KEYBOARD_LAYOUT, lastof(keyboard[0])); } else { strecpy(keyboard[0], _keyboard_opt[0], lastof(keyboard[0])); } if (StrEmpty(_keyboard_opt[1])) { GetString(keyboard[1], STR_OSK_KEYBOARD_LAYOUT_CAPS, lastof(keyboard[1])); } else { strecpy(keyboard[1], _keyboard_opt[1], lastof(keyboard[1])); } for (uint j = 0; j < 2; j++) { const char *kbd = keyboard[j]; bool ended = false; for (uint i = 0; i < OSK_KEYBOARD_ENTRIES; i++) { _keyboard[j][i] = Utf8Consume(&kbd); /* Be lenient when the last characters are missing (is quite normal) */ if (_keyboard[j][i] == '\0' || ended) { ended = true; _keyboard[j][i] = ' '; continue; } if (IsPrintable(_keyboard[j][i])) { errormark[j][i] = ' '; } else { has_error = true; errormark[j][i] = '^'; _keyboard[j][i] = ' '; } } } if (has_error) { ShowInfoF("The keyboard layout you selected contains invalid chars. Please check those chars marked with ^."); ShowInfoF("Normal keyboard: %s", keyboard[0]); ShowInfoF(" %s", errormark[0]); ShowInfoF("Caps Lock: %s", keyboard[1]); ShowInfoF(" %s", errormark[1]); } }
/* virtual */ void BaseNetworkContentDownloadStatusWindow::DrawWidget(const Rect &r, int widget) const { if (widget != WID_NCDS_BACKGROUND) return; /* Draw nice progress bar :) */ DrawFrameRect(r.left + 20, r.top + 4, r.left + 20 + (int)((this->width - 40LL) * this->downloaded_bytes / this->total_bytes), r.top + 14, COLOUR_MAUVE, FR_NONE); int y = r.top + 20; SetDParam(0, this->downloaded_bytes); SetDParam(1, this->total_bytes); SetDParam(2, this->downloaded_bytes * 100LL / this->total_bytes); DrawString(r.left + 2, r.right - 2, y, STR_CONTENT_DOWNLOAD_PROGRESS_SIZE, TC_FROMSTRING, SA_HOR_CENTER); StringID str; if (this->downloaded_bytes == this->total_bytes) { str = STR_CONTENT_DOWNLOAD_COMPLETE; } else if (!StrEmpty(this->name)) { SetDParamStr(0, this->name); SetDParam(1, this->downloaded_files); SetDParam(2, this->total_files); str = STR_CONTENT_DOWNLOAD_FILE; } else { str = STR_CONTENT_DOWNLOAD_INITIALISE; } y += FONT_HEIGHT_NORMAL + 5; DrawStringMultiLine(r.left + 2, r.right - 2, y, y + FONT_HEIGHT_NORMAL * 2, str, TC_FROMSTRING, SA_CENTER); }
/** * Rename a depot. * @param tile unused * @param flags type of operation * @param p1 id of depot * @param p2 unused * @param text the new name or an empty string when resetting to the default * @return the cost of this operation or an error */ CommandCost CmdRenameDepot(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { Depot *d = Depot::GetIfValid(p1); if (d == NULL) return CMD_ERROR; CommandCost ret = CheckTileOwnership(d->xy); if (ret.Failed()) return ret; bool reset = StrEmpty(text); if (!reset) { if (Utf8StringLength(text) >= MAX_LENGTH_DEPOT_NAME_CHARS) return CMD_ERROR; if (!IsUniqueDepotName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE); } if (flags & DC_EXEC) { free(d->name); if (reset) { d->name = NULL; MakeDefaultName(d); } else { d->name = strdup(text); } /* Update the orders and depot */ SetWindowClassesDirty(WC_VEHICLE_ORDERS); SetWindowDirty(WC_VEHICLE_DEPOT, d->xy); /* Update the depot list */ VehicleType vt = GetDepotVehicleType(d->xy); SetWindowDirty(GetWindowClassForVehicleType(vt), VehicleListIdentifier(VL_DEPOT_LIST, vt, GetTileOwner(d->xy), d->index).Pack()); } return CommandCost(); }
/** * Add a new item to the linked gamelist. If the IP and Port match * return the existing item instead of adding it again * @param address the address of the to-be added item * @return a point to the newly added or already existing item */ NetworkGameList *NetworkGameListAddItem(NetworkAddress address) { const char *hostname = address.GetHostname(); /* Do not query the 'any' address. */ if (StrEmpty(hostname) || strcmp(hostname, "0.0.0.0") == 0 || strcmp(hostname, "::") == 0) { return NULL; } NetworkGameList *item, *prev_item; prev_item = NULL; for (item = _network_game_list; item != NULL; item = item->next) { if (item->address == address) return item; prev_item = item; } item = CallocT<NetworkGameList>(1); item->next = NULL; item->address = address; if (prev_item == NULL) { _network_game_list = item; } else { prev_item->next = item; } DEBUG(net, 4, "[gamelist] added server to list"); UpdateNetworkGameWindow(false); return item; }
/** * Change the name of the president. * @param tile unused * @param flags operation to perform * @param p1 unused * @param p2 unused * @param text the new name or an empty string when resetting to the default * @return the cost of this operation or an error */ CommandCost CmdRenamePresident(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { bool reset = StrEmpty(text); if (!reset) { if (Utf8StringLength(text) >= MAX_LENGTH_PRESIDENT_NAME_CHARS) return CMD_ERROR; if (!IsUniquePresidentName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE); } if (flags & DC_EXEC) { Company *c = Company::Get(_current_company); free(c->president_name); if (reset) { c->president_name = NULL; } else { c->president_name = strdup(text); if (c->name_1 == STR_SV_UNNAMED && c->name == NULL) { char buf[80]; snprintf(buf, lengthof(buf), "%s Transport", text); DoCommand(0, 0, 0, DC_EXEC, CMD_RENAME_COMPANY, buf); } } MarkWholeScreenDirty(); CompanyAdminUpdate(c); } return CommandCost(); }
/** * Rename a sign. If the new name of the sign is empty, we assume * the user wanted to delete it. So delete it. Ownership of signs * has no meaning/effect whatsoever except for eyecandy * @param tile unused * @param flags type of operation * @param p1 index of the sign to be renamed/removed * @param p2 unused * @param text the new name or an empty string when resetting to the default * @return the cost of this operation or an error */ CommandCost CmdRenameSign(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { Sign *si = Sign::GetIfValid(p1); if (si == NULL) return CMD_ERROR; /* Rename the signs when empty, otherwise remove it */ if (!StrEmpty(text)) { if (Utf8StringLength(text) >= MAX_LENGTH_SIGN_NAME_CHARS) return CMD_ERROR; if (flags & DC_EXEC) { /* Delete the old name */ free(si->name); /* Assign the new one */ si->name = strdup(text); si->owner = _current_company; si->UpdateVirtCoord(); InvalidateWindowData(WC_SIGN_LIST, 0, 1); } } else { // Delete sign if (flags & DC_EXEC) { si->sign.MarkDirty(); delete si; InvalidateWindowData(WC_SIGN_LIST, 0, 0); } } return CommandCost(); }
static void SelectSongToPlay() { uint i = 0; uint j = 0; memset(_cur_playlist, 0, sizeof(_cur_playlist)); do { const char *filename = BaseMusic::GetUsedSet()->files[_playlists[_msf.playlist][i] - 1].filename; /* We are now checking for the existence of that file prior * to add it to the list of available songs */ if (!StrEmpty(filename) && FioCheckFileExists(filename, GM_DIR)) { _cur_playlist[j] = _playlists[_msf.playlist][i]; j++; } } while (_playlists[_msf.playlist][++i] != 0 && j < lengthof(_cur_playlist) - 1); /* Do not shuffle when on the intro-start window, as the song to play has to be the original TTD Theme*/ if (_msf.shuffle && _game_mode != GM_MENU) { i = 500; do { uint32 r = InteractiveRandom(); byte *a = &_cur_playlist[GB(r, 0, 5)]; byte *b = &_cur_playlist[GB(r, 8, 5)]; if (*a != 0 && *b != 0) { byte t = *a; *a = *b; *b = t; } } while (--i); } }
/** Save HighScore table to file */ void SaveToHighScore() { FILE *fp = fopen(_highscore_file, "wb"); if (fp != NULL) { uint i; HighScore *hs; for (i = 0; i < SP_SAVED_HIGHSCORE_END; i++) { for (hs = _highscore_table[i]; hs != endof(_highscore_table[i]); hs++) { /* First character is a command character, so strlen will fail on that */ byte length = min(sizeof(hs->company), StrEmpty(hs->company) ? 0 : (int)strlen(&hs->company[1]) + 1); if (fwrite(&length, sizeof(length), 1, fp) != 1 || // write away string length fwrite(hs->company, length, 1, fp) > 1 || // Yes... could be 0 bytes too fwrite(&hs->score, sizeof(hs->score), 1, fp) != 1 || fwrite(" ", 2, 1, fp) != 1) { // XXX - placeholder for hs->title, not saved anymore; compatibility DEBUG(misc, 1, "Could not save highscore."); i = SP_SAVED_HIGHSCORE_END; break; } } } fclose(fp); } }
/** The return of the client's request of the names of some NewGRFs */ DEF_UDP_RECEIVE_COMMAND(Client, PACKET_UDP_SERVER_NEWGRFS) { uint8 num_grfs; uint i; DEBUG(net, 6, "[udp] newgrf data reply from %s", client_addr->GetAddressAsString()); num_grfs = p->Recv_uint8 (); if (num_grfs > NETWORK_MAX_GRF_COUNT) return; for (i = 0; i < num_grfs; i++) { char *unknown_name; char name[NETWORK_GRF_NAME_LENGTH]; GRFConfig c; this->Recv_GRFIdentifier(p, &c); p->Recv_string(name, sizeof(name)); /* An empty name is not possible under normal circumstances * and causes problems when showing the NewGRF list. */ if (StrEmpty(name)) continue; /* Finds the fake GRFConfig for the just read GRF ID and MD5sum tuple. * If it exists and not resolved yet, then name of the fake GRF is * overwritten with the name from the reply. */ unknown_name = FindUnknownGRFName(c.grfid, c.md5sum, false); if (unknown_name != NULL && strcmp(unknown_name, UNKNOWN_GRF_NAME_PLACEHOLDER) == 0) { ttd_strlcpy(unknown_name, name, NETWORK_GRF_NAME_LENGTH); } } }
/** * Rename a waypoint. * @param tile unused * @param flags type of operation * @param p1 id of waypoint * @param p2 unused * @param text the new name or an empty string when resetting to the default * @return the cost of this operation or an error */ CommandCost CmdRenameWaypoint(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { Waypoint *wp = Waypoint::GetIfValid(p1); if (wp == NULL) return CMD_ERROR; if (wp->owner != OWNER_NONE) { CommandCost ret = CheckOwnership(wp->owner); if (ret.Failed()) return ret; } bool reset = StrEmpty(text); if (!reset) { if (Utf8StringLength(text) >= MAX_LENGTH_STATION_NAME_CHARS) return CMD_ERROR; if (!IsUniqueWaypointName(text)) return_cmd_error(STR_ERROR_NAME_MUST_BE_UNIQUE); } if (flags & DC_EXEC) { free(wp->name); wp->name = reset ? NULL : strdup(text); wp->UpdateVirtCoord(); } return CommandCost(); }
virtual void DrawWidget(const Rect &r, int widget) const { switch (widget) { case ANGRFW_GRF_LIST: { GfxFillRect(r.left + 1, r.top + 1, r.right - 1, r.bottom - 1, 0xD7); uint y = r.top + WD_FRAMERECT_TOP; uint min_index = this->vscroll.GetPosition(); uint max_index = min(min_index + this->vscroll.GetCapacity(), this->grfs.Length()); for (uint i = min_index; i < max_index; i++) { const GRFConfig *c = this->grfs[i]; bool h = c == this->sel; const char *text = (!StrEmpty(c->name)) ? c->name : c->filename; /* Draw selection background */ if (h) GfxFillRect(r.left + 1, y, r.right - 1, y + this->resize.step_height - 1, 156); DrawString(r.left + WD_FRAMERECT_LEFT, r.right - WD_FRAMERECT_RIGHT, y, text, h ? TC_WHITE : TC_ORANGE); y += this->resize.step_height; } break; } case ANGRFW_GRF_INFO: if (this->sel != NULL) { ShowNewGRFInfo(this->sel, r.left + WD_FRAMERECT_LEFT, r.top + WD_FRAMERECT_TOP, r.right - WD_FRAMERECT_RIGHT, r.bottom - WD_FRAMERECT_BOTTOM, false); } break; } }
void Create(GList1 &L, SString S) { SString emp, sub, hsub; GList1 p; StrAssign(emp, "()"); if (!(L = (GList1)malloc(sizeof(GLNode1)))) exit(OVERFLOW); if (!StrCompare(S, emp)) { L->tag = LIST; L->hp = NULL; } else if (StrLength(S) == 1) { L->tag = ATOM; L->atom = S[1]; } else { L->tag = LIST; SubString(sub, S, 2, StrLength(S) - 2); sever(sub, hsub); Create(L->hp, hsub); p = L->hp; while (!StrEmpty(sub)) { sever(sub, hsub); Create(p->tp, hsub); p = p->tp; } } L->tp = NULL; }
/** * Construct a pathname for a screenshot file. * @param default_fn Default filename. * @param ext Extension to use. * @param crashlog Create path for crash.png * @return Pathname for a screenshot file. */ static const char *MakeScreenshotName(const char *default_fn, const char *ext, bool crashlog = false) { bool generate = StrEmpty(_screenshot_name); if (generate) { if (_game_mode == GM_EDITOR || _game_mode == GM_MENU || _local_company == COMPANY_SPECTATOR) { strecpy(_screenshot_name, default_fn, lastof(_screenshot_name)); } else { GenerateDefaultSaveName(_screenshot_name, lastof(_screenshot_name)); } } /* Add extension to screenshot file */ size_t len = strlen(_screenshot_name); snprintf(&_screenshot_name[len], lengthof(_screenshot_name) - len, ".%s", ext); const char *screenshot_dir = crashlog ? _personal_dir : FiosGetScreenshotDir(); for (uint serial = 1;; serial++) { if (snprintf(_full_screenshot_name, lengthof(_full_screenshot_name), "%s%s", screenshot_dir, _screenshot_name) >= (int)lengthof(_full_screenshot_name)) { /* We need more characters than MAX_PATH -> end with error */ _full_screenshot_name[0] = '\0'; break; } if (!generate) break; // allow overwriting of non-automatic filenames if (!FileExists(_full_screenshot_name)) break; /* If file exists try another one with same name, but just with a higher index */ snprintf(&_screenshot_name[len], lengthof(_screenshot_name) - len, "#%u.%s", serial, ext); } return _full_screenshot_name; }
static void MakeNewGameDone() { SettingsDisableElrail(_settings_game.vehicle.disable_elrails); /* In a dedicated server, the server does not play */ if (_network_dedicated || BlitterFactoryBase::GetCurrentBlitter()->GetScreenDepth() == 0) { SetLocalCompany(COMPANY_SPECTATOR); IConsoleCmdExec("exec scripts/game_start.scr 0"); return; } /* Create a single company */ DoStartupNewCompany(false); Company *c = Company::Get(COMPANY_FIRST); c->settings = _settings_client.company; IConsoleCmdExec("exec scripts/game_start.scr 0"); SetLocalCompany(COMPANY_FIRST); InitializeRailGUI(); #ifdef ENABLE_NETWORK /* We are the server, we start a new company (not dedicated), * so set the default password *if* needed. */ if (_network_server && !StrEmpty(_settings_client.network.default_company_pass)) { NetworkChangeCompanyPassword(_local_company, _settings_client.network.default_company_pass); } #endif /* ENABLE_NETWORK */ if (_settings_client.gui.pause_on_newgame) DoCommandP(0, PM_PAUSED_NORMAL, 1, CMD_PAUSE); MarkWholeScreenDirty(); }
/** Perform the delayed (thread safe) insertion into the game list */ static void NetworkGameListHandleDelayedInsert() { _network_game_list_mutex->BeginCritical(); while (_network_game_delayed_insertion_list != NULL) { NetworkGameList *ins_item = _network_game_delayed_insertion_list; _network_game_delayed_insertion_list = ins_item->next; NetworkGameList *item = NetworkGameListAddItem(ins_item->address); if (item != NULL) { if (StrEmpty(item->info.server_name)) { ClearGRFConfigList(&item->info.grfconfig); memset(&item->info, 0, sizeof(item->info)); strecpy(item->info.server_name, ins_item->info.server_name, lastof(item->info.server_name)); strecpy(item->info.hostname, ins_item->info.hostname, lastof(item->info.hostname)); item->online = false; } item->manually |= ins_item->manually; if (item->manually) NetworkRebuildHostList(); UpdateNetworkGameWindow(false); } free(ins_item); } _network_game_list_mutex->EndCritical(); }
/** * Create a new story page. * @param tile unused. * @param flags type of operation * @param p1 various bitstuffed elements * - p1 = (bit 0 - 7) - Company for which this story page belongs to. * @param p2 unused. * @param text Title of the story page. Null is allowed in wich case a generic page title is provided by OpenTTD. * @return the cost of this operation or an error */ CommandCost CmdCreateStoryPage(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text) { if (!StoryPage::CanAllocateItem()) return CMD_ERROR; CompanyID company = (CompanyID)GB(p1, 0, 8); if (_current_company != OWNER_DEITY) return CMD_ERROR; if (company != INVALID_COMPANY && !Company::IsValidID(company)) return CMD_ERROR; if (flags & DC_EXEC) { if (_story_page_pool.items == 0) { /* Initialize the next sort value variable. */ _story_page_next_sort_value = 0; } StoryPage *s = new StoryPage(); s->sort_value = _story_page_next_sort_value; s->date = _date; s->company = company; if (StrEmpty(text)) { s->title = NULL; } else { s->title = strdup(text); } InvalidateWindowClassesData(WC_STORY_BOOK, -1); if (StoryPage::GetNumItems() == 1) InvalidateWindowData(WC_MAIN_TOOLBAR, 0); _new_story_page_id = s->index; _story_page_next_sort_value++; } return CommandCost(); }
/** * Get the blitter factory with the given name. * @param name the blitter factory to select. * @return The blitter factory, or nullptr when there isn't one with the wanted name. */ static BlitterFactory *GetBlitterFactory(const char *name) { #if defined(DEDICATED) const char *default_blitter = "null"; #else const char *default_blitter = "8bpp-optimized"; #if defined(WITH_COCOA) /* Some people reported lack of fullscreen support in 8 bpp mode. * While we prefer 8 bpp since it's faster, we will still have to test for support. */ if (!QZ_CanDisplay8bpp()) { /* The main display can't go to 8 bpp fullscreen mode. * We will have to switch to 32 bpp by default. */ default_blitter = "32bpp-anim"; } #endif /* defined(WITH_COCOA) */ #endif /* defined(DEDICATED) */ if (GetBlitters().size() == 0) return nullptr; const char *bname = (StrEmpty(name)) ? default_blitter : name; Blitters::iterator it = GetBlitters().begin(); for (; it != GetBlitters().end(); it++) { BlitterFactory *b = (*it).second; if (strcasecmp(bname, b->name) == 0) { return b; } } return nullptr; }
/** * Get the hostname; in case it wasn't given the * IPv4 dotted representation is given. * @return the hostname */ const char *NetworkAddress::GetHostname() { if (StrEmpty(this->hostname) && this->address.ss_family != AF_UNSPEC) { assert(this->address_length != 0); getnameinfo((struct sockaddr *)&this->address, this->address_length, this->hostname, sizeof(this->hostname), NULL, 0, NI_NUMERICHOST); } return this->hostname; }