Skip to content
Snippets Groups Projects
Commit 1c1f4e93 authored by Vesa Oikonen's avatar Vesa Oikonen
Browse files

added iftCopyItems() with some tests

parent e5d539ad
No related branches found
No related tags found
No related merge requests found
...@@ -4,13 +4,14 @@ Changes in tpcclib ...@@ -4,13 +4,14 @@ Changes in tpcclib
This is a concise list of changes; commit messages can be found in the This is a concise list of changes; commit messages can be found in the
[Git repository](https://gitlab.utu.fi/vesoik/tpcclib.git). [Git repository](https://gitlab.utu.fi/vesoik/tpcclib.git).
## version 0.7.7 (2021-02-04, in development) ## version 0.7.7 (2021-02-11, in development)
- Added application fit2auc. - Added application fit2auc.
- Bug fix in application fit_dexp with option -1BL. - Bug fix in application fit_dexp with option -1BL.
- Started writing application taccat. - Started writing application taccat.
- Corrections and additions in libtpcextensions in v2. - Corrections and additions in libtpcextensions in v2.
- libtpcfunc in v2 supports more functions, and added mfEvalIntToInf. - libtpcfunc in v2 supports more functions, and added mfEvalIntToInf.
- Added iftCopyItems() in libtpcift in v2.
## version 0.7.6 (2020-06-28) ## version 0.7.6 (2020-06-28)
......
...@@ -55,7 +55,7 @@ void iftFree( ...@@ -55,7 +55,7 @@ void iftFree(
Either key or value can be empty, but not both of them. Either key or value can be empty, but not both of them.
@sa iftInit, iftFree, iftDelete, iftDeleteKey, iftPutDouble @sa iftInit, iftFree, iftDelete, iftDeleteKey, iftPutDouble, iftCopyItems
@return tpcerror (TPCERROR_OK when successful). @return tpcerror (TPCERROR_OK when successful).
@pre Before first use initialize the IFT struct with iftInit(). @pre Before first use initialize the IFT struct with iftInit().
@author Vesa Oikonen @author Vesa Oikonen
...@@ -342,7 +342,7 @@ int iftReplaceKey( ...@@ -342,7 +342,7 @@ int iftReplaceKey(
The first occurrence of the key is kept. The first occurrence of the key is kept.
Search is case-insensitive, but otherwise key name match must be exact. Search is case-insensitive, but otherwise key name match must be exact.
@sa iftDeleteKey, iftFindKey @sa iftDeleteKey, iftFindKey, iftCopyItems
@return tpcerror (TPCERROR_OK when successful). @return tpcerror (TPCERROR_OK when successful).
*/ */
int iftDeleteDuplicateKeys( int iftDeleteDuplicateKeys(
...@@ -376,3 +376,57 @@ int iftDeleteDuplicateKeys( ...@@ -376,3 +376,57 @@ int iftDeleteDuplicateKeys(
/*****************************************************************************/ /*****************************************************************************/
/*****************************************************************************/ /*****************************************************************************/
/** Copy items from one IFT struct into another.
Duplicates, empty source, or no copied items are not considered as errors.
@sa iftDeleteDuplicateKeys, iftPut
@return tpcerror (TPCERROR_OK when successful).
*/
int iftCopyItems(
/** Pointer to target IFT. */
IFT *ift1,
/** Pointer to source IFT. */
IFT *ift2,
/** Specifies whether items without key name are copied (0) or not (1). */
int is_key_required,
/** Specifies whether items without value are copied (0) or not (1). */
int is_value_required,
/** Specifies whether comment items are copied or not.
- 0 = comment lines are not copied,
- 1 = also comment lines are copied,
- 2 = only comment lines are copied. */
int is_comment_accepted,
/** Pointer to status data; enter NULL if not needed */
TPCSTATUS *status
) {
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_FAIL);
if(ift1==NULL) return TPCERROR_FAIL;
int verbose=0; if(status!=NULL) verbose=status->verbose-1;
if(verbose>0)
printf("%s(*ift1, *ift2, %d, %d, %d, status)\n", __func__,
is_key_required, is_value_required, is_comment_accepted);
if(ift2==NULL || ift2->keyNr<1) {
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
return TPCERROR_OK;
}
/* Copy the appropriate items */
int n=0;
for(int i=0; i<ift2->keyNr; i++) {
if(is_key_required && (ift2->item[i].key==NULL || strlen(ift2->item[i].key)<1)) continue;
if(is_value_required && (ift2->item[i].value==NULL || strlen(ift2->item[i].value)<1)) continue;
if(ift2->item[i].comment!=0 && is_comment_accepted==0) continue;
if(ift2->item[i].comment==0 && is_comment_accepted==2) continue;
int ret=iftPut(ift1, ift2->item[i].key, ift2->item[i].value, ift2->item[i].comment, status);
if(ret!=TPCERROR_OK) return(status->error);
n++;
}
if(verbose>1) printf(" %d item(s) copied.\n", n);
statusSet(status, __func__, __FILE__, __LINE__, TPCERROR_OK);
return TPCERROR_OK;
}
/*****************************************************************************/
/*****************************************************************************/
...@@ -91,6 +91,8 @@ int main( ...@@ -91,6 +91,8 @@ int main(
fprintf(stderr, "failed (%d).\n", ret); return(i);} fprintf(stderr, "failed (%d).\n", ret); return(i);}
i++; if((ret=test_iftDeleteDuplicateKeys(verbose))!=0) { i++; if((ret=test_iftDeleteDuplicateKeys(verbose))!=0) {
fprintf(stderr, "failed (%d).\n", ret); return(i);} fprintf(stderr, "failed (%d).\n", ret); return(i);}
i++; if((ret=test_iftCopyItems(verbose))!=0) {
fprintf(stderr, "failed (%d).\n", ret); return(i);}
/* iftio */ /* iftio */
i++; if((ret=test_iftWriteItem(verbose))!=0) { i++; if((ret=test_iftWriteItem(verbose))!=0) {
fprintf(stderr, "failed (%d).\n", ret); return(i);} fprintf(stderr, "failed (%d).\n", ret); return(i);}
......
...@@ -336,3 +336,77 @@ int test_iftDeleteDuplicateKeys( ...@@ -336,3 +336,77 @@ int test_iftDeleteDuplicateKeys(
/*****************************************************************************/ /*****************************************************************************/
/*****************************************************************************/ /*****************************************************************************/
int test_iftCopyItems(
int verbose
) {
if(verbose>0) {
printf("\n=====================================\n");
printf("\n%s\n", __func__);
printf("\n=====================================\n");
}
IFT ift1, ift2;
iftInit(&ift1); iftInit(&ift2);
int ret;
// Target NULL -> error
ret=iftCopyItems(NULL, &ift2, 0, 0, 0, NULL);
if(ret==TPCERROR_OK) {iftFree(&ift1); iftFree(&ift2); return(1);}
// Both IFT empty -> ok, but nothing added
ret=iftCopyItems(&ift1, &ift2, 0, 0, 0, NULL);
if(ret!=TPCERROR_OK || ift1.keyNr!=0) {iftFree(&ift1); iftFree(&ift2); return(11);}
// Target IFT empty -> copied
if(iftPut(&ift2, "skey1", "svalue1", 0, NULL)) {iftFree(&ift1); iftFree(&ift2); return(20);}
if(iftCopyItems(&ift1, &ift2, 0, 0, 0, NULL) != TPCERROR_OK) {iftFree(&ift1); iftFree(&ift2); return(21);}
if(ift1.keyNr!=1) {iftFree(&ift1); iftFree(&ift2); return(22);}
if(iftFindPair(&ift1, ift2.item[0].key, ift2.item[0].value, 0) != 0) {iftFree(&ift1); iftFree(&ift2); return(23);}
// Require comment to copy
if(iftCopyItems(&ift1, &ift2, 0, 0, 2, NULL) != TPCERROR_OK) {iftFree(&ift1); iftFree(&ift2); return(31);}
if(ift1.keyNr!=1) {iftFree(&ift1); iftFree(&ift2); return(32);}
// Set comment
ift2.item[0].comment=1;
if(iftCopyItems(&ift1, &ift2, 0, 0, 2, NULL) != TPCERROR_OK) {iftFree(&ift1); iftFree(&ift2); return(33);}
if(ift1.keyNr!=2) {iftFree(&ift1); iftFree(&ift2); return(34);}
// Copy only if not comment
if(iftCopyItems(&ift1, &ift2, 0, 0, 0, NULL) != TPCERROR_OK) {iftFree(&ift1); iftFree(&ift2); return(35);}
if(ift1.keyNr!=2) {iftFree(&ift1); iftFree(&ift2); return(36);}
if(verbose>2) iftWrite(&ift1, stdout, NULL);
// Copy comment or no comment
iftFree(&ift2);
if(iftCopyItems(&ift2, &ift1, 0, 0, 1, NULL) != TPCERROR_OK) {iftFree(&ift1); iftFree(&ift2); return(41);}
if(ift2.keyNr!=2) {iftFree(&ift1); iftFree(&ift2); return(42);}
iftFree(&ift1); iftFree(&ift2);
// Missing key or value
if(iftPut(&ift2, "skey1", "svalue1", 0, NULL)) {iftFree(&ift1); iftFree(&ift2); return(100);}
if(iftPut(&ift2, "skey2", "", 0, NULL)) {iftFree(&ift1); iftFree(&ift2); return(101);}
if(iftPut(&ift2, "", "svalue3", 0, NULL)) {iftFree(&ift1); iftFree(&ift2); return(102);}
// Require both key and value
if(iftCopyItems(&ift1, &ift2, 1, 1, 1, NULL) != TPCERROR_OK) {iftFree(&ift1); iftFree(&ift2); return(111);}
if(ift1.keyNr!=1 || strcmp(ift1.item[0].key, "skey1")) {iftFree(&ift1); iftFree(&ift2); return(112);}
// Ignore missing key or value
if(iftCopyItems(&ift1, &ift2, 0, 0, 1, NULL) != TPCERROR_OK) {iftFree(&ift1); iftFree(&ift2); return(113);}
if(ift1.keyNr!=4 || strcmp(ift1.item[3].value, "svalue3")) {iftFree(&ift1); iftFree(&ift2); return(114);}
if(verbose>2) {printf("---\n"); iftWrite(&ift1, stdout, NULL);}
// Require key
if(iftCopyItems(&ift1, &ift2, 1, 0, 1, NULL) != TPCERROR_OK) {iftFree(&ift1); iftFree(&ift2); return(115);}
if(verbose>2) {printf("---\n"); iftWrite(&ift1, stdout, NULL);}
if(ift1.keyNr!=6 || strcmp(ift1.item[5].key, "skey2")) {iftFree(&ift1); iftFree(&ift2); return(116);}
// Require value
if(iftCopyItems(&ift1, &ift2, 0, 1, 1, NULL) != TPCERROR_OK) {iftFree(&ift1); iftFree(&ift2); return(117);}
if(verbose>2) {printf("---\n"); iftWrite(&ift1, stdout, NULL);}
if(ift1.keyNr!=8 || strcmp(ift1.item[7].value, "svalue3")) {iftFree(&ift1); iftFree(&ift2); return(118);}
iftFree(&ift1); iftFree(&ift2);
return(0);
}
/*****************************************************************************/
/*****************************************************************************/
...@@ -28,6 +28,7 @@ extern int test_iftDuplicate(int verbose); ...@@ -28,6 +28,7 @@ extern int test_iftDuplicate(int verbose);
extern int test_iftReplaceValue(int verbose); extern int test_iftReplaceValue(int verbose);
extern int test_iftReplaceKey(int verbose); extern int test_iftReplaceKey(int verbose);
extern int test_iftDeleteDuplicateKeys(int verbose); extern int test_iftDeleteDuplicateKeys(int verbose);
extern int test_iftCopyItems(int verbose);
/*****************************************************************************/ /*****************************************************************************/
/*****************************************************************************/ /*****************************************************************************/
......
...@@ -71,6 +71,9 @@ extern int iftDuplicate(IFT *ift1, IFT *ift2); ...@@ -71,6 +71,9 @@ extern int iftDuplicate(IFT *ift1, IFT *ift2);
extern int iftReplaceValue(IFT *ift, int i, const char *value, TPCSTATUS *status); extern int iftReplaceValue(IFT *ift, int i, const char *value, TPCSTATUS *status);
extern int iftReplaceKey(IFT *ift, int i, const char *key, TPCSTATUS *status); extern int iftReplaceKey(IFT *ift, int i, const char *key, TPCSTATUS *status);
extern int iftDeleteDuplicateKeys(IFT *ift, TPCSTATUS *status); extern int iftDeleteDuplicateKeys(IFT *ift, TPCSTATUS *status);
extern int iftCopyItems(
IFT *ift1, IFT *ift2, int is_key_required, int is_value_required, int is_comment_accepted, TPCSTATUS *status
);
/*****************************************************************************/ /*****************************************************************************/
/*****************************************************************************/ /*****************************************************************************/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment