FileBase.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2013 ARM Limited
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "platform/FileBase.h"
  17. #include "platform/FileLike.h"
  18. #include "platform/FileHandle.h"
  19. namespace mbed {
  20. FileBase *FileBase::_head = NULL;
  21. SingletonPtr<PlatformMutex> FileBase::_mutex;
  22. FileBase::FileBase(const char *name, PathType t) : _next(NULL),
  23. _name(name),
  24. _path_type(t)
  25. {
  26. _mutex->lock();
  27. if (name != NULL) {
  28. // put this object at head of the list
  29. _next = _head;
  30. _head = this;
  31. } else {
  32. _next = NULL;
  33. }
  34. _mutex->unlock();
  35. }
  36. FileBase::~FileBase()
  37. {
  38. _mutex->lock();
  39. if (_name != NULL) {
  40. // remove this object from the list
  41. if (_head == this) { // first in the list, so just drop me
  42. _head = _next;
  43. } else { // find the object before me, then drop me
  44. FileBase *p = _head;
  45. while (p->_next != this) {
  46. p = p->_next;
  47. }
  48. p->_next = _next;
  49. }
  50. }
  51. _mutex->unlock();
  52. if (getPathType() == FilePathType) {
  53. extern void remove_filehandle(FileHandle * file);
  54. remove_filehandle(static_cast<FileHandle *>(static_cast<FileLike *>(this)));
  55. }
  56. }
  57. FileBase *FileBase::lookup(const char *name, unsigned int len)
  58. {
  59. _mutex->lock();
  60. FileBase *p = _head;
  61. while (p != NULL) {
  62. /* Check that p->_name matches name and is the correct length */
  63. if (p->_name != NULL && std::strncmp(p->_name, name, len) == 0 && std::strlen(p->_name) == len) {
  64. _mutex->unlock();
  65. return p;
  66. }
  67. p = p->_next;
  68. }
  69. _mutex->unlock();
  70. return NULL;
  71. }
  72. FileBase *FileBase::get(int n)
  73. {
  74. _mutex->lock();
  75. FileBase *p = _head;
  76. int m = 0;
  77. while (p != NULL) {
  78. if (m == n) {
  79. _mutex->unlock();
  80. return p;
  81. }
  82. m++;
  83. p = p->_next;
  84. }
  85. _mutex->unlock();
  86. return NULL;
  87. }
  88. const char *FileBase::getName(void)
  89. {
  90. // Constant read so no lock needed
  91. return _name;
  92. }
  93. PathType FileBase::getPathType(void)
  94. {
  95. // Constant read so no lock needed
  96. return _path_type;
  97. }
  98. } // namespace mbed