foxygit / doom Log in
commit ea6d1a5fc8f0d2745e048bf04ffdcd226dffb8d7
Author:     Jonathan Dowland <jon@dow.land>
AuthorDate: Thu Feb 28 22:04:41 2019 +0000
Commit:     Jonathan Dowland <jon@dow.land>
CommitDate: Thu Feb 28 22:04:41 2019 +0000

    Address review comments (Thanks Fraggle!)

     • use arrlen for calculating static array lengths
     • use M_StringJoin instead of string.h stuff
     • rename files and functions to match convention
     • rand()/srand() are ANSI C, not random()/srandom()
     • en_US spelling
---
 src/CMakeLists.txt               |  2 +-
 src/Makefile.am                  |  6 +++---
 src/net_client.c                 |  4 ++--
 src/{petname.c => net_petname.c} | 32 +++++++++++---------------------
 src/{petname.h => net_petname.h} |  2 +-
 src/setup/multiplayer.c          |  4 ++--
 6 files changed, 20 insertions(+), 30 deletions(-)

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 04d68c48..3aa22b1b 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -75,11 +75,11 @@ set(GAME_SOURCE_FILES
     net_io.c            net_io.h
     net_loop.c          net_loop.h
     net_packet.c        net_packet.h
+    net_petname.c       net_petname.h
     net_query.c         net_query.h
     net_sdl.c           net_sdl.h
     net_server.c        net_server.h
     net_structrw.c      net_structrw.h
-    petname.c           petname.h
     sha1.c              sha1.h
     memio.c             memio.h
     tables.c            tables.h
diff --git a/src/Makefile.am b/src/Makefile.am
index 5eec30f9..05cf66af 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -41,11 +41,11 @@ net_common.c         net_common.h          \
 net_dedicated.c      net_dedicated.h       \
 net_io.c             net_io.h              \
 net_packet.c         net_packet.h          \
+net_petname.c        net_petname.h         \
 net_sdl.c            net_sdl.h             \
 net_query.c          net_query.h           \
 net_server.c         net_server.h          \
 net_structrw.c       net_structrw.h        \
-petname.c            petname.h             \
 z_native.c           z_zone.h

 @PROGRAM_PREFIX@server_SOURCES=$(COMMON_SOURCE_FILES) $(DEDSERV_FILES)
@@ -95,11 +95,11 @@ net_gui.c            net_gui.h             \
 net_io.c             net_io.h              \
 net_loop.c           net_loop.h            \
 net_packet.c         net_packet.h          \
+net_petname.c        net_petname.h         \
 net_query.c          net_query.h           \
 net_sdl.c            net_sdl.h             \
 net_server.c         net_server.h          \
 net_structrw.c       net_structrw.h        \
-petname.c            petname.h             \
 sha1.c               sha1.h                \
 memio.c              memio.h               \
 tables.c             tables.h              \
@@ -197,10 +197,10 @@ m_config.c           m_config.h            \
 m_controls.c         m_controls.h          \
 net_io.c             net_io.h              \
 net_packet.c         net_packet.h          \
+net_petname.c        net_petname.h         \
 net_sdl.c            net_sdl.h             \
 net_query.c          net_query.h           \
 net_structrw.c       net_structrw.h        \
-petname.c            petname.h             \
 z_native.c           z_zone.h

 if HAVE_WINDRES
diff --git a/src/net_client.c b/src/net_client.c
index eb6f9113..45624b43 100644
--- a/src/net_client.c
+++ b/src/net_client.c
@@ -38,7 +38,7 @@
 #include "net_query.h"
 #include "net_server.h"
 #include "net_structrw.h"
-#include "petname.h"
+#include "net_petname.h"
 #include "w_checksum.h"
 #include "w_wad.h"

@@ -1214,7 +1214,7 @@ void NET_CL_Init(void)

     if (net_player_name == NULL)
     {
-        net_player_name = getRandomPetName();
+        net_player_name = NET_GetRandomPetName();
     }

     if (net_player_name == NULL)
diff --git a/src/petname.c b/src/net_petname.c
similarity index 76%
rename from src/petname.c
rename to src/net_petname.c
index a7f9b307..7c9d6e8b 100644
--- a/src/petname.c
+++ b/src/net_petname.c
@@ -18,6 +18,8 @@
 #include <stdlib.h>
 #include <time.h>
 #include <string.h>
+#include "doomtype.h"
+#include "m_misc.h"

 static char *adjectives[] = {
     "Grumpy",
@@ -64,8 +66,6 @@ static char *adjectives[] = {
     "Posh",
     "Baby",
 };
-#define NUM_ADJECTIVES (sizeof adjectives / sizeof (char *))
-

 static char *nouns[] = {
     // Doom
@@ -97,34 +97,24 @@ static char *nouns[] = {
     "Reaver",
     "Crusader",
 };
-#define NUM_NOUNS (sizeof nouns / sizeof (char *))

 /*
  * ideally we would export this and the caller would invoke it during
  * their setup routine. But, the two callers only invoke getRandomPetName
- * once, so the initialisation might as well occur then.
+ * once, so the initialization might as well occur then.
  */
-static void initPetName()
+static void InitPetName()
 {
-    srandom((unsigned int)time(NULL));
+    srand((unsigned int)time(NULL));
 }

-char *getRandomPetName()
+char *NET_GetRandomPetName()
 {
-    char *a, *n, *r;
-
-    initPetName();
+    char *a, *n;

-    a = adjectives[random() % NUM_ADJECTIVES];
-    n = nouns[random() % NUM_NOUNS];
-    r = (char *)malloc(strlen(a) + (sizeof ' ') +
-                       strlen(n) + (sizeof '\0'));
-    if(r)
-    {
-        strcpy(r,a);
-        r[strlen(a)] = ' ';
-        strcpy(r + strlen(a) + 1, n);
-    }
+    InitPetName();
+    a = adjectives[rand() % arrlen(adjectives)];
+    n = nouns[rand() % arrlen(nouns)];

-    return r;
+    return M_StringJoin(a, " ", n, NULL);
 }
diff --git a/src/petname.h b/src/net_petname.h
similarity index 95%
rename from src/petname.h
rename to src/net_petname.h
index 2f4c90b4..29225074 100644
--- a/src/petname.h
+++ b/src/net_petname.h
@@ -15,4 +15,4 @@
 //     Generate a randomized, private, memorable name for a Player
 //

-char *getRandomPetName();
+char *NET_GetRandomPetName();
diff --git a/src/setup/multiplayer.c b/src/setup/multiplayer.c
index 0e50125b..823fda74 100644
--- a/src/setup/multiplayer.c
+++ b/src/setup/multiplayer.c
@@ -33,7 +33,7 @@
 #include "net_io.h"
 #include "net_query.h"

-#include "petname.h"
+#include "net_petname.h"

 #define MULTI_START_HELP_URL "https://www.chocolate-doom.org/setup-multi-start"
 #define MULTI_JOIN_HELP_URL "https://www.chocolate-doom.org/setup-multi-join"
@@ -1098,7 +1098,7 @@ void SetPlayerNameDefault(void)
 {
     if (net_player_name == NULL)
     {
-        net_player_name = getRandomPetName();
+        net_player_name = NET_GetRandomPetName();
     }

     if (net_player_name == NULL)