Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. MYAPI_ERROR getObjectSize(MYAPIHandle h, int* returnedSize);
  2.  
  3. int getObjectSize(MYAPIHandle h, MYAPI_ERROR* returnedError);
  4.  
  5. int size;
  6. if(getObjectSize(h, &size) != MYAPI_SUCCESS) {
  7. // Error handling
  8. }
  9.  
  10. MYAPIError error;
  11. int size;
  12. size = getObjectSize(h, &error);
  13. if(error != MYAPI_SUCCESS) {
  14. // Error handling
  15. }
  16.  
  17. rc = func(..., int **return_array, size_t *array_length);
  18.  
  19. if ((rc = func(...)) != API_SUCCESS) {
  20. /* Error Handling */
  21. }
  22.  
  23. /* Check for valid arguments */
  24. if (NULL == return_array || NULL == array_length)
  25. return API_INVALID_ARGS;
  26.  
  27. #include <setjmp.h>
  28. #include <stdio.h>
  29.  
  30. jmp_buf x;
  31.  
  32. void f()
  33. {
  34. longjmp(x,5); // throw 5;
  35. }
  36.  
  37. int main()
  38. {
  39. // output of this program is 5.
  40.  
  41. int i = 0;
  42.  
  43. if ( (i = setjmp(x)) == 0 )// try{
  44. {
  45. f();
  46. } // } --> end of try{
  47. else // catch(i){
  48. {
  49. switch( i )
  50. {
  51. case 1:
  52. case 2:
  53. default: fprintf( stdout, "error code = %dn", i); break;
  54. }
  55. } // } --> end of catch(i){
  56. return 0;
  57. }
  58.  
  59. #include <stdio.h>
  60. #include <setjmp.h>
  61.  
  62. #define TRY do{ jmp_buf ex_buf__; if( !setjmp(ex_buf__) ){
  63. #define CATCH } else {
  64. #define ETRY } }while(0)
  65. #define THROW longjmp(ex_buf__, 1)
  66.  
  67. int
  68. main(int argc, char** argv)
  69. {
  70. TRY
  71. {
  72. printf("In Try Statementn");
  73. THROW;
  74. printf("I do not appearn");
  75. }
  76. CATCH
  77. {
  78. printf("Got Exception!n");
  79. }
  80. ETRY;
  81.  
  82. return 0;
  83. }
  84.  
  85. NSError *error = nil;
  86. if ([myThing doThingError: &error] == NO)
  87. {
  88. // error handling
  89. }
  90.  
  91. if (MyFunc())
  92. DoSomething();
  93.  
  94. int size;
  95. if(getObjectSize(h, &size) != MYAPI_SUCCESS) {
  96. // Error handling
  97. }
  98.  
  99. int size;
  100. MYAPIError rc;
  101.  
  102. rc = getObjectSize(h, &size)
  103. if ( rc != MYAPI_SUCCESS) {
  104. // Error handling
  105. }
  106.  
  107. int getObjectSize(MYAPIHandle h, int* returnedSize);
  108. MYAPI_ERROR LastError;
  109. MYAPI_ERROR* getLastError() {return LastError;};
  110. #define FUNC_SUCCESS 1
  111. #define FUNC_FAIL 0
  112.  
  113. if(getObjectSize(h, &size) != FUNC_SUCCESS ) {
  114. MYAPI_ERROR* error = getLastError();
  115. // error handling
  116. }
  117.  
  118. struct lnode *insert(char *data, int len, struct lnode *list) {
  119. struct lnode *p, *q;
  120. uint8_t good;
  121. struct {
  122. uint8_t alloc_node : 1;
  123. uint8_t alloc_str : 1;
  124. } cleanup = { 0, 0 };
  125.  
  126. // allocate node.
  127. p = (struct lnode *)malloc(sizeof(struct lnode));
  128. good = cleanup.alloc_node = (p != NULL);
  129.  
  130. // good? then allocate str
  131. if (good) {
  132. p->str = (char *)malloc(sizeof(char)*len);
  133. good = cleanup.alloc_str = (p->str != NULL);
  134. }
  135.  
  136. // good? copy data
  137. if(good) {
  138. memcpy ( p->str, data, len );
  139. }
  140.  
  141. // still good? insert in list
  142. if(good) {
  143. if(NULL == list) {
  144. p->next = NULL;
  145. list = p;
  146. } else {
  147. q = list;
  148. while(q->next != NULL && good) {
  149. // duplicate found--not good
  150. good = (strcmp(q->str,p->str) != 0);
  151. q = q->next;
  152. }
  153. if (good) {
  154. p->next = q->next;
  155. q->next = p;
  156. }
  157. }
  158. }
  159.  
  160. // not-good? cleanup.
  161. if(!good) {
  162. if(cleanup.alloc_str) free(p->str);
  163. if(cleanup.alloc_node) free(p);
  164. }
  165.  
  166. // good? return list or else return NULL
  167. return (good? list: NULL);
  168.  
  169. MyHandle * h = MyApiCreateHandle();
  170.  
  171. /* first call checks for pointer nullity, since we cannot retrieve error code
  172. on a NULL pointer */
  173. if (h == NULL)
  174. return 0;
  175.  
  176. /* from here h is a valid handle */
  177.  
  178. /* get a pointer to the error struct that will be updated with each call */
  179. MyApiError * err = MyApiGetError(h);
  180.  
  181.  
  182. MyApiFileDescriptor * fd = MyApiOpenFile("/path/to/file.ext");
  183.  
  184. /* we want to know what can go wrong */
  185. if (err->code != MyApi_ERROR_OK) {
  186. fprintf(stderr, "(%d) %sn", err->code, err->message);
  187. MyApiDestroy(h);
  188. return 0;
  189. }
  190.  
  191. MyApiRecord record;
  192.  
  193. /* here the API could refuse to execute the operation if the previous one
  194. yielded an error, and eventually close the file descriptor itself if
  195. the error is not recoverable */
  196. MyApiReadFileRecord(h, &record, sizeof(record));
  197.  
  198. /* we want to know what can go wrong, here using a macro checking for failure */
  199. if (MyApi_FAILED(err)) {
  200. fprintf(stderr, "(%d) %sn", err->code, err->message);
  201. MyApiDestroy(h);
  202. return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement