123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- #include "blitz.h"
- void bbCAssertEx(){
- bbExThrowCString( "C Assert failed" );
- }
- static void debugNop(){
- }
- static void debugUnhandledEx( BBObject *ex ){
- bbWriteStderr( ex->clas->ToString( ex ) );
- exit(-1);
- }
- void (*bbOnDebugStop)()=debugNop;
- void (*bbOnDebugLog)( BBString *str )=debugNop;
- void (*bbOnDebugEnterStm)( BBDebugStm *stm )=debugNop;
- void (*bbOnDebugEnterScope)( BBDebugScope *scope )=debugNop;
- void (*bbOnDebugLeaveScope)()=debugNop;
- void (*bbOnDebugPushExState)()=debugNop;
- void (*bbOnDebugPopExState)()=debugNop;
- void (*bbOnDebugUnhandledEx)( BBObject *ex )=debugUnhandledEx;
- static unsigned int bpCount = 0;
- static unsigned int bpSize = 0;
- static BBSource * sources = 0;
- static void swap(BBSource* a, BBSource* b) {
- BBSource s = *a;
- *a = *b;
- *b = s;
- }
- static int partition (BBSource arr[], int low, int high) {
- BBULONG pivot = arr[high].id;
- int i = (low - 1);
- int j;
- for (j = low; j <= high- 1; j++) {
- if (arr[j].id < pivot) {
- i++;
- swap(&arr[i], &arr[j]);
- }
- }
- swap(&arr[i + 1], &arr[high]);
- return (i + 1);
- }
- static void sort(BBSource arr[], int low, int high) {
- if (low < high) {
- int part = partition(arr, low, high);
- sort(arr, low, part - 1);
- sort(arr, part + 1, high);
- }
- }
- void bbRegisterSource(BBULONG sourceId, const char * source) {
- if (sources == 0) {
- bpSize = 32;
- sources = calloc(bpSize, sizeof(BBSource));
- } else {
- if (bpCount == bpSize) {
- BBSource * bp = calloc(bpSize * 2, sizeof(BBSource));
- memcpy(bp, sources, bpSize * sizeof(BBSource));
- BBSource * old = sources;
- sources = bp;
- free(old);
- bpSize *= 2;
- }
- }
-
- sources[bpCount].id = sourceId;
- sources[bpCount].file = source;
-
- bpCount++;
-
- if (bpCount > 1) {
- sort(sources, 0, bpCount - 1);
- }
- }
- BBSource * bbSourceForId(BBULONG id) {
- if (bpCount > 0) {
- unsigned int first = 0;
- unsigned int last = bpCount - 1;
- unsigned int index = 0;
-
- while (first <= last) {
- index = (first + last) / 2;
- if (sources[index].id == id) {
- return &sources[index];
- } else {
- if (sources[index].id < id) {
- first = index + 1;
- } else {
- if (index == 0) {
- return 0;
- }
- last = index - 1;
- }
- }
- }
- }
- return 0;
- }
- BBSource * bbSourceForName(BBString * filename) {
- if (bpCount > 0) {
- char path[512];
- size_t len = 512;
- bbStringToUTF8StringBuffer(filename, path, &len);
- path[len] = 0;
-
- int i;
- for (i = 0; i < bpCount; i++) {
- if (strcmp(path, sources[i].file) == 0) {
- return &sources[i];
- }
- }
- }
- return 0;
- }
|