iphone_email.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. Copyright (C) 2009-2011 id Software LLC, a ZeniMax Media company.
  3. Copyright (C) 2009 Id Software, Inc.
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include "iphone_email.h"
  17. #include <time.h>
  18. char *consoleBuffer = NULL; //buffer for the console output
  19. int size = 0;
  20. /*
  21. * ReplaceAt()
  22. * Replaces the char at location with the insertString
  23. */
  24. void ReplaceAt( char *oldString, const char *insertString, int location)
  25. {
  26. int length = strlen(oldString);
  27. int chunkLength = strlen(insertString);
  28. char *newString = malloc(length + chunkLength + 1);//the 1 includes space for the null terminating character
  29. #if 0
  30. //strcpy(newString, old);
  31. strncpy(newString, oldString, location);
  32. newString[location] = '\0';
  33. strcat(newString, insertString);
  34. strcpy(&newString[location+chunkLength], &oldString[location + 1]);
  35. free(oldString);
  36. oldString = newString;
  37. #endif
  38. //copy the front part
  39. char frontPart[location+1];
  40. strncpy(frontPart, oldString, location);
  41. frontPart[location] = '\0';
  42. printf("\nfrontPart: %s", frontPart);
  43. //copy the back part
  44. char backPart[length - location];
  45. strcpy(backPart, &oldString[location+1]);
  46. backPart[length - location - 1] = '\0';
  47. printf("\nbackPart: %s\n\n", backPart);
  48. //put it all together in the new string
  49. newString[0] = '\0';
  50. strcat(newString, frontPart);
  51. strcat(newString, insertString);
  52. strcat(newString, backPart);
  53. //delete old string
  54. free(oldString);
  55. //replace old string
  56. oldString = newString;
  57. }
  58. /*
  59. * AppendBuffer()
  60. * Directly appends the console buffer
  61. */
  62. void AppendBuffer(const char *buf)
  63. {
  64. int length = strlen(buf) + 1; //strlen doesn't include the null terminating character
  65. char *temp = malloc(length);
  66. strcpy(temp, buf);
  67. for (int i = 0; i < length; ++i)
  68. {
  69. if (temp[i] == ' ' || temp[i] == '\n' || temp[i] == '=' )
  70. temp[i] = '_';
  71. }
  72. #if 0
  73. int i = 0;
  74. while (temp[i] != '\0')
  75. {
  76. if (temp[i] == ' ')
  77. ReplaceAt(temp, "_testString_", i);
  78. ++i;
  79. }
  80. length = strlen(temp) + 1;
  81. #endif
  82. //copy the old & new string into a buffer
  83. char *newBuf = malloc(size + length);
  84. if (consoleBuffer)
  85. {
  86. strcpy(newBuf, consoleBuffer);
  87. }
  88. strcpy(&newBuf[size], temp);
  89. //delete the old string and have it point to the new one
  90. free(consoleBuffer);
  91. consoleBuffer = newBuf;
  92. size = strlen(consoleBuffer);
  93. //delete the temp string
  94. free(temp);
  95. }
  96. /*
  97. * AppendChunk()
  98. * Just append a chunk of the incoming string from start to i
  99. */
  100. void AppendChunk(const char *buf, int start, int i)
  101. {
  102. int size = i+1 - start;
  103. char chunk[size];
  104. chunk[0] = '\0';
  105. strncpy(chunk, &buf[start], size-1);
  106. chunk[size-1] = '\0';
  107. AppendBuffer(chunk);
  108. }
  109. /*
  110. * AppendConsoleBuffer()
  111. * Appends the console buffer while replacings non-URLscheme compatible text
  112. */
  113. void AppendConsoleBuffer(const char *buf)
  114. {
  115. int length = strlen(buf) + 1; //strlen doesn't include the null terminating character
  116. char *temp = malloc(length);
  117. strcpy(temp, buf);
  118. int start = 0;
  119. int i = 0;
  120. for (i = 0; i < length; ++i)
  121. {
  122. //replace space and = with _
  123. //spaces and = tend to mess up the URL scheme
  124. if (temp[i] == ' ' )
  125. {
  126. temp[i] = '_';
  127. AppendChunk(temp, start, i); ++i; start = i;
  128. AppendBuffer("\%20"); //line space
  129. }
  130. if (temp[i] == '=' )
  131. {
  132. temp[i] = '_';
  133. AppendChunk(temp, start, i); ++i; start = i;
  134. AppendBuffer("\%3D"); //=
  135. }
  136. if (temp[i] == '\n')
  137. {
  138. temp[i] = '_';
  139. AppendChunk(temp, start, i); ++i; start = i;
  140. AppendBuffer("\%0A"); //line return
  141. }
  142. }
  143. //append the last of the chunk
  144. i = length - 1;
  145. if (start < i)
  146. {
  147. AppendChunk(temp, start, i);
  148. }
  149. // AppendString("\%0A"); //line return
  150. }
  151. /*
  152. *
  153. * Email the console buffer to id
  154. *
  155. */
  156. void EmailConsole()
  157. {
  158. if (!consoleBuffer)
  159. return;
  160. //copy everything from consoleBuffer into a char*
  161. char *buffer = malloc(1024+size+1);
  162. #if 0
  163. time_t rawtime;
  164. struct tm * timeinfo;
  165. time ( &rawtime );
  166. timeinfo = gmtime ( &rawtime );
  167. char dateBuffer[128];
  168. sprintf(dateBuffer, asctime(timeinfo));
  169. for (int i = 0; i < strlen(dateBuffer); ++i)
  170. {
  171. if (dateBuffer[i] == ' ' || dateBuffer[i] == ':')
  172. dateBuffer[i] = '_';
  173. }
  174. printf("formatted time: %s\n", dateBuffer);
  175. sprintf( buffer, "mailto:iphonesupport@idsoftware.com?subject=DoomClassicReport%s&body=%s", dateBuffer, consoleBuffer);
  176. #else
  177. sprintf( buffer, "mailto:iphonesupport@idsoftware.com?subject=DoomClassicReport&body=%s", consoleBuffer);
  178. #endif
  179. //call the mail app
  180. NSURL *url = [[NSURL alloc] initWithString:[[NSString alloc] initWithCString:buffer]];
  181. [[UIApplication sharedApplication] openURL:url];
  182. free(buffer);
  183. }