nt DLLEXPORT EN_loadpatternfile(EN_Project p, const char *filename, const char *id) /*---------------------------------------------------------------- ** Input: filename = name of the file containing pattern data ** id = ID for the new pattern ** Output: none ** Returns: error code ** Purpose: loads a time pattern from a file into a project. **---------------------------------------------------------------- */ { FILE *file; char line[MAXLINE+1]; int err = 0; int i; int len = 0; double value; double *values = NULL; int CHUNK = 50; if (!p->Openflag) return 102; file = fopen(filename, "r"); if (file == NULL) return 302; // Add the new pattern if ((err = EN_addpattern(p, id)) != 0) { fclose(file); return err; } // Get the index of the newly added pattern if ((err = EN_getpatternindex(p, id, &i)) != 0) { fclose(file); return err; } // Read pattern values while (fgets(line, sizeof(line), file) != NULL) { // Skip lines that don't contain valid numbers if (!getfloat(line, &value) continue; // Resize multiplier array if it's full if (len % CHUNK == 0) { values = (double *) realloc(values, (len + CHUNK) * sizeof(double)); // Abort if memory allocation error if (values == NULL) { fclose(file); return 101; } } values[len] = value; len++; } fclose(file); // Transfer multipliers to pattern err = EN_setpattern(p, i, values, len); free(values); return err; }