1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
// Local function
void free_proID(struct prof_Name_ID *proID);
// Local const int
const int limit = 50;
// Local struct
struct prof_Name_ID
{
char *_class[limit];
char *name[limit];
char *bonus[limit];
};
// Local #define
#define CREATE(result, type, number) \
do \
{ \
if ((number) * sizeof(type) <= 0) \
LOG(SYSTEMLOG, "[CREATE-ERROR]", "SYSERR: Zero bytes or less requested at %s:%d.", __FILE__, __LINE__); \
if (!((result) = (type *) calloc ((number), sizeof(type)))) \
{ \
LOG(SYSTEMLOG, "[SYSERR: malloc failure]", "SYSERR: malloc failure"); \
perror("SYSERR: malloc failure"); \
abort(); \
} \
} while(0)
void testFunction()
{
MySQL my;
char szQueryText[500];
MYSQL_RES *My_result = NULL;
struct prof_Name_ID *proID = NULL;
CREATE(proID, struct prof_Name_ID, 1);
// Initialize and null out local struct prof_Name_ID
for (int a = 0; a < limit; a++)
{
proID->_class[a] = _strdup("");
proID->name[a] = _strdup("");
proID->bonus[a] = _strdup("");
}
// Pull data in from MySQL
snprintf(szQueryText, sizeof(szQueryText), "SELECT * FROM someTable WHERE active = 1 ORDER BY rowID ASC;");
My_result = MySQL__query(szQueryText);
int num = my.Query(szQueryText);
if (num > limit)
return;
while (num > 0)
{
my.NextRow();
num--;
if (proID->_class[atoi(my.GetData("professionID"))])
free(proID->_class[atoi(my.GetData("professionID"))]);
proID->_class[atoi(my.GetData("professionID"))] = NULL;
if (proID->name[atoi(my.GetData("professionID"))])
free(proID->name[atoi(my.GetData("professionID"))]);
proID->name[atoi(my.GetData("professionID"))] = NULL;
if (proID->bonus[atoi(my.GetData("professionID"))])
free(proID->bonus[atoi(my.GetData("professionID"))]);
proID->bonus[atoi(my.GetData("professionID"))] = NULL;
proID->_class[atoi(my.GetData("professionID"))] = _strdup(my.GetData("professionClass"));
proID->name[atoi(my.GetData("professionID"))] = _strdup(my.GetData("professionName"));
proID->bonus[atoi(my.GetData("professionID"))] = _strdup(my.GetData("professionBonus"));
}
// Okay, now free() the data we _strdup'd
free_proID(proID);
}
void free_proID(struct prof_Name_ID *proID)
{
for (int a = 0; a < limit; a++)
{
if (proID->_class[a])
free(proID->_class[a]);
if (proID->name[a])
free(proID->name[a]);
if (proID->bonus[a])
free(proID->bonus[a]);
proID->_class[a] = NULL;
proID->name[a] = NULL;
proID->bonus[a] = NULL;
}
if (proID)
{
free(proID);
proID = NULL;
}
}
|