📄 wchar_sort.c
字号:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "prjdb.h"#ifndef MCO_PLATFORM_X64#define PAGE_SIZE (uint2)128#else#define PAGE_SIZE (uint2)256#endif#define DATABASE_SIZE 1024*1024*70#define ADDRESS 0x2000000char * dbname = "dbname";mco_db_h db = 0;/* Fatal error trap */static void errhandler( int n ) { fprintf(stdout, "\n*** eXtremeDB runtime fatal error: %d\n", n ); exit( -1 );}#define OBJ1 1#define OBJ2 2#define OBJ3 4#define OBJ4 8#define OBJ5 0x10typedef struct tagOne { struct tagOne * next; uint4 flags; uint4 line_no; void * line; uint4 line_len;}one_t, *one_h;one_h root;one_h * plast = &root;uint4 line_cnt = 0;void DumpShorts( char* rem, wchar_t * ptr, uint2 len ) { uint2 i; if ( rem ) fprintf( stdout, "%s:", rem ); for ( i=0; i<len; i++ ) { fprintf( stdout, "0x%08hx ", ptr[i] ); }; fprintf( stdout, "\n" );};int LoadFile( char * fn ) { FILE * f; void * mem_buf; uint4 mem_buf_sz; wchar_t * p, * n, * q; unsigned short * _p, * _n; MCO_RET rc; uint4 len_b; one_h one; if ( 0 == ( f = fopen( fn, "rb" ) ) ) return 1; fseek( f, 0, SEEK_END ); mem_buf_sz = ftell( f ); fseek( f, 0, SEEK_SET ); if ( mem_buf_sz < 2 ) { printf ( "The file is not a valid unicode text file.\n" ); return 1; };// if file contain wchar size(2) p=_p = mem_buf = malloc( mem_buf_sz + 2 ); fread( mem_buf, 1, mem_buf_sz, f ); fclose( f ); _p[( mem_buf_sz/2 )] = 0; if ( _p[0] != 0xFEFF ) { _n = _p; while ( ((unsigned long)_n - (unsigned long)_p) < mem_buf_sz ) { *_n = (*_n & 0xFF00) >> 8 | (*_n & 0xFF) << 8; _n++; }; }; if (sizeof(wchar_t)==4) { long ps=(mem_buf_sz/2)+1; p=malloc( (mem_buf_sz/2)*sizeof(wchar_t) + sizeof(wchar_t) ); _n=_p; n=p; while (ps) { *n=*_n; _n++;n++;ps--; } free(_p); mem_buf=_p=p; } if ( p[0] != 0xFEFF ) { printf ( "The file is not a valid unicode text file.\n" ); free( mem_buf ); return 1; }; p++; do { mco_trans_h t; Obj1 obj1; Obj2 obj2; Obj3 obj3; Obj4 obj4; Obj5 obj5; uint2 i, j, sz; wchar_t * pw; uint2 pw_len; n = p; while ( *n != 0xD && *n != 0xA && *n != 0 ) n++; len_b = (unsigned long)n - (unsigned long)p; one = (one_h) malloc( sizeof(one_t) ); one->next = 0; one->flags = 0; one->line_len = len_b; one->line = malloc( len_b+sizeof(wchar_t) ); memcpy( one->line, p, len_b ); q = (wchar_t*)(((mco_puint)one->line)+len_b); *q = 0; one->line_no = line_cnt; line_cnt ++; *plast = one; plast = &one->next; pw = (wchar_t*)one->line; pw_len = one->line_len / sizeof(wchar_t); /* begin a read-write transaction */ if ((rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t)) == MCO_S_OK) { Obj1_new ( t, &obj1 ); Obj1_line_no_put( &obj1, one->line_no ); Obj1_line_put ( &obj1, pw, pw_len ); sz = pw_len; if ( sz > 16 ) sz = 16; Obj1_part_put ( &obj1, pw, sz ); sz = pw_len / Obj1_parts_a_length; for (i=0;i<Obj1_parts_a_length;i++) { Obj1_parts_a_put( &obj1, i, &pw[i*sz], sz ); }; sz = pw_len / Obj1_parts_s_length; for (i=0;i<Obj1_parts_s_length;i++) { Obj1_parts_s_put( &obj1, i, &pw[i*sz], sz ); }; sz = pw_len / 3; Obj1_vect_a_alloc( &obj1, sz ); for ( i=0; i<sz ; i++ ) { Obj1_vect_a_put( &obj1, i, &pw[i*3], 3 ); }; sz = 1; for ( i=0; i<pw_len; i++ ) { if ( pw[i] == 0x20 ) sz ++; }; Obj1_vect_s_alloc( &obj1, sz ); i = j = 0; while ( i < pw_len ) { sz = 0; while ( (i+sz) < pw_len && pw[i+sz] != 0x20 ) sz++; Obj1_vect_s_put( &obj1, j, &pw[i], sz ); j ++; i += sz+1; }; if ( MCO_S_OK == (rc=mco_trans_commit(t) )) { one->flags |= OBJ1; } else { printf("Error: %d at %d ", rc, __LINE__ ); }; } else { printf( "Can't open a transaction. Error code: %d\n", rc); }; /* begin a read-write transaction */ if ((rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t)) == MCO_S_OK) { Obj2_new ( t, &obj2 ); Obj2_line_put ( &obj2, pw, pw_len ); if ( MCO_S_OK == (rc=mco_trans_commit(t) )) { one->flags |= OBJ2; } else { if ( rc != MCO_S_DUPLICATE ) printf("Error: %d at %d ", rc, __LINE__ ); }; } else { printf( "Can't open a transaction. Error code: %d\n", rc); }; /* begin a read-write transaction */ if ((rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t)) == MCO_S_OK) { Obj3_new ( t, &obj3 ); Obj3_line_put ( &obj3, pw, pw_len ); if ( MCO_S_OK == (rc=mco_trans_commit(t) )) { one->flags |= OBJ3; } else { if ( rc != MCO_S_DUPLICATE ) printf("Error: %d at %d ", rc, __LINE__ ); }; } else { printf( "Can't open a transaction. Error code: %d\n", rc); }; /* begin a read-write transaction */ if ((rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t)) == MCO_S_OK) { Obj4_new ( t, &obj4 ); Obj4_line_put ( &obj4, pw, pw_len ); if ( MCO_S_OK == (rc=mco_trans_commit(t) )) { one->flags |= OBJ4; } else { if ( rc != MCO_S_DUPLICATE ) printf("Error: %d at %d ", rc, __LINE__ ); }; } else { printf( "Can't open a transaction. Error code: %d\n", rc); }; /* begin a read-write transaction */ if ((rc = mco_trans_start(db, MCO_READ_WRITE, MCO_TRANS_FOREGROUND, &t)) == MCO_S_OK) { Obj5_new ( t, &obj5 ); Obj5_line_put ( &obj5, pw, pw_len ); if ( MCO_S_OK == (rc=mco_trans_commit(t) )) { one->flags |= OBJ5; } else { if ( rc != MCO_S_DUPLICATE ) printf("Error: %d at %d ", rc, __LINE__ ); }; } else { printf( "Can't open a transaction. Error code: %d\n", rc); }; if ( *n == 0 ) break; while ( *n != 0xA ) n++; p = n+1; } while ( 1 ); free( mem_buf );/* one = root; while ( one ) { DumpShorts( "chain", one->line, one->line_len / 2 ); one = one->next; };*/ return 0;};int CheckContent() { MCO_RET rc = MCO_S_OK; mco_trans_h t; Obj1 obj; one_h one = root; while ( rc == MCO_S_OK && one ) { /* begin a read-write transaction */ if ((rc = mco_trans_start(db, MCO_READ_ONLY, MCO_TRANS_FOREGROUND, &t)) == MCO_S_OK) { if ( MCO_S_OK == (rc = Obj1_by_line_no_find( t, one->line_no, &obj ))) { uint2 i, j, sz = 0; void * p; wchar_t * pw; wchar_t part[16]; wchar_t arr10[10]; /* wstring */ Obj1_line_size( &obj, &sz ); if ( sz*sizeof(wchar_t) != one->line_len ) { printf( "Invalid string size detected at %d. LineNo:%d.\n", __LINE__, one->line_no ); printf( "expected:%d found:%d\n", one->line_len / sizeof(wchar_t), sz ); break; }; p = malloc( (sz+1)*sizeof(wchar_t) ); Obj1_line_get( &obj, p, sz+1, &sz ); if ( sz*sizeof(wchar_t) != one->line_len || 0 != memcmp( one->line, p, one->line_len ) ) { printf( "Invalid string detected at %d. LineNo:%d.\n", __LINE__, one->line_no ); printf( "expected:%d found:%d\n", one->line_len / sizeof(wchar_t), sz ); DumpShorts( "expected", one->line, one->line_len/2 ); DumpShorts( " found", p, sz ); break; }; if ( ((wchar_t*)p)[sz] != 0 ) { printf( "No zero-terminator at the end of the string at %d. LineNo:%d. ", __LINE__, one->line_no ); break; }; free( p ); /* nchar */ sz = sizeof(part)/sizeof(part[0]); Obj1_part_get( &obj, part, sz ); if ( sz > one->line_len/sizeof(wchar_t) ) sz = one->line_len/sizeof(wchar_t); if ( 0 != memcmp( part, one->line, sz*sizeof(wchar_t) ) ) { printf( "Invalid array detected at %d. LineNo:%d.\n", __LINE__, one->line_no ); DumpShorts( "expected", one->line, one->line_len/sizeof(wchar_t) ); DumpShorts( " found", part, sz ); break; }; /* array of nchar */ sz = (one->line_len/sizeof(wchar_t)) / Obj1_parts_a_length; pw = one->line; for ( i=0; i<Obj1_parts_a_length; i++ ) { Obj1_parts_a_at( &obj, i, part, sizeof(part)/sizeof(part[0]) ); if ( 0 != memcmp( part, &pw[i*sz], sz > 10 ? 10 : sz ) ) { printf( "Invalid array item detected at %d. LineNo:%d.\n", __LINE__, one->line_no ); DumpShorts( "expected", &pw[i*sz], sz ); DumpShorts( " found", part, sz ); break; }; }; if ( i < Obj1_parts_a_length ) break; /* array of nstring */ sz = (one->line_len/sizeof(wchar_t)) / Obj1_parts_s_length; pw = one->line; for ( i=0; i<Obj1_parts_s_length; i++ ) { Obj1_parts_s_at_len( &obj, i, &j ); if ( j != sz ) { printf( "Invalid string size detected at %d. LineNo:%d.\n", __LINE__, one->line_no ); printf( "expected:%d found:%d\n", sz, j ); break; }; p = malloc( (j+1)*sizeof(wchar_t) ); Obj1_parts_s_at( &obj, i, p, j+1, &j ); if ( sz != j || 0 != memcmp( &pw[i*sz], p, sz ) ) { printf( "The strings are not equal at %d. LineNo:%d.\n", __LINE__, one->line_no ); printf( "expected:%d found:%d\n", sz, j ); DumpShorts( "expected", &pw[i*sz], sz ); DumpShorts( " found", p, sz ); break; }; if ( ((wchar_t*)p)[sz] != 0 ) { printf( "No zero-terminator at the end of the string at %d. LineNo:%d. ", __LINE__, one->line_no ); break; }; free( p ); }; if ( i < Obj1_parts_s_length ) break; /* vector of nchar */ sz = (one->line_len/sizeof(wchar_t)) / 3; Obj1_vect_a_size( &obj, &j );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -