| 1 | /*************************************************************************** |
| 2 | imagemanager.cpp - description |
| 3 | ------------------- |
| 4 | begin : Sat Aug 19 2000 |
| 5 | copyright : (C) 2000 by Waldemar Baraldi |
| 6 | email : [email protected] |
| 7 | ***************************************************************************/ |
| 8 | |
| 9 | /*************************************************************************** |
| 10 | * * |
| 11 | * This program is free software; you can redistribute it and/or modify * |
| 12 | * it under the terms of the GNU General Public License as published by * |
| 13 | * the Free Software Foundation; either version 2 of the License, or * |
| 14 | * (at your option) any later version. * |
| 15 | * * |
| 16 | ***************************************************************************/ |
| 17 | |
| 18 | #include "imagemanager.h" |
| 19 | |
| 20 | ImageManager::ImageManager():Object(){ |
| 21 | hash=new Hash(); |
| 22 | dirs=new List(); |
| 23 | base_dir=new Str(""); |
| 24 | dirs->insert(dirs->getFirst(), new Str("")); |
| 25 | } |
| 26 | |
| 27 | ImageManager::~ImageManager(){ |
| 28 | hash->empty(true); |
| 29 | dirs->empty(true); |
| 30 | delete base_dir; |
| 31 | delete hash; |
| 32 | delete dirs; |
| 33 | } |
| 34 | |
| 35 | void ImageManager::setBaseDir(Str * dir){ |
| 36 | delete base_dir; |
| 37 | base_dir=dir; |
| 38 | } |
| 39 | |
| 40 | Image * ImageManager::preLoadImage(Str * filename){ |
| 41 | Str * aux; |
| 42 | Str * sub_dir=NULL; |
| 43 | FILE * file; |
| 44 | Image * ima=NULL; |
| 45 | Index * ind = dirs->getFirst(); |
| 46 | while (!ima && ind!=dirs->getEnd()){ |
| 47 | sub_dir=new Str((Str*)(dirs->getObject(ind))); |
| 48 | sub_dir->concat("/"); |
| 49 | sub_dir->concat(filename); |
| 50 | aux=new Str(base_dir); |
| 51 | aux->concat("/"); |
| 52 | aux->concat(sub_dir); |
| 53 | if ((file=fopen(aux->get(), "r"))){ |
| 54 | fclose(file); |
| 55 | ima = new Image(new Str(aux)); |
| 56 | hash->add(sub_dir, ima); |
| 57 | delete aux; |
| 58 | delete filename; |
| 59 | }else{ |
| 60 | delete aux; |
| 61 | delete sub_dir; |
| 62 | } |
| 63 | ind=dirs->getNext(ind); |
| 64 | } |
| 65 | return (ima); |
| 66 | } |
| 67 | |
| 68 | Image * ImageManager::getImage(Str * filename){ |
| 69 | Image * ima=NULL; |
| 70 | Index * ind = dirs->getFirst(); |
| 71 | Str * aux; |
| 72 | Str slash = Str("/"); |
| 73 | while ((!ima) && (ind!=dirs->getEnd())){ |
| 74 | aux = new Str((Str*)(dirs->getObject(ind))); |
| 75 | aux->concat(&slash); |
| 76 | aux->concat(filename); |
| 77 | ima = (Image *)hash->find(aux); |
| 78 | ind=dirs->getNext(ind); |
| 79 | } |
| 80 | if (!ima) ima=preLoadImage(new Str(filename)); |
| 81 | if (ima){ |
| 82 | delete filename; |
| 83 | return ima; |
| 84 | } |
| 85 | printf("Fatal error: File %s not found\n", filename->get()); |
| 86 | delete filename; |
| 87 | exit(-1); |
| 88 | } |
| 89 | |
| 90 | void ImageManager::addDir(Str * dir){ |
| 91 | dirs->insert(dirs->getEnd(), dir); |
| 92 | } |
| 93 | |
| 94 | void ImageManager::addDirList(List * dir_list){ |
| 95 | Index * ind=dir_list->getFirst(); |
| 96 | while (!dir_list->isEnd(ind)){ |
| 97 | addDir((Str*)(dir_list->getObject(ind))); |
| 98 | ind=dir_list->getNext(ind); |
| 99 | } |
| 100 | dir_list->empty(false); |
| 101 | delete dir_list; |
| 102 | } |
| 103 | |
| 104 | void ImageManager::clearDirs(){ |
| 105 | dirs->empty(true); |
| 106 | dirs->insert(dirs->getFirst(), new Str("")); |
| 107 | } |
| 108 | |
| 109 | void ImageManager::empty(){ |
| 110 | hash->empty(true); |
| 111 | clearDirs(); |
| 112 | } |
| 113 |