Move int_array helpfer functions to utils/common.c

These can be useful outside scan.c, so make them available.

Signed-hostap: Jouni Malinen <j@w1.fi>
This commit is contained in:
Jouni Malinen 2013-12-25 20:02:57 +02:00
parent 715bf904ff
commit 98eda9c26d
3 changed files with 97 additions and 68 deletions

View file

@ -735,3 +735,95 @@ char * freq_range_list_str(const struct wpa_freq_range_list *list)
return buf; return buf;
} }
int int_array_len(const int *a)
{
int i;
for (i = 0; a && a[i]; i++)
;
return i;
}
void int_array_concat(int **res, const int *a)
{
int reslen, alen, i;
int *n;
reslen = int_array_len(*res);
alen = int_array_len(a);
n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
if (n == NULL) {
os_free(*res);
*res = NULL;
return;
}
for (i = 0; i <= alen; i++)
n[reslen + i] = a[i];
*res = n;
}
static int freq_cmp(const void *a, const void *b)
{
int _a = *(int *) a;
int _b = *(int *) b;
if (_a == 0)
return 1;
if (_b == 0)
return -1;
return _a - _b;
}
void int_array_sort_unique(int *a)
{
int alen;
int i, j;
if (a == NULL)
return;
alen = int_array_len(a);
qsort(a, alen, sizeof(int), freq_cmp);
i = 0;
j = 1;
while (a[i] && a[j]) {
if (a[i] == a[j]) {
j++;
continue;
}
a[++i] = a[j++];
}
if (a[i])
i++;
a[i] = 0;
}
void int_array_add_unique(int **res, int a)
{
int reslen;
int *n;
for (reslen = 0; *res && (*res)[reslen]; reslen++) {
if ((*res)[reslen] == a)
return; /* already in the list */
}
n = os_realloc_array(*res, reslen + 2, sizeof(int));
if (n == NULL) {
os_free(*res);
*res = NULL;
return;
}
n[reslen] = a;
n[reslen + 1] = 0;
*res = n;
}

View file

@ -519,6 +519,11 @@ int freq_range_list_includes(const struct wpa_freq_range_list *list,
unsigned int freq); unsigned int freq);
char * freq_range_list_str(const struct wpa_freq_range_list *list); char * freq_range_list_str(const struct wpa_freq_range_list *list);
int int_array_len(const int *a);
void int_array_concat(int **res, const int *a);
void int_array_sort_unique(int *a);
void int_array_add_unique(int **res, int a);
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))

View file

@ -142,74 +142,6 @@ static void wpa_supplicant_assoc_try(struct wpa_supplicant *wpa_s,
} }
static int int_array_len(const int *a)
{
int i;
for (i = 0; a && a[i]; i++)
;
return i;
}
static void int_array_concat(int **res, const int *a)
{
int reslen, alen, i;
int *n;
reslen = int_array_len(*res);
alen = int_array_len(a);
n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
if (n == NULL) {
os_free(*res);
*res = NULL;
return;
}
for (i = 0; i <= alen; i++)
n[reslen + i] = a[i];
*res = n;
}
static int freq_cmp(const void *a, const void *b)
{
int _a = *(int *) a;
int _b = *(int *) b;
if (_a == 0)
return 1;
if (_b == 0)
return -1;
return _a - _b;
}
static void int_array_sort_unique(int *a)
{
int alen;
int i, j;
if (a == NULL)
return;
alen = int_array_len(a);
qsort(a, alen, sizeof(int), freq_cmp);
i = 0;
j = 1;
while (a[i] && a[j]) {
if (a[i] == a[j]) {
j++;
continue;
}
a[++i] = a[j++];
}
if (a[i])
i++;
a[i] = 0;
}
/** /**
* wpa_supplicant_trigger_scan - Request driver to start a scan * wpa_supplicant_trigger_scan - Request driver to start a scan
* @wpa_s: Pointer to wpa_supplicant data * @wpa_s: Pointer to wpa_supplicant data