3 * =============================================================================
4 * SourceMod Sample Extension
5 * Copyright (C) 2004-2007 AlliedModders LLC. All rights reserved.
6 * =============================================================================
8 * This program is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, version 3.0, as published by the
10 * Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License along with
18 * this program. If not, see <http://www.gnu.org/licenses/>.
20 * As a special exception, AlliedModders LLC gives you permission to link the
21 * code of this program (as well as its derivative works) to "Half-Life 2," the
22 * "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software
23 * by the Valve Corporation. You must obey the GNU General Public License in
24 * all respects for all other code used. Additionally, AlliedModders LLC grants
25 * this exception to all derivative works. AlliedModders LLC defines further
26 * exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007),
27 * or <http://www.sourcemod.net/license.php>.
38 unsigned int obj_index;
42 int _SortObjectNamesDescending(const void *ptr1, const void *ptr2);
43 unsigned int strncopy(char *dest, const char *src, size_t count);
44 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...);
46 TopMenu::TopMenu(ITopMenuObjectCallbacks *callbacks)
53 if (playerhelpers->IsServerActivated())
55 CreatePlayers(playerhelpers->GetMaxClients());
61 /* Delete all categories */
63 while (i >= m_Categories.size())
65 RemoveFromMenu(m_Categories[i - 1]->obj->object_id);
68 /* Remove all objects */
69 for (i = 0; i < m_Objects.size(); i++)
71 assert(m_Objects[i]->is_free == true);
75 m_pTitle->OnTopMenuObjectRemoved(this, 0);
77 /* Delete all cached config entries */
78 for (i = 0; i < m_Config.cats.size(); i++)
80 delete m_Config.cats[i];
84 for (i = 0; i <= (size_t)m_max_clients; i++)
86 TearDownClient(&m_clients[i]);
91 void TopMenu::OnClientConnected(int client)
93 if (m_clients == NULL)
98 topmenu_player_t *player = &m_clients[client];
99 TearDownClient(player);
102 void TopMenu::OnClientDisconnected(int client)
104 if (m_clients == NULL)
109 topmenu_player_t *player = &m_clients[client];
110 TearDownClient(player);
113 void TopMenu::OnServerActivated(int max_clients)
115 if (m_clients == NULL)
117 CreatePlayers(max_clients);
121 unsigned int TopMenu::AddToMenu(const char *name,
122 TopMenuObjectType type,
123 ITopMenuObjectCallbacks *callbacks,
124 IdentityToken_t *owner,
130 if (type == TopMenuObject_Category && parent != 0)
134 else if (type == TopMenuObject_Item && parent == 0)
138 else if (m_ObjLookup.retrieve(name) != NULL)
142 else if (type != TopMenuObject_Item && type != TopMenuObject_Category)
147 /* If we're adding an item, make sure the parent is valid,
148 * and that the parent is a category.
150 topmenu_object_t *parent_obj = NULL;
151 topmenu_category_t *parent_cat = NULL;
152 if (type == TopMenuObject_Item)
154 /* Check parent index. Note it will be >= 1 here. */
155 if (parent > m_Objects.size() || m_Objects[parent - 1]->is_free)
159 parent_obj = m_Objects[parent - 1];
161 /* Find an equivalent pointer in the category array. */
162 for (size_t i = 0; i < m_Categories.size(); i++)
164 if (m_Categories[i]->obj == parent_obj)
166 parent_cat = m_Categories[i];
171 /* If none was found, leave. */
172 if (parent_cat == NULL)
178 /* Re-use an old object pointer if we can. */
179 topmenu_object_t *obj = NULL;
180 for (size_t i = 0; i < m_Objects.size(); i++)
182 if (m_Objects[i]->is_free == true)
189 /* Otherwise, allocate a new one. */
192 obj = new topmenu_object_t;
193 obj->object_id = ((unsigned int)m_Objects.size()) + 1;
194 m_Objects.push_back(obj);
197 /* Initialize the object's properties. */
198 obj->callbacks = callbacks;
202 obj->is_free = false;
203 obj->parent = parent_obj;
204 strncopy(obj->name, name, sizeof(obj->name));
205 strncopy(obj->cmdname, cmdname ? cmdname : "", sizeof(obj->cmdname));
207 if (obj->type == TopMenuObject_Category)
209 /* Create a new category entry */
210 topmenu_category_t *cat = new topmenu_category_t;
212 cat->reorder = false;
215 /* Add it, then update our serial change number. */
216 m_Categories.push_back(cat);
219 /* Updating sorting info */
220 m_bCatsNeedResort = true;
222 else if (obj->type == TopMenuObject_Item)
224 /* Update the category, mark it as needing changes */
225 parent_cat->obj_list.push_back(obj);
226 parent_cat->reorder = true;
227 parent_cat->serial++;
229 /* If the category just went from 0 to 1 items, mark it as
230 * changed, so clients get the category drawn.
232 if (parent_cat->obj_list.size() == 1)
238 m_ObjLookup.insert(name, obj);
240 return obj->object_id;
243 void TopMenu::RemoveFromMenu(unsigned int object_id)
246 || object_id > m_Objects.size()
247 || m_Objects[object_id - 1]->is_free)
252 topmenu_object_t *obj = m_Objects[object_id - 1];
254 m_ObjLookup.remove(obj->name);
256 if (obj->type == TopMenuObject_Category)
258 /* Find it in the category list. */
259 for (size_t i = 0; i < m_Categories.size(); i++)
261 if (m_Categories[i]->obj == obj)
263 /* Mark all children as removed + free. Note we could
264 * call into RemoveMenuItem() for this, but it'd be very
267 topmenu_category_t *cat = m_Categories[i];
268 for (size_t j = 0; j < m_Categories[i]->obj_list.size(); j++)
270 cat->obj_list[j]->callbacks->OnTopMenuObjectRemoved(this, cat->obj_list[j]->object_id);
271 cat->obj_list[j]->is_free = true;
274 /* Remove the category from the list, then delete it. */
275 m_Categories.erase(m_Categories.iterAt(i));
281 /* Update the root as changed. */
283 m_bCatsNeedResort = true;
285 else if (obj->type == TopMenuObject_Item)
287 /* Find the category this item is in. */
288 topmenu_category_t *parent_cat = NULL;
289 for (size_t i = 0; i < m_Categories.size(); i++)
291 if (m_Categories[i]->obj == obj->parent)
293 parent_cat = m_Categories[i];
298 /* Erase it from the category's lists. */
301 for (size_t i = 0; i < parent_cat->obj_list.size(); i++)
303 if (parent_cat->obj_list[i] == obj)
305 parent_cat->obj_list.erase(parent_cat->obj_list.iterAt(i));
307 /* If this category now has no items, mark root as changed
308 * so clients won't get the category drawn anymore.
310 if (parent_cat->obj_list.size() == 0)
318 /* Update the category as changed. */
319 parent_cat->reorder = true;
320 parent_cat->serial++;
324 /* Finally, mark the object as free. */
327 /* The callbacks pointer is still valid, so fire away! */
328 obj->callbacks->OnTopMenuObjectRemoved(this, object_id);
331 bool TopMenu::DisplayMenu(int client, unsigned int hold_time, TopMenuPosition position)
333 if (m_clients == NULL)
338 IGamePlayer *pPlayer = playerhelpers->GetGamePlayer(client);
339 if (!pPlayer->IsInGame())
344 UpdateClientRoot(client, pPlayer);
346 topmenu_player_t *pClient = &m_clients[client];
347 if (pClient->root == NULL)
352 bool return_value = false;
354 if (position == TopMenuPosition_LastCategory &&
355 pClient->last_category < m_Categories.size())
357 return_value = DisplayCategory(client, pClient->last_category, hold_time, true);
359 else if (position == TopMenuPosition_LastRoot)
361 pClient->root->DisplayAtItem(client, hold_time, pClient->last_root_pos);
363 else if (position == TopMenuPosition_Start)
365 pClient->last_position = 0;
366 pClient->last_category = 0;
367 return_value = pClient->root->Display(client, hold_time);
373 bool TopMenu::DisplayCategory(int client, unsigned int category, unsigned int hold_time, bool last_position)
375 UpdateClientCategory(client, category);
377 topmenu_player_t *pClient = &m_clients[client];
378 if (category >= pClient->cat_count
379 || pClient->cats[category].menu == NULL)
384 bool return_value = false;
386 topmenu_player_category_t *player_cat = &(pClient->cats[category]);
388 pClient->last_category = category;
391 return_value = player_cat->menu->DisplayAtItem(client, hold_time, pClient->last_position);
395 return_value = player_cat->menu->Display(client, hold_time);
401 void TopMenu::OnMenuSelect2(IBaseMenu *menu, int client, unsigned int item, unsigned int item_on_page)
403 const char *item_name = menu->GetItemInfo(item, NULL);
409 topmenu_object_t *obj;
410 topmenu_player_t *pClient = &m_clients[client];
411 topmenu_object_t **pObject = m_ObjLookup.retrieve(item_name);
419 /* We now have the object... what do we do with it? */
420 if (obj->type == TopMenuObject_Category)
422 /* If it's a category, the user wants to view it.. */
423 for (size_t i = 0; i < m_Categories.size(); i++)
425 if (m_Categories[i]->obj == obj)
427 pClient->last_root_pos = item_on_page;
428 DisplayCategory(client, (unsigned int)i, MENU_TIME_FOREVER, false);
435 pClient->last_position = item_on_page;
437 /* Re-check access in case this user had their credentials revoked */
438 if (obj->cmdname[0] != '\0' && !adminsys->CheckAccess(client, obj->cmdname, obj->flags, false))
440 DisplayMenu(client, 0, TopMenuPosition_LastCategory);
444 /* Pass the information on to the callback */
445 obj->callbacks->OnTopMenuSelectOption(this, client, obj->object_id);
449 void TopMenu::OnMenuDrawItem(IBaseMenu *menu, int client, unsigned int item, unsigned int &style)
451 const char *item_name = menu->GetItemInfo(item, NULL);
457 topmenu_object_t *obj;
458 topmenu_object_t **pObject = m_ObjLookup.retrieve(item_name);
466 if (obj->cmdname[0] == '\0')
471 if (!adminsys->CheckAccess(client, obj->cmdname, obj->flags, false))
473 style = ITEMDRAW_IGNORE;
477 unsigned int TopMenu::OnMenuDisplayItem(IBaseMenu *menu,
481 const ItemDrawInfo &dr)
483 const char *item_name = menu->GetItemInfo(item, NULL);
489 topmenu_object_t *obj;
490 topmenu_object_t **pObject = m_ObjLookup.retrieve(item_name);
498 /* Ask the object to render the text for this client */
500 obj->callbacks->OnTopMenuDrawOption(this, client, obj->object_id, renderbuf, sizeof(renderbuf));
502 /* Build the new draw info */
503 ItemDrawInfo new_dr = dr;
504 new_dr.display = renderbuf;
506 /* Man I love the menu API. Ask the panel to draw the item and give the position
507 * back to Core's renderer. This way we don't have to worry about keeping the
508 * render buffer static!
510 return panel->DrawItem(new_dr);
513 void TopMenu::OnMenuCancel(IBaseMenu *menu, int client, MenuCancelReason reason)
515 if (reason == MenuCancel_ExitBack)
517 /* If the client chose exit back, they were on a category menu, and we can
518 * now display the root menu from the last known position.
520 DisplayMenu(client, 0, TopMenuPosition_LastRoot);
524 void TopMenu::UpdateClientRoot(int client, IGamePlayer *pGamePlayer)
526 topmenu_player_t *pClient = &m_clients[client];
527 IGamePlayer *pPlayer = pGamePlayer ? pGamePlayer : playerhelpers->GetGamePlayer(client);
529 /* Determine if an update is necessary */
530 bool is_update_needed = false;
531 if (pClient->menu_serial != m_SerialNo)
533 is_update_needed = true;
535 else if (pPlayer->GetUserId() != pClient->user_id)
537 is_update_needed = true;
540 /* If no update is needed at the root level, just leave now */
541 if (!is_update_needed)
546 /* First we need to flush the cache... */
547 TearDownClient(pClient);
549 /* Now, rebuild the category list, but don't create menus */
550 if (m_Categories.size() == 0)
552 pClient->cat_count = 0;
553 pClient->cats = NULL;
557 pClient->cat_count = (unsigned int)m_Categories.size();
558 pClient->cats = new topmenu_player_category_t[pClient->cat_count];
559 memset(pClient->cats, 0, sizeof(topmenu_player_category_t) * pClient->cat_count);
562 /* Re-sort the root categories if needed */
563 SortCategoriesIfNeeded();
565 /* Build the root menu */
566 IBaseMenu *root_menu = menus->GetDefaultStyle()->CreateMenu(this, myself->GetIdentity());
568 /* Add the sorted items */
569 for (size_t i = 0; i < m_SortedCats.size(); i++)
571 if (m_Categories[m_SortedCats[i]]->obj_list.size() == 0)
575 root_menu->AppendItem(m_Categories[m_SortedCats[i]]->obj->name, ItemDrawInfo(""));
578 /* Now we need to handle un-sorted items. This is slightly trickier, as we need to
579 * pre-render each category name, and cache those names. Phew!
581 if (m_UnsortedCats.size())
583 obj_by_name_t *item_list = new obj_by_name_t[m_UnsortedCats.size()];
584 for (size_t i = 0; i < m_UnsortedCats.size(); i++)
586 obj_by_name_t *temp_obj = &item_list[i];
587 topmenu_object_t *obj = m_Categories[m_UnsortedCats[i]]->obj;
588 obj->callbacks->OnTopMenuDrawOption(this,
592 sizeof(temp_obj->name));
593 temp_obj->obj_index = m_UnsortedCats[i];
596 /* Sort our temp list */
597 qsort(item_list, m_UnsortedCats.size(), sizeof(obj_by_name_t), _SortObjectNamesDescending);
599 /* Add the new sorted categories */
600 for (size_t i = 0; i < m_SortedCats.size(); i++)
602 if (m_Categories[item_list[i].obj_index]->obj_list.size() == 0)
606 root_menu->AppendItem(m_Categories[item_list[i].obj_index]->obj->name, ItemDrawInfo(""));
612 /* Set the menu's title */
614 m_pTitle->OnTopMenuDrawTitle(this, client, 0, renderbuf, sizeof(renderbuf));
615 root_menu->SetDefaultTitle(renderbuf);
617 /* The client is now fully updated */
618 pClient->root = root_menu;
619 pClient->user_id = pPlayer->GetUserId();
620 pClient->menu_serial = m_SerialNo;
621 pClient->last_position = 0;
622 pClient->last_category = 0;
623 pClient->last_root_pos = 0;
626 void TopMenu::UpdateClientCategory(int client, unsigned int category)
628 /* Update the client's root menu just in case it needs it. This
629 * will validate that we have both a valid client and a valid
630 * category structure for that client.
632 UpdateClientRoot(client);
634 /* Now it's guaranteed that our category tables will be usable */
635 topmenu_player_t *pClient = &m_clients[client];
636 topmenu_category_t *cat = m_Categories[category];
637 topmenu_player_category_t *player_cat = &(pClient->cats[category]);
639 /* Does the category actually need updating? */
640 if (player_cat->serial == cat->serial)
645 /* Destroy any existing menu */
646 if (player_cat->menu)
648 player_cat->menu->Destroy();
649 player_cat->menu = NULL;
652 if (pClient->last_category == category)
654 pClient->last_position = 0;
657 IBaseMenu *cat_menu = menus->GetDefaultStyle()->CreateMenu(this, myself->GetIdentity());
659 /* Categories get an "exit back" button */
660 cat_menu->SetMenuOptionFlags(cat_menu->GetMenuOptionFlags() | MENUFLAG_BUTTON_EXITBACK);
662 /* Re-sort the category if needed */
663 SortCategoryIfNeeded(category);
665 /* Build the menu with the sorted items first */
666 for (size_t i = 0; i < cat->sorted.size(); i++)
668 cat_menu->AppendItem(cat->sorted[i]->name, ItemDrawInfo(""));
671 /* Now handle unsorted items */
672 if (cat->unsorted.size())
674 /* Build a list of the item names */
675 obj_by_name_t *item_list = new obj_by_name_t[cat->unsorted.size()];
676 for (size_t i = 0; i < cat->unsorted.size(); i++)
678 obj_by_name_t *item = &item_list[i];
679 topmenu_object_t *obj = cat->unsorted[i];
680 obj->callbacks->OnTopMenuDrawOption(this,
685 item->obj_index = (unsigned int)i;
689 qsort(item_list, cat->unsorted.size(), sizeof(obj_by_name_t), _SortObjectNamesDescending);
691 /* Add to the menu */
692 for (size_t i = 0; i < cat->unsorted.size(); i++)
694 cat_menu->AppendItem(cat->unsorted[item_list[i].obj_index]->name, ItemDrawInfo(""));
700 /* Set the menu's title */
702 cat->obj->callbacks->OnTopMenuDrawTitle(this,
707 cat_menu->SetDefaultTitle(renderbuf);
710 player_cat->menu = cat_menu;
711 player_cat->serial = cat->serial;
714 void TopMenu::SortCategoryIfNeeded(unsigned int category)
716 topmenu_category_t *cat = m_Categories[category];
723 cat->unsorted.clear();
725 if (cat->obj_list.size() == 0)
727 cat->reorder = false;
731 CVector<unsigned int> to_sort;
732 for (size_t i = 0; i < cat->obj_list.size(); i++)
734 to_sort.push_back(i);
737 /* Find a matching category in the configs */
738 config_category_t *config_cat = NULL;
739 for (size_t i = 0; i < m_Config.cats.size(); i++)
741 if (strcmp(m_Config.strings.GetString(m_Config.cats[i]->name), cat->obj->name) == 0)
743 config_cat = m_Config.cats[i];
748 /* If there is a matching category, build a pre-sorted item list */
749 if (config_cat != NULL)
751 /* Find matching objects in this category */
752 for (size_t i = 0; i < config_cat->commands.size(); i++)
754 const char *config_name = m_Config.strings.GetString(config_cat->commands[i]);
755 for (size_t j = 0; j < to_sort.size(); j++)
757 if (strcmp(config_name, cat->obj_list[to_sort[j]]->name) == 0)
759 /* Place in the final list, then remove from the temporary list */
760 cat->sorted.push_back(cat->obj_list[to_sort[j]]);
761 to_sort.erase(to_sort.iterAt(j));
768 /* Push any remaining items onto the unsorted list */
769 for (size_t i = 0; i < to_sort.size(); i++)
771 cat->unsorted.push_back(cat->obj_list[to_sort[i]]);
774 cat->reorder = false;
777 void TopMenu::SortCategoriesIfNeeded()
779 if (!m_bCatsNeedResort)
784 /* Clear sort results */
785 m_SortedCats.clear();
786 m_UnsortedCats.clear();
788 if (m_Categories.size() == 0)
790 m_bCatsNeedResort = false;
794 CVector<unsigned int> to_sort;
795 for (unsigned int i = 0; i < (unsigned int)m_Categories.size(); i++)
797 to_sort.push_back(i);
800 /* If we have any predefined categories, add them in as they appear. */
801 for (size_t i= 0; i < m_Config.cats.size(); i++)
803 /* Find this category and map it in if we can */
804 for (size_t j = 0; j < to_sort.size(); j++)
806 if (strcmp(m_Config.strings.GetString(m_Config.cats[i]->name),
807 m_Categories[to_sort[j]]->obj->name) == 0)
809 /* Add to the real list and remove from the temporary */
810 m_SortedCats.push_back(to_sort[j]);
811 to_sort.erase(to_sort.iterAt(j));
817 /* Push any remaining items onto the unsorted list */
818 for (size_t i = 0; i < to_sort.size(); i++)
820 m_UnsortedCats.push_back(to_sort[i]);
823 m_bCatsNeedResort = false;
826 void TopMenu::CreatePlayers(int max_clients)
828 m_max_clients = max_clients;
829 m_clients = (topmenu_player_t *)malloc(sizeof(topmenu_player_t) * (max_clients + 1));
830 memset(m_clients, 0, sizeof(topmenu_player_t) * (max_clients + 1));
833 void TopMenu::TearDownClient(topmenu_player_t *player)
835 if (player->cats != NULL)
837 for (unsigned int i = 0; i < player->cat_count; i++)
839 topmenu_player_category_t *player_cat = &(player->cats[i]);
840 if (player_cat->menu != NULL)
842 player_cat->menu->Destroy();
845 delete [] player->cats;
848 if (player->root != NULL)
850 player->root->Destroy();
853 memset(player, 0, sizeof(topmenu_player_t));
856 bool TopMenu::LoadConfiguration(const char *file, char *error, size_t maxlength)
859 unsigned int line = 0, col = 0;
861 if ((err = textparsers->ParseFile_SMC(file, this, &line, &col))
864 const char *err_string = textparsers->GetSMCErrorString(err);
867 err_string = "Unknown";
870 UTIL_Format(error, maxlength, "%s", err_string);
878 bool TopMenu::OnIdentityRemoval(IdentityToken_t *owner)
880 /* First sweep the categories owned by us */
881 CVector<unsigned int> obj_list;
882 for (size_t i = 0; i < m_Categories.size(); i++)
884 if (m_Categories[i]->obj->owner == owner)
886 obj_list.push_back(m_Categories[i]->obj->object_id);
890 for (size_t i = 0; i < obj_list.size(); i++)
892 RemoveFromMenu(obj_list[i]);
895 /* Now we can look for actual items */
896 for (size_t i = 0; i < m_Objects.size(); i++)
898 if (m_Objects[i]->is_free)
902 if (m_Objects[i]->owner == owner)
904 assert(m_Objects[i]->type != TopMenuObject_Category);
905 RemoveFromMenu(m_Objects[i]->object_id);
912 #define PARSE_STATE_NONE 0
913 #define PARSE_STATE_CATEGORY 1
914 unsigned int ignore_parse_level = 0;
915 unsigned int current_parse_state = 0;
916 config_category_t *cur_cat = NULL;
918 void TopMenu::ReadSMC_ParseStart()
920 current_parse_state = PARSE_STATE_NONE;
921 ignore_parse_level = 0;
924 /* Reset the old config */
925 m_Config.strings.Reset();
926 for (size_t i = 0; i < m_Config.cats.size(); i++)
928 delete m_Config.cats[i];
930 m_Config.cats.clear();
933 SMCParseResult TopMenu::ReadSMC_NewSection(const char *name, bool opt_quotes)
935 if (ignore_parse_level)
937 ignore_parse_level++;
941 if (current_parse_state == PARSE_STATE_NONE)
943 cur_cat = new config_category_t;
944 cur_cat->name = m_Config.strings.AddString(name);
945 m_Config.cats.push_back(cur_cat);
946 current_parse_state = PARSE_STATE_CATEGORY;
950 ignore_parse_level = 1;
954 return SMCParse_Continue;
957 SMCParseResult TopMenu::ReadSMC_KeyValue(const char *key, const char *value, bool key_quotes, bool value_quotes)
959 if (ignore_parse_level > 0
960 || current_parse_state != PARSE_STATE_CATEGORY
963 return SMCParse_Continue;
966 if (strcmp(key, "item") == 0)
968 cur_cat->commands.push_back(m_Config.strings.AddString(value));
971 return SMCParse_Continue;
974 SMCParseResult TopMenu::ReadSMC_LeavingSection()
976 if (ignore_parse_level)
978 ignore_parse_level--;
982 if (current_parse_state == PARSE_STATE_CATEGORY)
985 current_parse_state = PARSE_STATE_NONE;
989 return SMCParse_Continue;
992 unsigned int TopMenu::FindCategory(const char *name)
994 topmenu_object_t **p_obj = m_ObjLookup.retrieve(name);
1000 topmenu_object_t *obj = *p_obj;
1001 if (obj->type != TopMenuObject_Category)
1006 return obj->object_id;
1009 int _SortObjectNamesDescending(const void *ptr1, const void *ptr2)
1011 obj_by_name_t *obj1 = (obj_by_name_t *)ptr1;
1012 obj_by_name_t *obj2 = (obj_by_name_t *)ptr2;
1013 return strcmp(obj1->name, obj2->name);
1016 unsigned int strncopy(char *dest, const char *src, size_t count)
1024 while ((*src) && (--count))
1030 return (dest - start);
1033 size_t UTIL_Format(char *buffer, size_t maxlength, const char *fmt, ...)
1037 size_t len = vsnprintf(buffer, maxlength, fmt, ap);
1040 if (len >= maxlength)
1042 buffer[maxlength - 1] = '\0';
1043 return (maxlength - 1);