PageRenderTime 29ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/libtunepimp-0.5.3/lib/fileio.cpp.glibc210_strrchr.patch

#
Patch | 199 lines | 159 code | 40 blank | 0 comment | 0 complexity | 321f6aa642ef5b2f12fc7b10c2aadb3b MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, GPL-2.0, LGPL-2.0
  1. /*----------------------------------------------------------------------------
  2. libtunepimp -- The MusicBrainz tagging library.
  3. Let a thousand taggers bloom!
  4. Copyright (C) Robert Kaye 2003
  5. This file is part of libtunepimp.
  6. libtunepimp is free software you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation either version 2 of the License, or
  9. (at your option) any later version.
  10. libtunepimp is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with libtunepimp if not, write to the Free Software
  16. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. $Id: fileio.cpp 1416 2005-07-03 06:48:17Z robert $
  18. ----------------------------------------------------------------------------*/
  19. #include <assert.h>
  20. #include <errno.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <string>
  24. #ifndef WIN32
  25. #include <unistd.h>
  26. #endif
  27. #include <sys/stat.h>
  28. #include <sys/types.h>
  29. using namespace std;
  30. #include "utf8/utf8util.h"
  31. #include "fileio.h"
  32. static int uniqueId = 0;
  33. #define DB printf("%s:%d\n", __FILE__, __LINE__);
  34. #ifdef __cplusplus
  35. extern "C"
  36. {
  37. #endif
  38. static const char *dirSep = "/";
  39. static const char dirSepChar = '/';
  40. static int copyAndDelete(const char *from, const char *to, const char *encoding);
  41. #define BUF_SIZE 4096
  42. TFILE *topen(const char *path, const char *mode, const char *encoding)
  43. {
  44. return (TFILE *)fopen(utf8ToEncoding(path, encoding).c_str(), mode);
  45. }
  46. size_t tread(void *ptr, size_t size, size_t nmemb, TFILE *stream)
  47. {
  48. return fread(ptr, size, nmemb, (FILE *)stream);
  49. }
  50. size_t twrite(const void *ptr, size_t size, size_t nmemb, TFILE *stream)
  51. {
  52. return fwrite(ptr, size, nmemb, (FILE *)stream);
  53. }
  54. int tseek(TFILE *stream, long offset, int whence)
  55. {
  56. return fseek((FILE *)stream, offset, whence);
  57. }
  58. long ttell(TFILE *stream)
  59. {
  60. return ftell((FILE *)stream);
  61. }
  62. int tclose(TFILE *stream)
  63. {
  64. return fclose((FILE*)stream);
  65. }
  66. int tflush(TFILE *stream)
  67. {
  68. return fflush((FILE *)stream);
  69. }
  70. int tunlink(const char *pathname, const char *encoding)
  71. {
  72. return unlink(utf8ToEncoding(pathname, encoding).c_str());
  73. }
  74. int trename(const char *oldpath, const char *newpath, const char *encoding)
  75. {
  76. int ret;
  77. ret = rename(utf8ToEncoding(oldpath, encoding).c_str(), utf8ToEncoding(newpath, encoding).c_str());
  78. if (ret && errno == EXDEV)
  79. return copyAndDelete(oldpath, newpath, encoding);
  80. return ret;
  81. }
  82. int tmkdir(const char *pathname, const char *encoding)
  83. {
  84. return mkdir(utf8ToEncoding(pathname, encoding).c_str(), 0755);
  85. }
  86. int trmdir(const char *pathname, const char *encoding)
  87. {
  88. return rmdir(utf8ToEncoding(pathname, encoding).c_str());
  89. }
  90. int taccess(const char *pathname, int mode, const char *encoding)
  91. {
  92. assert(mode == F_OK);
  93. return access(utf8ToEncoding(pathname, encoding).c_str(), mode);
  94. }
  95. void tmktempname(const char *path, char *newPath, int newPathLen)
  96. {
  97. char *ptr, *temp;
  98. temp = (char *)malloc(strlen(path) + 32);
  99. ptr = strrchr(path, dirSepChar);
  100. if (ptr)
  101. {
  102. int len = (int)(ptr - path);
  103. strncpy(temp, path, len);
  104. temp[len] = 0;
  105. }
  106. else
  107. strcpy(temp, ".");
  108. strcat(temp, dirSep);
  109. sprintf(temp + strlen(temp), "libtp%d%d.temp", (int)getpid(), uniqueId++);
  110. strncpy(newPath, temp, newPathLen - 1);
  111. newPath[newPathLen - 1] = 0;
  112. free(temp);
  113. }
  114. //---------------------------------------------------------------------------
  115. static int copyAndDelete(const char *from, const char *to, const char *encoding)
  116. {
  117. TFILE *in, *out;
  118. int ret = 0;
  119. errno = 0;
  120. in = topen(from, "rb", encoding);
  121. if (in == NULL)
  122. return -1;
  123. out = topen(to, "wb", encoding);
  124. if (out == NULL)
  125. {
  126. tclose(in);
  127. return -1;
  128. }
  129. char *buf = new char[BUF_SIZE];
  130. for(;;)
  131. {
  132. int numRead = tread(buf, sizeof(char), BUF_SIZE, in);
  133. if (numRead <= 0)
  134. break;
  135. int numWritten = twrite(buf, sizeof(char), numRead, out);
  136. if (numWritten != numRead)
  137. {
  138. //err = string("Could not write file to destination directory. Disk full?");
  139. ret = -1;
  140. break;
  141. }
  142. }
  143. tclose(in);
  144. tclose(out);
  145. delete [] buf;
  146. if (ret == 0)
  147. {
  148. ret = tunlink(from, encoding);
  149. if (ret < 0)
  150. {
  151. //err = string("Could remove old file: '") + from + string("'.");
  152. tunlink(to, encoding);
  153. }
  154. }
  155. return ret;
  156. }
  157. #ifdef __cplusplus
  158. }
  159. #endif