Glib学习(8) 动态字节数组 Byte Arrays

先上说明文档网址:http://web.mit.edu/barnowl/share/gtk-doc/html/glib/glib-Byte-Arrays.html


动态字节数组和前两个动态数组一样,只不过是用来存储字节数据的,这个数组的功能很像strings,和他的区别就是这个字节数组有处理二进制数据的能力,strings一般用来处理ascii字符,所以字节数组并不会以0作为结尾,这个特点在运行例子程序中也会看到,其他的就没有什么了,这个数组的用法与前两种几乎没有区别,所以我在例子代码中不做注释性解释。


结构体定义:

GByteArray

typedef struct {
  guint8 *data;
  guint	  len;
} GByteArray;

功能函数:

Synopsis

#include 


                    GByteArray;
GByteArray*         g_byte_array_new                    (void);
GByteArray*         g_byte_array_sized_new              (guint reserved_size);
GByteArray*         g_byte_array_append                 (GByteArray *array,
                                                         const guint8 *data,
                                                         guint len);
GByteArray*         g_byte_array_prepend                (GByteArray *array,
                                                         const guint8 *data,
                                                         guint len);
GByteArray*         g_byte_array_remove_index           (GByteArray *array,
                                                         guint index_);
GByteArray*         g_byte_array_remove_index_fast      (GByteArray *array,
                                                         guint index_);
GByteArray*         g_byte_array_remove_range           (GByteArray *array,
                                                         guint index_,
                                                         guint length);
void                g_byte_array_sort                   (GByteArray *array,
                                                         GCompareFunc compare_func);
void                g_byte_array_sort_with_data         (GByteArray *array,
                                                         GCompareDataFunc compare_func,
                                                         gpointer user_data);
GByteArray*         g_byte_array_set_size               (GByteArray *array,
                                                         guint length);
guint8*             g_byte_array_free                   (GByteArray *array,
                                                         gboolean free_segment);

例子代码:

#include 

static gint
sort(gconstpointer p1, gconstpointer p2)
{
    char a, b;
    
    a = *(char *)(p1);
    b = *(char *)(p2);

    return (a > b ? +1 : a == b ? 0 : -1);
}

static gint
sort_r(gconstpointer p1, gconstpointer p2, gpointer user_data)
{
    char a, b;
    
    a = *(char *)(p1);
    b = *(char *)(p2);

    return (a < b ? +1 : a == b ? 0 : -1);
}

static void
test_array(void)
{
    GByteArray *gbarray = NULL;
    gint i;

    gbarray = g_byte_array_new();

    g_byte_array_append (gbarray, (guint8*) "abcde12345", 10);

    g_printf("There should be '%d' items now.\t\tResult: %d.\n", 10, gbarray->len);
    g_printf("All of items:\n");
    g_printf("%s\n", gbarray->data);

    g_printf("All of items[after append]: len:%d\n", gbarray->len);
    g_byte_array_append(gbarray,(guint8*) "append!", 7);
    g_printf("%s\n", gbarray->data);

    g_printf("All of items[after prepend]: len:%d\n", gbarray->len);
    g_byte_array_prepend(gbarray, (guint8*)"prepend!", 8);
    g_printf("%s\n", gbarray->data);

    g_byte_array_remove_index(gbarray, 1);
    g_printf("All of items[exclude the second item]: len:%d\n", gbarray->len);
    g_printf("%s\n", gbarray->data);

    g_byte_array_remove_index_fast(gbarray, 1);
    g_printf("All of items[exclude the second item]: len:%d\n", gbarray->len);
    g_printf("%s\n", gbarray->data);

    g_byte_array_remove_range(gbarray, 2, 2);
    g_printf("All of items[after remove 2 items from the third item]: len:%d\n", gbarray->len);
    g_printf("%s\n", gbarray->data);

    g_byte_array_sort(gbarray, sort);
    g_printf("All of items[sorted]: len:%d\n", gbarray->len);
    g_printf("%s\n", gbarray->data);

    g_byte_array_sort_with_data(gbarray, sort_r, NULL);
    g_printf("All of items[sorted reversed]: len:%d\n", gbarray->len);
    g_printf("%s\n", gbarray->data);

    g_byte_array_free(gbarray, TRUE);
}

int
main(void)
{
    g_printf("BEGIN:\n************************************************************\n");
    test_array();
    g_printf("\n************************************************************\nDONE\n");

    return 0;
}



运行结果:

linux@ubuntu:~/16021/glibdemo$ gcc Byte_Arrays.c -o Byte_Arrays -lglib-2.0
linux@ubuntu:~/16021/glibdemo$ ./Byte_Arrays 
BEGIN:
************************************************************
There should be '10' items now.		Result: 10.
All of items:
abcde12345
All of items[after append]: len:10
abcde12345append!
All of items[after prepend]: len:17
prepend!abcde12345append!�^�p�^�
All of items[exclude the second item]: len:24
pepend!abcde12345append!!�^�p�^�
All of items[exclude the second item]: len:23
p!pend!abcde12345append!!�^�p�^�
All of items[after remove 2 items from the third item]: len:21
p!nd!abcde12345appendnd!!�^�p�^�
All of items[sorted]: len:21
!!12345aabcdddeennpppnd!!�^�p�^�
All of items[sorted reversed]: len:21
pppnneedddcbaa54321!!nd!!�^�p�^�

************************************************************
DONE
linux@ubuntu:~/16021/glibdemo$ 

从上面的运行结果可以看出有些情况会出现乱码,这是因为printf函数通过检测‘\0’作为字符串结束,如果结尾没有0就会继续打印,而且可以看出即使删除了数据并将后面数据向前移动后最后面的数据依然存在,只是结构体的len改变了。



你可能感兴趣的:(glib)