Add aaa-12 新增计算数组下标、新增用户组、按用户名查询用户组
RCA: SOL: 修改人:zhouzian 检视人:zhouzian
This commit is contained in:
parent
0a9a54d5ed
commit
8039059be2
|
@ -0,0 +1,8 @@
|
|||
fndef COMMOM_USER_H_
|
||||
#define COMMON_USER_H_
|
||||
|
||||
#define SPECHAR(element) (strpbrk((element), "~!@#$%^&*()_+{}|:\"<>?\\,./;\'[]-=`")) //校验特殊字符
|
||||
#define GETGROUPID(ID, NAME1, NAME2) ((((ID) != 0) && (strcmp((NAME1), (NAME2)) == 0)) ? (ID) : 0) //根据用户名查询用户组ID
|
||||
#define CHECKOUTARG(element) (((element) == NULL || (element) == "" || SPECHAR(element)) ? true : false) //校验参数
|
||||
|
||||
#endif
|
|
@ -0,0 +1,136 @@
|
|||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<stdbool.h>
|
||||
#include"array_index.h"
|
||||
|
||||
ARRAY g_user_index_head = { 0 };
|
||||
ARRAY g_group_index_head = { 0 };
|
||||
|
||||
#define INDEX_LEN_16
|
||||
|
||||
#ifdef INDEX_LEN_16
|
||||
#define INDEX_LEN 16
|
||||
#define INDEX_LOW_MASK 0x0000ffff
|
||||
#define INDEX_HI_MASK 0xffff0000
|
||||
#else
|
||||
#endif
|
||||
|
||||
|
||||
#define CUR_INDEX (head->cur)
|
||||
#define MAX_INDEX (head->size)
|
||||
#define INVALID_INDEX (0)
|
||||
#define HI_ELEMENT_GET(element) ((element) >> INDEX_LEN)
|
||||
#define HI_ELEMENT_SET(element, value) (((element) & INDEX_LOW_MASK) | ((value) << INDEX_LEN))
|
||||
#define LOW_ELEMENT_GET(element) ((element) & INDEX_LOW_MASK)
|
||||
#define LOW_ELEMENT_SET(element, value) (((element) & INDEX_HI_MASK) | ((value) & INDEX_LOW_MASK))
|
||||
|
||||
/*
|
||||
*初始化索引
|
||||
*失败:1
|
||||
*成功:0
|
||||
*/
|
||||
int init_array(ARRAY* head, int index_size)
|
||||
{
|
||||
int i;
|
||||
|
||||
head->array = malloc(sizeof(head->array) * index_size);
|
||||
if (NULL == head->array)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
//head->cur = 1;
|
||||
//head->size = index_size;
|
||||
CUR_INDEX = 1;
|
||||
MAX_INDEX = index_size;
|
||||
|
||||
/*初始化第一个值*/
|
||||
head->array[0] = HI_ELEMENT_SET(head->array[0], 0);
|
||||
head->array[0] = LOW_ELEMENT_SET(head->array[0], 1);
|
||||
|
||||
for (i = 1; i < index_size - 1; i++)
|
||||
{
|
||||
head->array[i] = HI_ELEMENT_SET(head->array[i], i - 1);
|
||||
head->array[i] = LOW_ELEMENT_SET(head->array[i], i + 1);
|
||||
}
|
||||
|
||||
/*初始化最后一个值*/
|
||||
head->array[i] = HI_ELEMENT_SET(head->array[i], i - 1);
|
||||
head->array[i] = LOW_ELEMENT_SET(head->array[i], 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 顺序分配index */
|
||||
unsigned short alloc_index(ARRAY* head)
|
||||
{
|
||||
unsigned short index = CUR_INDEX; //获取cur
|
||||
|
||||
/*判断index无效,返回0*/
|
||||
if ((0 == index) || ((MAX_INDEX - 1) <= index))
|
||||
{
|
||||
return INVALID_INDEX;
|
||||
}
|
||||
|
||||
CUR_INDEX = LOW_ELEMENT_GET(head->array[index]); //移动cur
|
||||
head->array[index] = 0; //被使用过的元素置0
|
||||
return index; //返回被使用的下标作为ID
|
||||
|
||||
//if ((0 < index) || ((MAX_INDEX-1) > index))
|
||||
|
||||
}
|
||||
|
||||
/* 指定分配index
|
||||
*成功返回:index
|
||||
*失败返回:0
|
||||
*/
|
||||
unsigned short alloc_index_special(ARRAY* head, unsigned short index)
|
||||
{
|
||||
unsigned short hi_index, low_index;
|
||||
|
||||
/*判断index无效,返回0*/
|
||||
if ((0 == index) || ((MAX_INDEX - 1) <= index))
|
||||
{
|
||||
return INVALID_INDEX;
|
||||
}
|
||||
|
||||
/* 判断已经被使用过了,返回0 */
|
||||
if (INVALID_INDEX == head->array[index])
|
||||
{
|
||||
return INVALID_INDEX;
|
||||
}
|
||||
|
||||
/* 判断和顺序插入相同 */
|
||||
if (CUR_INDEX == index)
|
||||
{
|
||||
return alloc_index(head);
|
||||
}
|
||||
|
||||
low_index = LOW_ELEMENT_GET(head->array[index]);
|
||||
hi_index = HI_ELEMENT_GET(head->array[index]);
|
||||
|
||||
head->array[low_index] = HI_ELEMENT_SET(head->array[low_index], hi_index);
|
||||
head->array[hi_index] = LOW_ELEMENT_SET(head->array[hi_index], low_index);
|
||||
head->array[index] = 0;
|
||||
return index;
|
||||
}
|
||||
|
||||
void free_index(ARRAY* head, unsigned short index)
|
||||
{
|
||||
unsigned short cur_index = CUR_INDEX;
|
||||
|
||||
if ((0 == index) || ((MAX_INDEX - 1) <= index))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (INVALID_INDEX != head->array[index])
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CUR_INDEX = index;
|
||||
head->array[index] = LOW_ELEMENT_SET(head->array[index], cur_index);
|
||||
head->array[cur_index] = HI_ELEMENT_SET(head->array[cur_index], index);
|
||||
return;
|
||||
}
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
#ifndef ARRAY_INDEX_H_
|
||||
#define ARRAY_INDEX_H_
|
||||
|
||||
#define USER_INDEX_MAX (100 + 2)
|
||||
#define GROUP_INDEX_MAX (20 + 2)
|
||||
|
||||
typedef struct array
|
||||
{
|
||||
unsigned int* array; //数组
|
||||
int cur; //当前可用
|
||||
int size; //数组大小
|
||||
}ARRAY;
|
||||
|
||||
/*初始化数组*/
|
||||
int init_array(ARRAY* head, int index_size);
|
||||
|
||||
/*顺序添加*/
|
||||
unsigned short alloc_index(ARRAY* head);
|
||||
|
||||
/*指定添加*/
|
||||
unsigned short alloc_index_special(ARRAY* head, unsigned short index);
|
||||
|
||||
/*指定释放*/
|
||||
void free_index(ARRAY* head, unsigned short index);
|
||||
|
||||
ARRAY g_user_index_head;
|
||||
ARRAY g_group_index_head;
|
||||
|
||||
#endif
|
|
@ -0,0 +1,173 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include "array_index.h"
|
||||
#include "user_group.h"
|
||||
#include "../../commom/common_user.h"
|
||||
|
||||
#define ADD_SUCCESS 0
|
||||
#define ADD_FAIL_FULL 1
|
||||
#define ADD_FAIL_LENGTH 2
|
||||
#define ADD_FAIL_SPECHARS 3
|
||||
#define ADD_FAIL_DUP 4
|
||||
|
||||
#define DEL_SUCCESS 0
|
||||
#define DEL_FAIL_NOTEXIST 1
|
||||
#define DEL_FAIL_STRTEGY 2
|
||||
|
||||
|
||||
USERGROUP g_group_table[GROUP_INDEX_MAX];
|
||||
|
||||
/* 初始化参数 */
|
||||
int InitUserGroup()
|
||||
{
|
||||
/* 初始化用户组的index */
|
||||
int initResult = init_array(&g_group_index_head, GROUP_INDEX_MAX);
|
||||
if(1 == initResult)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
memset(g_group_table, 0, sizeof(g_group_table));
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 添加元素 */
|
||||
unsigned short AddGroup(USERGROUP* group, char* name, char* description)
|
||||
{
|
||||
/* 校验用户组名和描述的长度 */
|
||||
if((GNAMESIZE-1) < strlen(name) || NULL == name || (GDESIZE - 1) < strlen(description))
|
||||
{
|
||||
return ADD_FAIL_LENGTH;
|
||||
}
|
||||
|
||||
/* 校验特殊字符 */
|
||||
if(SPECHAR(name))
|
||||
{
|
||||
return ADD_FAIL_SPECHARS;
|
||||
}
|
||||
|
||||
/* 校验重名 */
|
||||
for(int i = 0; i < GROUP_INDEX_MAX; i++)
|
||||
{
|
||||
if(strcmp(name, group[i].gname) == 0)
|
||||
{
|
||||
return ADD_FAIL_DUP;
|
||||
}
|
||||
}
|
||||
|
||||
/* 生成用户组ID */
|
||||
unsigned short ID = alloc_index(&g_group_index_head);
|
||||
if (0 == ID)
|
||||
{
|
||||
return ADD_FAIL_FULL;
|
||||
}
|
||||
|
||||
group[ID].ID = ID;
|
||||
strcpy(group[ID].gname, name);
|
||||
strcpy(group[ID].gdescription, description);
|
||||
|
||||
return ADD_SUCCESS;
|
||||
}
|
||||
|
||||
/* 查询用户组-按用户名 */
|
||||
USERGROUP* findGroupByName(USERGROUP* group, char* gname, USERGROUP* groupout)
|
||||
{
|
||||
if (NULL == gname)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if ( NULL == groupout)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
groupout->ID = 0;
|
||||
for (int i = 0; i < GROUP_INDEX_MAX; i++)
|
||||
{
|
||||
if ((strcmp(gname, group[i].gname) == 0) && (group[i].ID != 0))
|
||||
{
|
||||
groupout->ID = group[i].ID;
|
||||
strcpy(groupout->gname, group[i].gname);
|
||||
strcpy(groupout->gdescription, group[i].gdescription);
|
||||
return groupout;
|
||||
}
|
||||
}
|
||||
|
||||
if( 0 == groupout->ID )
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
return groupout;
|
||||
}
|
||||
|
||||
|
||||
/* 获得用户组个数 */
|
||||
unsigned short getGroupCount(USERGROUP* group)
|
||||
{
|
||||
int num = 0;
|
||||
for (int i = 0; i < GROUP_INDEX_MAX; i++)
|
||||
{
|
||||
if(0 != group[i].ID)
|
||||
{
|
||||
num++;
|
||||
}
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
/* 查询用户组列表 */
|
||||
USERGROUP* showUserGroupList(USERGROUP* group, USERGROUP* grouplist)
|
||||
{
|
||||
int j = 0;
|
||||
for(int i = 0; i < GROUP_INDEX_MAX; i++)
|
||||
{
|
||||
if(group[i].ID != 0)
|
||||
{
|
||||
grouplist[j].ID = group[i].ID;
|
||||
strcpy(grouplist[j].gname, group[i].gname);
|
||||
strcpy(grouplist[j].gdescription, group[i].gdescription);
|
||||
j++;
|
||||
}
|
||||
}
|
||||
return grouplist;
|
||||
}
|
||||
|
||||
unsigned short getGroupIDByGName(USERGROUP* group, char* gname)
|
||||
{
|
||||
unsigned short GID_temp = 0;
|
||||
for (int i = 0; i < GROUP_INDEX_MAX && GID_temp == 0; i++)
|
||||
{
|
||||
GID_temp = GETGROUPID(group[i].ID, gname, group[i].gname);
|
||||
}
|
||||
return GID_temp;
|
||||
}
|
||||
|
||||
/*删除元素*/
|
||||
unsigned short DelGroupByName(USERGROUP* group, char* gname)
|
||||
{
|
||||
if ( CHECKOUTARG(gname) )
|
||||
{
|
||||
return DEL_FAIL_NOTEXIST;
|
||||
}
|
||||
|
||||
/* 判断被删除的用户组是否存在 */
|
||||
unsigned short GID_temp = getGroupIDByGName(group, gname);
|
||||
|
||||
if ( 0 == GID_temp)
|
||||
{
|
||||
return DEL_FAIL_NOTEXIST;
|
||||
}
|
||||
|
||||
/* 调用策略提供的接口,查询是否绑定策略*/
|
||||
|
||||
/* 根据用户组ID删除用户列表中对应的用户 */
|
||||
|
||||
/* 根据用户组ID删除用户组列表中对应的用户组 - 释放index、ID置0 */
|
||||
free_index(&g_group_index_head, GID_temp);
|
||||
group[GID_temp].ID = 0;
|
||||
|
||||
/* 强制用户下线 */
|
||||
|
||||
return DEL_SUCCESS;
|
||||
}
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
#ifndef USER_GROUP_H_
|
||||
#define USER_GROUP_H_
|
||||
|
||||
#define GNAMESIZE (127 + 1)
|
||||
#define GDESIZE (127 + 1)
|
||||
|
||||
typedef struct usergroup
|
||||
{
|
||||
int ID;
|
||||
char gname[GNAMESIZE];
|
||||
char gdescription[GDESIZE];
|
||||
}USERGROUP;
|
||||
|
||||
USERGROUP g_group_table[GROUP_INDEX_MAX];
|
||||
|
||||
/* 初始化参数 */
|
||||
int InitUserGroup();
|
||||
|
||||
/* 添加元素-新增用户组 */
|
||||
unsigned short AddGroup(USERGROUP* UG, char* UGNAME, char* UGDES);
|
||||
|
||||
/* 获得用户组个数 */
|
||||
unsigned short getGroupCount(USERGROUP* UG);
|
||||
|
||||
/* 查询用户组列表 */
|
||||
USERGROUP* showUserGroupList(USERGROUP* UG, USERGROUP* UGLIST);
|
||||
|
||||
/* 根据用户组名查询用户组 */
|
||||
USERGROUP* findGroupByName(USERGROUP* UG, char* UGNAME, USERGROUP* UGRES);
|
||||
|
||||
/* 根据用户组名查询用户组ID */
|
||||
unsigned short getGroupIDByGName(USERGROUP* UG, char* UGNAME);
|
||||
|
||||
/* 根据用户组名删除用户组 */
|
||||
unsigned short DelGroupByName(USERGROUP* UG, char* UGNAME);
|
||||
|
||||
#endif
|
||||
|
Loading…
Reference in New Issue