Commit 6433d10a authored by Vesa Oikonen's avatar Vesa Oikonen
Browse files

added csvTranspose()

parent 5e0d4c53
......@@ -286,7 +286,7 @@ int csvSetDimensions(
/*****************************************************************************/
/** Check whether CSV is regular, that is, each row contain the same number of columns.
@author Vesa Oikonen
@sa csvTrimRight, csvSetDimensions, csvRowLength, csvWrite, csvRead, csvDuplicate
@sa csvTranspose, csvTrimRight, csvSetDimensions, csvRowLength, csvRead, csvDuplicate
@return Returns 1 if CSV is regular, 0 if not.
*/
int csvIsRegular(
......@@ -315,7 +315,7 @@ int csvIsRegular(
Resulting CSV may still not be regular.
@author Vesa Oikonen
@sa csvIsRegular, csvRowLength, csvRead
@sa csvIsRegular, csvRowLength, csvRead, csvTranspose
@return enum tpcerror (TPCERROR_OK when successful).
*/
int csvTrimRight(
......@@ -375,7 +375,7 @@ char* csvCell(
/** Replace the value of CVS field.
@return tpcerror (TPCERROR_OK when successful).
@author Vesa Oikonen
@sa csvRead, csvSearchField, csvCell, csvRemoveItem
@sa csvRead, csvSearchField, csvCell, csvRemoveItem, csvTranspose
*/
int csvCellReplace(
/** Pointer to CSV. */
......@@ -498,7 +498,7 @@ static int csvReorgQSort(const void *c1, const void *c2)
/// @endcond
/** Sort CSV data array by increasing row and column numbers.
@return enum tpcerror (TPCERROR_OK when successful).
@sa csvList, csvAllocate, csvWrite
@sa csvTranspose, csvList, csvAllocate, csvWrite
*/
int csvReorg(
/** Pointer to CSV structure. */
......@@ -521,3 +521,49 @@ int csvReorg(
/*****************************************************************************/
/*****************************************************************************/
/** Transpose data in CSV structure.
To work properly, CSV should not contain comment cells/lines.
@return enum tpcerror (TPCERROR_OK when successful).
@sa csvIsRegular, csvDuplicate, csvCellReplace, csvReorg, csvRmComments
*/
int csvTranspose(
/** Pointer to CSV.*/
CSV *csv
) {
if(csv==NULL) return(TPCERROR_FAIL);
if(csv->nr<1) return(TPCERROR_NO_DATA);
/* Get dimensions */
{
int ret=csvSetDimensions(csv);
if(ret!=TPCERROR_OK) return(ret);
}
if(csv->row_nr<1 || csv->col_nr<1) return(TPCERROR_NO_DATA);
/* If only one row and column, then nothing to do */
if(csv->row_nr==1 && csv->col_nr==1) return(TPCERROR_OK);
/* Switch column and row numbers */
for(int i=0; i<csv->nr; i++) {
int cr=csv->c[i].col;
csv->c[i].col=csv->c[i].row;
csv->c[i].row=cr;
}
{
int cn=csv->col_nr;
csv->col_nr=csv->row_nr;
csv->row_nr=cn;
}
/* Sort CSV data by rows and columns */
{
int ret=csvReorg(csv, NULL);
if(ret!=TPCERROR_OK) return(ret);
}
return(TPCERROR_OK);
}
/*****************************************************************************/
/*****************************************************************************/
......@@ -105,6 +105,8 @@ int main(
fprintf(stderr, "failed (%d).\n", ret); return(i);}
i++; if((ret=test_csvReorg(&status))!=0) {
fprintf(stderr, "failed (%d).\n", ret); return(i);}
i++; if((ret=test_csvTranspose(&status))!=0) {
fprintf(stderr, "failed (%d).\n", ret); return(i);}
/* csvio */
i++; if((ret=test_csvList(&status))!=0) {
fprintf(stderr, "failed (%d).\n", ret); return(i);}
......
......@@ -921,3 +921,62 @@ int test_csvReorg(
/*****************************************************************************/
/*****************************************************************************/
int test_csvTranspose(
TPCSTATUS *status
) {
int verbose=0; if(status!=NULL) verbose=status->verbose;
statusSet(status, __func__, __FILE__, __LINE__, 0);
if(verbose>0) {
printf("\n=====================================\n");
printf("\n%s\n", __func__);
printf("\n=====================================\n");
}
CSV csv; csvInit(&csv);
if(verbose>1) printf("\nTest with stupid input\n");
if(csvTranspose(NULL)==TPCERROR_OK) return(1);
if(csvTranspose(&csv)==TPCERROR_OK) return(2);
if(verbose>1) printf(" ok\n");
if(verbose>1) printf("\n make test data with just one item\n");
if(csvAllocate(&csv, 30)!=TPCERROR_OK) return(100);
csv.col_nr=6; csv.row_nr=5;
csv.separator=',';
int i=0; csv.c[i].row=2; csv.c[i].col=3; csv.c[i].content=strdup("cell34");
csv.nr=1+i;
if(verbose>3) csvList(&csv, stdout);
if(csvTranspose(&csv)!=TPCERROR_OK) {csvFree(&csv); return(101);}
if(verbose>3) {printf("->\n"); csvList(&csv, stdout);}
if(csv.c[0].row!=3 || csv.c[0].col!=2 || strcmp(csv.c[0].content, "cell34")) {csvFree(&csv); return(102);}
csvFree(&csv);
if(verbose>1) printf(" ok\n");
if(verbose>1) printf("\n make test data with several items\n");
if(csvAllocate(&csv, 30)!=TPCERROR_OK) return(200);
csv.col_nr=6; csv.row_nr=5;
csv.separator='\t';
i=0; csv.c[i].row=2; csv.c[i].col=3; csv.c[i].content=strdup("cell34");
i=1; csv.c[i].row=2; csv.c[i].col=4; csv.c[i].content=strdup("cell35");
i=2; csv.c[i].row=4; csv.c[i].col=1; csv.c[i].content=strdup("cell52");
i=3; csv.c[i].row=4; csv.c[i].col=5; csv.c[i].content=strdup("cell56");
i=4; csv.c[i].row=0; csv.c[i].col=0; csv.c[i].content=strdup("cell11");
csv.nr=1+i;
if(verbose>3) csvWrite(&csv, 1, stdout, NULL);
if(csvTranspose(&csv)!=TPCERROR_OK) {csvFree(&csv); return(201);}
if(verbose>3) {printf("->\n"); csvWrite(&csv, 1, stdout, NULL);}
if(verbose>3) csvList(&csv, stdout);
i=0; if(csv.c[i].row!=0 || csv.c[i].col!=0 || strcmp(csv.c[i].content, "cell11")) {csvFree(&csv); return(202);}
i=1; if(csv.c[i].row!=1 || csv.c[i].col!=4 || strcmp(csv.c[i].content, "cell52")) {csvFree(&csv); return(203);}
i=2; if(csv.c[i].row!=3 || csv.c[i].col!=2 || strcmp(csv.c[i].content, "cell34")) {csvFree(&csv); return(204);}
i=3; if(csv.c[i].row!=4 || csv.c[i].col!=2 || strcmp(csv.c[i].content, "cell35")) {csvFree(&csv); return(205);}
i=4; if(csv.c[i].row!=5 || csv.c[i].col!=4 || strcmp(csv.c[i].content, "cell56")) {csvFree(&csv); return(206);}
csvFree(&csv);
if(verbose>1) printf(" ok\n");
statusSet(status, __func__, __FILE__, __LINE__, 0);
return(0);
}
/*****************************************************************************/
/*****************************************************************************/
......@@ -33,6 +33,7 @@ int test_csvRemoveItem(TPCSTATUS *status);
int test_csvRemoveEmptyRows(TPCSTATUS *status);
int test_csvRemoveComments(TPCSTATUS *status);
int test_csvReorg(TPCSTATUS *status);
int test_csvTranspose(TPCSTATUS *status);
/*****************************************************************************/
/*****************************************************************************/
......
......@@ -69,6 +69,7 @@ int csvRemoveItem(CSV *csv, int i);
int csvRemoveEmptyRows(CSV *csv);
int csvRemoveComments(CSV *csv);
int csvReorg(CSV *d, TPCSTATUS *status);
int csvTranspose(CSV *d);
/*****************************************************************************/
/*****************************************************************************/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment