Опубликован: 14.12.2010 | Уровень: для всех | Доступ: платный
Лекция 21: Программы на языке С при использовании статически подключаемой библиотеки
Программные коды подключаемых файлов:
// file stack.h
#ifndef STACK_H__
#define STACK_H__
/// by default a stack reserves space for 16 items
#define STACK_INITIAL_CAPACITY 16
typedef struct stack {
/// number of items in the stack
int m_length;
/// capacity of the stack
int m_capacity;
/// block of memory for the stack
int *m_items;
} stack_t;
/// create a new stack and returns it
stack_t *stack_create (int capacity);
/// destroys the stack and frees resources
void stack_destroy (stack_t *stack);
/// pushes an item into the stack
int stack_push (stack_t *stack, int item);
/// pops the item from the stack
int stack_pop (stack_t *stack);
/// checks whether the stack is empty
int stack_is_empty (stack_t *stack);
#endif// file stack.c
#include <assert.h>
#include <malloc.h>
#include <stddef.h>
#include "stack.h"
static int stack_ensure_capacity (stack_t *stack, int capacity)
{
int capacityDesired;
int *p;
if (stack->m_capacity >= capacity)
return 1;
capacityDesired = stack->m_capacity * 2;
p = realloc (stack->m_items, capacityDesired * sizeof (int));
if (!p)
return 0;
stack->m_items = p;
stack->m_capacity = capacityDesired;
return 1;
}
stack_t* stack_create (int capacity) {
stack_t *result;
if (capacity <= 0)
capacity = STACK_INITIAL_CAPACITY;
result = malloc (sizeof (stack_t));
if (!result)
return NULL;
result->m_items = malloc (capacity * sizeof (int));
if (!result->m_items) {
free (result);
return NULL;
}
result->m_capacity = capacity;
result->m_length = 0;
return result;
}
void stack_destroy (stack_t *stack)
{
assert (stack != NULL);
assert (stack->m_items != NULL);
free (stack->m_items);
free (stack);
}
int stack_push (stack_t *stack, int item)
{
assert (stack != NULL);
assert (stack->m_capacity > 0);
assert (stack->m_items != NULL);
if (!stack_ensure_capacity (stack, stack->m_length + 1))
return 0;
stack->m_items[stack->m_length++] = item;
return 1;
}
int stack_pop (stack_t *stack)
{
assert (!stack_is_empty (stack));
return stack->m_items[--stack->m_length];
}
int stack_is_empty (stack_t *stack)
{
assert (stack != NULL);
return stack->m_length <= 0;
}// file queue.h
#ifndef QUEUE_H__
#define QUEUE_H__
typedef struct queue_item
{
/// pointer to the next item in the queue
struct queue_item *m_next;
/// item data
int m_item;
} queue_item_t;
typedef struct queue
{
/// number of items in the queue
int m_length;
/// first item in the queue
struct queue_item *m_head;
/// last items in the queue
struct queue_item **m_tailnext;
} queue_t;
/// creates a new queue and returns it
queue_t *queue_create ();
/// destroys the queue and frees resources
void queue_destroy (queue_t *queue);
/// pushes an item into the queue adding it to the queue's tail
int queue_push (queue_t *queue, int item);
/// pops the item from the queue, removing it from the queue's head
int queue_pop (queue_t *queue);
/// checks whether the queue is empty
int queue_is_empty (queue_t *queue);
#endif// file queue.c
#include <assert.h>
#include <malloc.h>
#include <stddef.h>
#include "queue.h"
queue_t* queue_create ()
{
queue_t *queue;
queue = malloc (sizeof (queue_t));
if (!queue)
return NULL;
queue->m_head = NULL;
queue->m_tailnext = &(queue->m_head);
queue->m_length = 0;
return queue;
}
void queue_destroy (queue_t *queue)
{
queue_item_t *p;
assert (queue != NULL);
for (p = queue->m_head; p != NULL; p = p->m_next)
free (p);
free (queue);
}
int queue_push (queue_t *queue, int item)
{
queue_item_t *p;
assert (queue != NULL);
assert (queue->m_tailnext != NULL);
// create new queue item and insert it into tail
p = malloc (sizeof (queue_item_t));
if (!p)
return 0;
p->m_next = NULL;
p->m_item = item;
*queue->m_tailnext = p;
queue->m_tailnext = &(p->m_next);
++queue->m_length;
return 1; }
int queue_pop (queue_t *queue) {
queue_item_t *p;
assert (!queue_is_empty (queue));
// detach head and return the item
p = queue->m_head;
if (p)
{
int item = p->m_item;
queue->m_head = p->m_next;
// if the last one was removed than
// we should reset our tail
if (queue->m_tailnext == &(p->m_next))
queue->m_tailnext = &(queue->m_head);
free (p);
--queue->m_length;
assert (queue->m_length >= 0);
return item;
}
assert (1 != 1);
// should not happen
return 0;
}
int queue_is_empty (queue_t *queue)
{
assert (queue != NULL);
return queue->m_length <= 0;
}Для работы с созданной библиотекой следует создать проект с главной функцией main(), в которой подключаются файлы, расположенные в созданной статической библиотеке, с помощью директив #include. Для подключаемых файлов необходимо указать путь, где они расположены. Обычно это делается с помощью нотации "..\..\stack.h", которая указывает, что файл stack.h расположен на два уровня выше, чем функция main(), в которой он будет использоваться.