Ticket #7052: Deskbar_ResourceSet_cpp_style_fix.diff

File Deskbar_ResourceSet_cpp_style_fix.diff, 13.7 KB (added by jscipione, 13 years ago)
  • src/apps/deskbar/ResourceSet.cpp

    diff --git a/src/apps/deskbar/ResourceSet.cpp b/src/apps/deskbar/ResourceSet.cpp
    index 6bbb33b..7fd0f29 100644
    a b Except as contained in this notice, the name of Be Incorporated shall not be  
    2626used in advertising or otherwise to promote the sale, use or other dealings in
    2727this Software without prior written authorization from Be Incorporated.
    2828
    29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
    30 of Be Incorporated in the United States and other countries. Other brand product
    31 names are registered trademarks or trademarks of their respective holders.
     29Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered
     30trademarks of Be Incorporated in the United States and other countries. Other
     31brand product names are registered trademarks or trademarks of their respective
     32holders.
    3233All rights reserved.
    3334*/
    3435
    namespace TResourcePrivate {  
    7071            :   fDeleteOK(false)
    7172        {
    7273        }
    73        
     74
    7475        virtual ~TypeObject()
    7576        {
    7677            if (!fDeleteOK)
    7778                debugger("deleting object owned by BResourceSet");
    7879        }
    79        
     80
    8081        void Delete()
    8182        {
    8283            fDeleteOK = true;
    8384        }
    84        
     85
    8586    private:
    8687        TypeObject(const TypeObject &);
    8788        TypeObject &operator=(const TypeObject &);
    8889        bool operator==(const TypeObject &);
    8990        bool operator!=(const TypeObject &);
    90        
     91
    9192        bool fDeleteOK;
    9293    };
    93    
     94
    9495    class BitmapTypeItem : public BBitmap, public TypeObject {
    9596    public:
    9697        BitmapTypeItem(BRect bounds, uint32 flags, color_space depth,
    97             int32 bytesPerRow=B_ANY_BYTES_PER_ROW, screen_id screenID
     98            int32 bytesPerRow = B_ANY_BYTES_PER_ROW, screen_id screenID
    9899            = B_MAIN_SCREEN_ID)
    99100            :   BBitmap(bounds, flags, depth, bytesPerRow, screenID)
    100101        {
    101102        }
    102        
     103
    103104        BitmapTypeItem(const BBitmap* source, bool accepts_views = false,
    104105            bool need_contiguous = false)
    105106            :   BBitmap(source, accepts_views, need_contiguous)
    106107        {
    107108        }
    108        
     109
    109110        BitmapTypeItem(BMessage* data)
    110111            :   BBitmap(data)
    111112        {
    112113        }
    113        
     114
    114115        virtual ~BitmapTypeItem()
    115116        {
    116117        }
    117118    };
    118    
     119
    119120    class StringBlockTypeItem : public TStringBlock, public TypeObject {
    120121    public:
    121122        StringBlockTypeItem(BDataIO* data)
    122123            :   TStringBlock(data)
    123124        {
    124125        }
    125        
     126
    126127        StringBlockTypeItem(const void* block, size_t size)
    127128            :   TStringBlock(block, size)
    128129        {
    129130        }
    130        
     131
    131132        virtual ~StringBlockTypeItem()
    132133        {
    133134        }
    134135    };
    135    
     136
    136137    class TypeItem {
    137138    public:
    138139        TypeItem(int32 id, const char* name, const void* data, size_t size)
    139             : fID(id), fName(name),
    140               fData(const_cast<void*>(data)), fSize(size), fObject(0),
    141               fOwnData(false), fSourceIsFile(false)
     140            :   fID(id), fName(name),
     141                fData(const_cast<void*>(data)), fSize(size), fObject(0),
     142                fOwnData(false), fSourceIsFile(false)
    142143        {
    143144        }
    144        
     145
    145146        TypeItem(int32 id, const char* name, BFile* file)
    146147            :   fID(id),
    147148                fName(name),
    namespace TResourcePrivate {  
    162163                }
    163164            }
    164165        }
    165        
     166
    166167        virtual ~TypeItem()
    167168        {
    168169            if (fOwnData) {
    namespace TResourcePrivate {  
    172173            }
    173174            SetObject(NULL);
    174175        }
    175        
     176
    176177        int32 ID() const
    177             { return fID; }
     178        {
     179            return fID;
     180        }
    178181
    179182        const char* Name() const
    180             { return fName.String(); }
     183        {
     184            return fName.String();
     185        }
    181186
    182187        const void* Data() const
    183             { return fData; }
     188        {
     189            return fData;
     190        }
    184191
    185192        size_t Size() const
    186             { return fSize; }
    187        
     193        {
     194            return fSize;
     195        }
     196
    188197        void SetObject(TypeObject* object)
    189198        {
    190199            if (object == fObject)
    namespace TResourcePrivate {  
    195204        }
    196205
    197206        TypeObject* Object() const
    198             { return fObject; }
    199        
     207        {
     208            return fObject;
     209        }
     210
    200211        void SetSourceIsFile(bool state)
    201             { fSourceIsFile = state; }
     212        {
     213            fSourceIsFile = state;
     214        }
    202215
    203216        bool SourceIsFile() const
    204             { return fSourceIsFile; }
    205        
     217        {
     218            return fSourceIsFile;
     219        }
     220
    206221    private:
    207222        int32 fID;
    208223        BString fName;
    namespace TResourcePrivate {  
    212227        bool fOwnData;
    213228        bool fSourceIsFile;
    214229    };
    215    
     230
    216231    static bool FreeTypeItemFunc(void* item)
    217232    {
    218233        delete reinterpret_cast<TypeItem*>(item);
    namespace TResourcePrivate {  
    225240            :   fType(type)
    226241        {
    227242        }
    228        
     243
    229244        virtual ~TypeList()
    230245        {
    231246            fItems.DoForEach(FreeTypeItemFunc);
    232247            fItems.MakeEmpty();
    233248        }
    234        
     249
    235250        type_code Type() const
    236             { return fType; }
    237        
     251        {
     252            return fType;
     253        }
     254
    238255        TypeItem* FindItemByID(int32 id)
    239256        {
    240257            for (int32 i = 0; i < fItems.CountItems(); i++ ) {
    namespace TResourcePrivate {  
    254271            }
    255272            return NULL;
    256273        }
    257        
     274
    258275        void AddItem(TypeItem* item)
    259276        {
    260277            fItems.AddItem(item);
    261278        }
    262        
     279
    263280    private:
    264281        type_code fType;
    265282        BList fItems;
    266283    };
    267 
    268284}
    269285
    270286using namespace TResourcePrivate;
    TStringBlock::TStringBlock(BDataIO* data)  
    282298    fStrings = (char*)malloc(1024);
    283299    size_t pos = 0;
    284300    ssize_t amount;
    285     while ((amount=data->Read(fStrings + pos, 1024)) == 1024) {
     301    while ((amount = data->Read(fStrings + pos, 1024)) == 1024) {
    286302        pos += amount;
    287303        fStrings = (char*)realloc(fStrings, pos + 1024);
    288304    }
    289305    if (amount > 0)
    290306        pos += amount;
    291    
     307
    292308    fNumEntries = PreIndex(fStrings, amount);
    293309    fIndex = (size_t*)malloc(sizeof(size_t) * fNumEntries);
    294310    MakeIndex(fStrings, amount, fNumEntries, fIndex);
    TStringBlock::TStringBlock(const void* block, size_t size)  
    304320{
    305321    fIndex = (size_t*)const_cast<void*>(block);
    306322    fStrings = (char*)const_cast<void*>(block);
    307    
     323
    308324    // Figure out how many entries there are.
    309325    size_t last_off = 0;
    310326    while (fIndex[fNumEntries] > last_off && fIndex[fNumEntries] < size ) {
    size_t  
    339355TStringBlock::PreIndex(char* strings, ssize_t len)
    340356{
    341357    size_t count = 0;
    342    
    343358    char* orig = strings;
    344359    char* end = strings + len;
    345360    bool in_cr = false;
    346361    bool first = true;
    347362    bool skipping = false;
     363
    348364    while (orig < end) {
    349365        if (*orig == '\n' || *orig == '\r' || *orig == 0) {
    350366            if (!in_cr && *orig == '\r')
    TStringBlock::PreIndex(char* strings, ssize_t len)  
    371387                case '\\':
    372388                    *strings = '\\';
    373389                    break;
    374                    
     390
    375391                case '\n':
    376392                    *strings = '\n';
    377393                    break;
    378                    
     394
    379395                case '\r':
    380396                    *strings = '\r';
    381397                    break;
    382                    
     398
    383399                case '\t':
    384400                    *strings = '\t';
    385401                    break;
    386                    
     402
    387403                default:
    388404                    *strings = *orig;
    389405                    break;
    390406            }
    391         } else 
     407        } else
    392408            *strings = *orig;
    393409
    394410        orig++;
    395411        strings++;
    396412    }
    397    
    398413    return count;
    399414}
    400415
    TStringBlock::MakeIndex(const char* strings, ssize_t len,  
    405420{
    406421    *resultingIndex++ = 0;
    407422    indexLength--;
    408    
     423
    409424    ssize_t pos = 0;
    410425    while (pos < len && indexLength > 0) {
    411426        if (strings[pos] == 0 ) {
    TResourceSet::AddResources(BResources* RESOURCES_ONLY(resources))  
    475490#if USE_RESOURCES
    476491    if (!resources)
    477492        return B_BAD_VALUE;
    478    
     493
    479494    BAutolock lock(&fLock);
    480495    status_t err = fResources.AddItem(resources) ? B_OK : B_ERROR;
    481496    if (err != B_OK)
    TResourceSet::AddDirectory(const char* fullPath)  
    499514        delete path;
    500515        return err;
    501516    }
    502    
     517
    503518    BAutolock lock(&fLock);
    504519    err = fDirectories.AddItem(path) ? B_OK : B_ERROR;
    505520    if (err != B_OK)
    TResourceSet::AddEnvDirectory(const char* in, const char* defaultValue)  
    514529{
    515530    BString buf;
    516531    status_t err = ExpandString(&buf, in);
    517    
     532
    518533    if (err != B_OK) {
    519534        if (defaultValue)
    520535            return AddDirectory(defaultValue);
    521536        return err;
    522537    }
    523    
     538
    524539    return AddDirectory(buf.String());
    525540}
    526541
    status_t  
    529544TResourceSet::ExpandString(BString* out, const char* in)
    530545{
    531546    const char* start = in;
     547
    532548    while (*in) {
    533549        if (*in == '$') {
    534550            if (start < in)
    535                  out->Append(start, (int32)(in - start));
    536            
     551                out->Append(start, (int32)(in - start));
     552
    537553            in++;
    538554            char variableName[1024];
    539555            size_t i = 0;
    TResourceSet::ExpandString(BString* out, const char* in)  
    545561                if (*in)
    546562                    in++;
    547563
    548             } else
    549                 while ((isalnum(*in) || *in == '_') && i
    550                     < sizeof(variableName) - 1)
     564            } else {
     565                while ((isalnum(*in) || *in == '_')
     566                    && i < sizeof(variableName) - 1)
    551567                    variableName[i++] = *in++;
    552            
     568            }
     569
    553570            start = in;
    554            
    555571            variableName[i] = '\0';
    556            
     572
    557573            const char* val = getenv(variableName);
    558574            if (!val) {
    559575                PRINT(("Error: env var %s not found.\n", &variableName[0]));
    560576                return B_NAME_NOT_FOUND;
    561577            }
    562            
     578
    563579            status_t err = ExpandString(out, val);
    564580            if (err != B_OK)
    565581                return err;
    566            
     582
    567583        } else if (*in == '\\') {
    568584            if (start < in)
    569585                out->Append(start, (int32)(in - start));
    TResourceSet::ExpandString(BString* out, const char* in)  
    576592
    577593    if (start < in)
    578594        out->Append(start, (int32)(in - start));
    579    
     595
    580596    return B_OK;
    581597}
    582598
    const void*  
    585601TResourceSet::FindResource(type_code type, int32 id, size_t* outSize)
    586602{
    587603    TypeItem* item = FindItemID(type, id);
    588    
     604
    589605    if (outSize)
    590606        *outSize = item ? item->Size() : 0;
    591607
    const void*  
    597613TResourceSet::FindResource(type_code type, const char* name, size_t* outSize)
    598614{
    599615    TypeItem* item = FindItemName(type, name);
    600    
     616
    601617    if (outSize)
    602618        *outSize = item ? item->Size() : 0;
    603619
    TResourceSet::FindStringBlock(type_code type, const char* name)  
    632648    return ReturnStringBlockItem(FindItemName(type, name));
    633649}
    634650
    635    
     651
    636652const char*
    637653TResourceSet::FindString(type_code type, int32 id, uint32 index)
    638654{
    639655    const TStringBlock* stringBlock = FindStringBlock(type, id);
     656
    640657    if (!stringBlock)
    641658        return NULL;
    642659
    const char*  
    648665TResourceSet::FindString(type_code type, const char* name, uint32 index)
    649666{
    650667    const TStringBlock* stringBlock = FindStringBlock(type, name);
     668
    651669    if (!stringBlock)
    652670        return NULL;
    653671
    654672    return stringBlock->String(index);
    655673}
    656674
    657    
     675
    658676TypeList*
    659677TResourceSet::FindTypeList(type_code type)
    660678{
    661679    BAutolock lock(&fLock);
    662    
     680
    663681    int32 count = fTypes.CountItems();
    664682    for (int32 i = 0; i < count; i++ ) {
    665683        TypeList* list = (TypeList*)fTypes.ItemAt(i);
    666684        if (list && list->Type() == type)
    667685            return list;
    668686    }
    669    
     687
    670688    return NULL;
    671689}
    672690
    TResourceSet::FindItemID(type_code type, int32 id)  
    676694{
    677695    TypeList* list = FindTypeList(type);
    678696    TypeItem* item = NULL;
    679    
    680     if (list) item = list->FindItemByID(id);
    681    
     697
     698    if (list)
     699        item = list->FindItemByID(id);
     700
    682701    if (!item)
    683702        item = LoadResource(type, id, 0, &list);
    684    
     703
    685704    return item;
    686705}
    687706
    TResourceSet::FindItemName(type_code type, const char* name)  
    691710{
    692711    TypeList* list = FindTypeList(type);
    693712    TypeItem* item = NULL;
    694    
     713
    695714    if (list)
    696715        item = list->FindItemByName(name);
    697    
     716
    698717    if (!item)
    699718        item = LoadResource(type, -1, name, &list);
    700    
     719
    701720    return item;
    702721}
    703722
    TResourceSet::LoadResource(type_code type, int32 id, const char* name,  
    707726    TypeList** inOutList)
    708727{
    709728    TypeItem* item = NULL;
    710    
     729
    711730    if (name) {
    712731        BEntry entry;
    713        
     732
    714733        // If a named resource, first look in directories.
    715734        fLock.Lock();
    716735        int32 count = fDirectories.CountItems();
    TResourceSet::LoadResource(type_code type, int32 id, const char* name,  
    731750        }
    732751        fLock.Unlock();
    733752    }
    734    
     753
    735754#if USE_RESOURCES
    736755    if (!item) {
    737756        // Look through resource objects for data.
    TResourceSet::LoadResource(type_code type, int32 id, const char* name,  
    746765                    data = resource->LoadResource(type, id, &size);
    747766                else if (name != NULL)
    748767                    data = resource->LoadResource(type, name, &size);
    749                    
     768
    750769                if (data && size) {
    751770                    item = new TypeItem(id, name, data, size);
    752771                    item->SetSourceIsFile(false);
    TResourceSet::ReturnBitmapItem(type_code, TypeItem* from)  
    787806{
    788807    if (!from)
    789808        return NULL;
    790    
     809
    791810    TypeObject* obj = from->Object();
    792811    BitmapTypeItem* bitmap = dynamic_cast<BitmapTypeItem*>(obj);
    793812    if (bitmap)
    TResourceSet::ReturnBitmapItem(type_code, TypeItem* from)  
    803822    // Try to read as an archived bitmap.
    804823    stream.Seek(0, SEEK_SET);
    805824    BMessage archive;
    806     if (archive.Unflatten(&stream) == B_OK ) {
     825    if (archive.Unflatten(&stream) == B_OK) {
    807826        bitmap = new BitmapTypeItem(&archive);
    808827        if (bitmap && bitmap->InitCheck() != B_OK) {
    809             bitmap->Delete();   // allows us to delete this bitmap...
     828            bitmap->Delete();
     829                // allows us to delete this bitmap...
    810830            delete bitmap;
    811831            bitmap = NULL;
    812832        }
    TResourceSet::ReturnBitmapItem(type_code, TypeItem* from)  
    815835    if (bitmap) {
    816836        BAutolock lock(&fLock);
    817837        if (from->Object() != NULL) {
    818             // Whoops!  Someone snuck in under us.
     838            // Whoops! Someone snuck in under us.
    819839            bitmap->Delete();
    820840            delete bitmap;
    821841            bitmap = dynamic_cast<BitmapTypeItem*>(from->Object());
    822         } else 
     842        } else
    823843            from->SetObject(bitmap);
    824844    }
    825845
    TResourceSet::ReturnStringBlockItem(TypeItem* from)  
    832852{
    833853    if (!from)
    834854        return NULL;
    835    
     855
    836856    TypeObject* obj = from->Object();
    837857    StringBlockTypeItem* stringBlock = dynamic_cast<StringBlockTypeItem*>(obj);
    838858    if (stringBlock)
    839859        return stringBlock;
    840    
     860
    841861    // Can't change an existing object.
    842862    if (obj)
    843863        return NULL;
    844    
     864
    845865    // Don't have a string block in the item -- we'll create one.
    846866    if (from->SourceIsFile() ) {
    847867        BMemoryIO stream(from->Data(), from->Size());
    848868        stringBlock = new StringBlockTypeItem(&stream);
    849869    } else
    850870        stringBlock = new StringBlockTypeItem(from->Data(), from->Size());
    851    
     871
    852872    if (stringBlock) {
    853873        BAutolock lock(&fLock);
    854874        if (from->Object() != NULL) {
    855             // Whoops!  Someone snuck in under us.
     875            // Whoops! Someone snuck in under us.
    856876            delete stringBlock;
    857877            stringBlock = dynamic_cast<StringBlockTypeItem*>(from->Object());
    858878        } else
    859879            from->SetObject(stringBlock);
    860880    }
    861    
     881
    862882    return stringBlock;
    863883}
    864884
    TResourceSet::ReturnStringBlockItem(TypeItem* from)  
    867887
    868888
    869889namespace TResourcePrivate {
    870 
    871890    TResourceSet* gResources = NULL;
    872891    BLocker gResourceLocker;
    873    
    874892}
    875893
    876894
    AppResSet()  
    880898    // If already have it, return immediately.
    881899    if (gResources)
    882900        return gResources;
    883    
     901
    884902    // Don't have 'em, lock access to make 'em.
    885903    if (!gResourceLocker.Lock())
    886904        return NULL;
    AppResSet()  
    889907        gResourceLocker.Unlock();
    890908        return gResources;
    891909    }
    892    
     910
    893911    // Make 'em.
    894912    gResources = new TResourceSet;
    895913    gResources->AddResources(BApplication::AppResources());