comparison audioDB.cpp @ 367:3b6cd1dfbddb gcc-4.3-cleanups

deal with the write() calls in implementation of the API, too. (Can't use CHECKED_WRITE() because audioDB::error() is out of scope, so we have to do it the old-fashioned way.)
author mas01cr
date Wed, 12 Nov 2008 15:23:30 +0000
parents ee0a97b3a630
children
comparison
equal deleted inserted replaced
366:521812d63516 367:3b6cd1dfbddb
991 int audiodb_batchinsert(adb_ptr mydb, adb_insert_ptr ins, unsigned int size) { 991 int audiodb_batchinsert(adb_ptr mydb, adb_insert_ptr ins, unsigned int size) {
992 992
993 const char *argv[22]; 993 const char *argv[22];
994 int argvctr=0; 994 int argvctr=0;
995 unsigned int i=0; 995 unsigned int i=0;
996 int retval=0;
997 char tempfeaturename[]="tempfeatureXXXXXX"; 996 char tempfeaturename[]="tempfeatureXXXXXX";
998 char temppowername[]="temppowerXXXXXX"; 997 char temppowername[]="temppowerXXXXXX";
999 char tempkeyname[]="tempkeyXXXXXX"; 998 char tempkeyname[]="tempkeyXXXXXX";
1000 char temptimesname[]="temptimesXXXXXX"; 999 char temptimesname[]="temptimesXXXXXX";
1001 int tempfeaturefd=0; 1000 int tempfeaturefd = -1;
1002 int temppowerfd=0; 1001 int temppowerfd = -1;
1003 int tempkeyfd=0; 1002 int tempkeyfd = -1;
1004 int temptimesfd=0; 1003 int temptimesfd = -1;
1005 1004
1006 int flags[4]={0}; 1005 int flags[4]={0};
1007 int apierror=0; 1006 int apierror=0;
1008 1007
1009 /* So the final API should take an array of structs. However, the current 1008 /* So the final API should take an array of structs. However, the current
1027 if (ins[0].key){ flags[2]++;}; 1026 if (ins[0].key){ flags[2]++;};
1028 if (ins[0].times){ flags[3]++;}; 1027 if (ins[0].times){ flags[3]++;};
1029 1028
1030 1029
1031 /* make four temp files */ 1030 /* make four temp files */
1032 tempfeaturefd=mkstemp(tempfeaturename); 1031 if ((tempfeaturefd = mkstemp(tempfeaturename)) == -1)
1033 if (tempfeaturefd !=-1){ 1032 goto error;
1034 1033 if ((temppowerfd = mkstemp(temppowername)) == -1)
1035 temppowerfd=mkstemp(temppowername); 1034 goto error;
1036 if (temppowerfd !=-1){ 1035 if ((tempkeyfd=mkstemp(tempkeyname)) == -1)
1037 1036 goto error;
1038 tempkeyfd=mkstemp(tempkeyname); 1037 if ((temptimesfd=mkstemp(temptimesname)) == -1)
1039 if (tempkeyfd !=-1){ 1038 goto error;
1040 1039
1041 temptimesfd=mkstemp(temptimesname);
1042 if (temptimesfd !=-1){
1043
1044 } else {
1045 retval=-1;
1046 close(tempkeyfd);
1047 remove(tempkeyname);
1048 close(temppowerfd);
1049 remove(temppowername);
1050 close(tempfeaturefd);
1051 remove(tempfeaturename);
1052 }
1053
1054 } else {
1055 retval=-1;
1056 close(temppowerfd);
1057 remove(temppowername);
1058 close(tempfeaturefd);
1059 remove(tempfeaturename);
1060 }
1061
1062 } else {
1063 retval=-1;
1064 close(tempfeaturefd);
1065 remove(tempfeaturename);
1066 }
1067
1068 } else {
1069 retval=-1;
1070 }
1071
1072
1073
1074
1075
1076 if (retval == -1){
1077 return -1;
1078 }
1079
1080 /* Ok, so we should have a working set of files to write to */ 1040 /* Ok, so we should have a working set of files to write to */
1081 /* I'm going to assume that the same format is kept for all structs in the array */ 1041 /* I'm going to assume that the same format is kept for all structs in the array */
1082 /* That is, each struct should be correctly formed, and contain at least a features file, because I'm just going to pass the terms along to the text files */ 1042 /* That is, each struct should be correctly formed, and contain at least a features file, because I'm just going to pass the terms along to the text files */
1083 for (i=0; i<size; i++){ 1043 for (i = 0; i < size; i++) {
1084 1044 if (write(tempfeaturefd,ins[i].features,strlen(ins[i].features)) != (ssize_t) strlen(ins[i].features))
1085 write(tempfeaturefd,ins[i].features,strlen(ins[i].features)); 1045 goto error;
1086 write(tempfeaturefd,"\n",1); 1046 if (write(tempfeaturefd,"\n",1) != 1)
1087 1047 goto error;
1088 if (flags[1]){ 1048
1089 write(temppowerfd,ins[i].power,strlen(ins[i].power)); 1049 if (flags[1]) {
1090 write(temppowerfd,"\n",1); 1050 if (write(temppowerfd,ins[i].power,strlen(ins[i].power)) != (ssize_t) strlen(ins[i].power))
1091 } 1051 goto error;
1092 1052 if (write(temppowerfd,"\n",1) != 1)
1093 if (flags[2]){ 1053 goto error;
1094 write(tempkeyfd,ins[i].key,strlen(ins[i].key)); 1054 }
1095 write(tempkeyfd,"\n",1); 1055 if (flags[2]) {
1096 } 1056 if (write(tempkeyfd,ins[i].key,strlen(ins[i].key)) != (ssize_t) strlen(ins[i].key))
1097 1057 goto error;
1098 if (flags[3]){ 1058 if (write(tempkeyfd,"\n",1) != 1)
1099 write(temptimesfd,ins[i].times,strlen(ins[i].times)); 1059 goto error;
1100 write(temptimesfd,"\n",1); 1060 }
1101 } 1061 if (flags[3]) {
1102 } 1062 if (write(temptimesfd,ins[i].times,strlen(ins[i].times)) != (ssize_t) strlen(ins[i].times))
1063 goto error;
1064 if (write(temptimesfd,"\n",1) != 1)
1065 goto error;
1066 }
1067 }
1103 1068
1104 argv[argvctr++]="-F"; 1069 argv[argvctr++]="-F";
1105 argv[argvctr++]=tempfeaturename; 1070 argv[argvctr++]=tempfeaturename;
1106 close(tempfeaturefd); 1071 close(tempfeaturefd);
1107 close(temppowerfd); 1072 close(temppowerfd);
1132 remove(tempkeyname); 1097 remove(tempkeyname);
1133 remove(temptimesname); 1098 remove(temptimesname);
1134 1099
1135 1100
1136 return apierror; 1101 return apierror;
1102
1103 error:
1104 if(tempfeaturefd != -1) {
1105 close(tempfeaturefd);
1106 remove(tempfeaturename);
1107 }
1108 if(temppowerfd != -1) {
1109 close(temppowerfd);
1110 remove(temppowername);
1111 }
1112 if(tempkeyfd != -1) {
1113 close(tempkeyfd);
1114 remove(tempkeyname);
1115 }
1116 if(temptimesfd != -1) {
1117 close(temptimesfd);
1118 remove(temptimesname);
1119 }
1120 return -1;
1137 } 1121 }
1138 1122
1139 1123
1140 int audiodb_query(adb_ptr mydb, adb_query_ptr adbq, adb_queryresult_ptr adbqr){ 1124 int audiodb_query(adb_ptr mydb, adb_query_ptr adbq, adb_queryresult_ptr adbqr){
1141 1125